perf:优化代码
This commit is contained in:
parent
801e05b23e
commit
aa3e8f2bb6
@ -24,7 +24,9 @@
|
||||
"keywords": [],
|
||||
"types": "types/index.d.ts",
|
||||
"files": [
|
||||
"types"
|
||||
"types",
|
||||
"dist",
|
||||
"src"
|
||||
],
|
||||
"license": "UNLICENSED",
|
||||
"dependencies": {
|
||||
|
@ -3,7 +3,7 @@ import { app } from './app.ts';
|
||||
import './route.ts';
|
||||
const config = useConfig();
|
||||
// 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';
|
||||
// export { aiApp };
|
||||
export { app };
|
||||
|
24
src/routes-simple/middleware/auth.ts
Normal file
24
src/routes-simple/middleware/auth.ts
Normal 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 };
|
||||
};
|
@ -21,6 +21,7 @@ const cacheFilePath = useFileStore('cache-file', { needExists: true });
|
||||
let clients = [];
|
||||
|
||||
const router = new SimpleRouter();
|
||||
|
||||
const error = (msg: string, code = 500) => {
|
||||
return JSON.stringify({ code, message: msg });
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user