first commit

This commit is contained in:
编码猿
2024-09-27 02:06:13 +08:00
commit 852d94fbb9
36760 changed files with 3274413 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
/**
* 启动我们的后端项目
*/
import "reflect-metadata";
import { Init } from './init'
import { useKoaServer,useContainer, Action } from 'routing-controllers';
import { join } from 'path';
import { Container } from 'typedi';
import { AuthorizationChecker } from '@/utils/authorizationChecker';
class App extends Init {
app: typeof this.koa;
authorizationChecker: AuthorizationChecker = new AuthorizationChecker();
constructor() {
super()
useContainer(Container);
this.app = useKoaServer(this.koa, {
authorizationChecker: this.authorizationChecker.check.bind(this.authorizationChecker),
cors: this.config.cors.configCors,
validation: true,
classTransformer: true,
controllers: [ `${join(__dirname, '../controller/**/*{.js,.ts}')}` ]
})
this.run()
}
run() {
this.app.listen(this.config.port, () => {
console.log( `
👏 博客后端接口 👏
👉 1:PC官网: http://127.0.0.1:${this.config.port}/
👉 2:Admin后台: xxxxx
`);
})
this.app.on('error', (err, ctx) => {
console.error('server error', err, ctx)
});
}
}
new App

View File

@@ -0,0 +1,51 @@
/**
* 初始化项目插件的配置
*/
import Application from 'koa'
import bodyparser from 'koa-bodyparser'
import json from 'koa-json'
import logger from 'koa-logger'
import staticPublic from 'koa-static'
// @ts-ignore
import onerror from 'koa-onerror'
import config, { ConfigType } from '@/config'
import { DataSource } from "typeorm";
import { MysqlConnectionOptions } from "typeorm/driver/mysql/MysqlConnectionOptions";
import { join } from 'path';
export class Init {
// 存放我们koa框架用到的所有插件
private plugin: Array<Application.Middleware<Application.DefaultState & { }, Application.DefaultContext & {}>> = []
protected koa: Application = new Application();
protected config: ConfigType = <ConfigType><unknown>config;
constructor() {
this.initPlugin()
}
initPlugin() {
onerror(this.koa)
// 初始化 typeorm 的连接,并保存到全局变量 orm 中
this.config.orm = new DataSource(<MysqlConnectionOptions>this.config.mysql)
this.config.orm.initialize()
// koa 的插件数据
this.plugin = [
bodyparser({
enableTypes:['json', 'form', 'text']
}),
json(),
// logger((str, args) => {
// console.log("str: ", str);
// console.log("args: ", args);
// }),
staticPublic(join(__dirname,config.staticPath))
]
this.plugin.forEach(v => this.koa.use(v))
}
}