mirror of
https://github.com/whyour/qinglong.git
synced 2026-07-01 04:40:38 +08:00
初始化api
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
import { Container } from 'typedi';
|
||||
import LoggerInstance from './logger';
|
||||
|
||||
export default ({ models }: { models: { name: string; model: any }[] }) => {
|
||||
try {
|
||||
models.forEach((m) => {
|
||||
Container.set(m.name, m.model);
|
||||
});
|
||||
|
||||
Container.set('logger', LoggerInstance);
|
||||
} catch (e) {
|
||||
LoggerInstance.error('🔥 Error on dependency injector loader: %o', e);
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,55 @@
|
||||
import { Request, Response, NextFunction, Application } from 'express';
|
||||
import bodyParser from 'body-parser';
|
||||
import cors from 'cors';
|
||||
import routes from '../api';
|
||||
import config from '../config';
|
||||
import jwt from 'express-jwt';
|
||||
|
||||
export default ({ app }: { app: Application }) => {
|
||||
app.enable('trust proxy');
|
||||
app.use(cors());
|
||||
|
||||
app.use(bodyParser.json());
|
||||
app.use(
|
||||
jwt({ secret: config.secret as string, algorithms: ['HS384'] }).unless({
|
||||
path: ['/api/auth'],
|
||||
}),
|
||||
);
|
||||
app.use(config.api.prefix, routes());
|
||||
|
||||
app.use((req, res, next) => {
|
||||
const err: any = new Error('Not Found');
|
||||
err['status'] = 404;
|
||||
next(err);
|
||||
});
|
||||
|
||||
app.use(
|
||||
(
|
||||
err: Error & { status: number },
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction,
|
||||
) => {
|
||||
if (err.name === 'UnauthorizedError') {
|
||||
return res.status(err.status).send({ message: err.message }).end();
|
||||
}
|
||||
return next(err);
|
||||
},
|
||||
);
|
||||
|
||||
app.use(
|
||||
(
|
||||
err: Error & { status: number },
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction,
|
||||
) => {
|
||||
res.status(err.status || 500);
|
||||
res.json({
|
||||
errors: {
|
||||
message: err.message,
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,15 @@
|
||||
import expressLoader from './express';
|
||||
import dependencyInjectorLoader from './dependencyInjector';
|
||||
import Logger from './logger';
|
||||
|
||||
export default async ({ expressApp }) => {
|
||||
Logger.info('✌️ DB loaded and connected!');
|
||||
|
||||
await dependencyInjectorLoader({
|
||||
models: [],
|
||||
});
|
||||
Logger.info('✌️ Dependency Injector loaded');
|
||||
|
||||
await expressLoader({ app: expressApp });
|
||||
Logger.info('✌️ Express loaded');
|
||||
};
|
||||
@@ -0,0 +1,32 @@
|
||||
import winston from 'winston';
|
||||
import config from '../config';
|
||||
|
||||
const transports = [];
|
||||
if (process.env.NODE_ENV !== 'development') {
|
||||
transports.push(new winston.transports.Console());
|
||||
} else {
|
||||
transports.push(
|
||||
new winston.transports.Console({
|
||||
format: winston.format.combine(
|
||||
winston.format.cli(),
|
||||
winston.format.splat(),
|
||||
),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
const LoggerInstance = winston.createLogger({
|
||||
level: config.logs.level,
|
||||
levels: winston.config.npm.levels,
|
||||
format: winston.format.combine(
|
||||
winston.format.timestamp({
|
||||
format: 'YYYY-MM-DD HH:mm:ss',
|
||||
}),
|
||||
winston.format.errors({ stack: true }),
|
||||
winston.format.splat(),
|
||||
winston.format.json(),
|
||||
),
|
||||
transports,
|
||||
});
|
||||
|
||||
export default LoggerInstance;
|
||||
Reference in New Issue
Block a user