perf:优化代码

This commit is contained in:
xion 2024-11-21 02:09:05 +08:00
parent 801e05b23e
commit aa3e8f2bb6
4 changed files with 29 additions and 2 deletions

View File

@ -24,7 +24,9 @@
"keywords": [], "keywords": [],
"types": "types/index.d.ts", "types": "types/index.d.ts",
"files": [ "files": [
"types" "types",
"dist",
"src"
], ],
"license": "UNLICENSED", "license": "UNLICENSED",
"dependencies": { "dependencies": {

View File

@ -3,7 +3,7 @@ import { app } from './app.ts';
import './route.ts'; import './route.ts';
const config = useConfig(); const config = useConfig();
// import { app as aiApp } from '@kevisual/ai-lang/src/index.ts'; // import { app as aiApp } from '@kevisual/ai-lang/src/index.ts';
import { uploadMiddleware } from './lib/upload.ts'; import { uploadMiddleware } from './routes-simple/upload.ts';
import { loadApps } from './load-apps.ts'; import { loadApps } from './load-apps.ts';
// export { aiApp }; // export { aiApp };
export { app }; export { app };

View File

@ -0,0 +1,24 @@
import { User } from '@/models/user.ts';
import http from 'http';
export const error = (msg: string, code = 500) => {
return JSON.stringify({ code, message: msg });
};
export const checkAuth = async (req: http.IncomingMessage, res: http.ServerResponse) => {
const authroization = req.headers?.['authorization'] as string;
if (!authroization) {
res.statusCode = 401;
res.end(error('Invalid authorization'));
return { tokenUser: null, token: null };
}
const token = authroization.split(' ')[1];
let tokenUser;
try {
tokenUser = await User.verifyToken(token);
} catch (e) {
res.statusCode = 401;
res.end(error('Invalid token'));
return { tokenUser: null, token: null };
}
return { tokenUser, token };
};

View File

@ -21,6 +21,7 @@ const cacheFilePath = useFileStore('cache-file', { needExists: true });
let clients = []; let clients = [];
const router = new SimpleRouter(); const router = new SimpleRouter();
const error = (msg: string, code = 500) => { const error = (msg: string, code = 500) => {
return JSON.stringify({ code, message: msg }); return JSON.stringify({ code, message: msg });
}; };