From 2c755d84e5ec33be998935fdf017b26d9894979b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=95=8A=E5=B9=B3?= <951984189@qq.com> Date: Thu, 4 Feb 2021 11:32:43 +0800 Subject: [PATCH] 111 --- .vscode/launch.json | 34 -------------------- README.md | 2 +- README.zh-CN.md | 2 +- bootstrap.js | 7 +++++ package.json | 31 +++++++++--------- src/config/config.default.ts | 18 ++++++++--- src/config/config.local.ts | 20 ------------ src/configuration.ts | 56 +++++---------------------------- src/controller/api.ts | 28 ++--------------- src/controller/controller.ts | 36 --------------------- src/controller/home.ts | 28 ++++------------- src/decorator/CoolController.ts | 20 ------------ src/middleware/report.ts | 16 ---------- src/model/user.ts | 10 ------ src/service/user.ts | 1 - 15 files changed, 56 insertions(+), 253 deletions(-) delete mode 100644 .vscode/launch.json create mode 100644 bootstrap.js delete mode 100644 src/controller/controller.ts delete mode 100644 src/decorator/CoolController.ts delete mode 100644 src/middleware/report.ts delete mode 100644 src/model/user.ts diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index b5bfad1..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,34 +0,0 @@ -{ - // 使用 IntelliSense 了解相关属性。 - // 悬停以查看现有属性的描述。 - // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "name": "Midway Debug", - "type": "node", - "autoAttachChildProcesses": true, - "console": "integratedTerminal", - "env": { - "NODE_ENV": "local" - }, - "port": 9229, - // "preLaunchTask": "TypeScript compile", - "protocol": "auto", - "request": "launch", - "restart": true, - "runtimeArgs": [ - "run", - "debug", - "--", - "--inspect-brk" - ], - "runtimeExecutable": "npm", - "skipFiles": [ - // "${workspaceFolder}/node_modules/**/*.js", - "${workspaceFolder}/node_modules/rxjs/**/*.js", - "/**/*.js" - ] - } - ] -} diff --git a/README.md b/README.md index 74afd3e..c42e27f 100755 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# my_midway_project +# my-midway-project ## QuickStart diff --git a/README.zh-CN.md b/README.zh-CN.md index 6e1cbad..91e4d7a 100755 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -1,4 +1,4 @@ -# my_midway_project +# my-midway-project ## 快速入门 diff --git a/bootstrap.js b/bootstrap.js new file mode 100644 index 0000000..b59c58f --- /dev/null +++ b/bootstrap.js @@ -0,0 +1,7 @@ +const WebFramework = require('@midwayjs/web').Framework; +const web = new WebFramework().configure({ + port: 7001, +}); + +const { Bootstrap } = require('@midwayjs/bootstrap'); +Bootstrap.load(web).run(); diff --git a/package.json b/package.json index f53b034..7587a9f 100755 --- a/package.json +++ b/package.json @@ -1,27 +1,24 @@ { - "name": "cool-admin", + "name": "my-midway-project", "version": "1.0.0", "description": "", "private": true, "dependencies": { - "@midwayjs/decorator": "^2.7.0", - "@midwayjs/orm": "^1.3.0", "@midwayjs/web": "^2.7.3", - "axios": "^0.21.1", + "@midwayjs/decorator": "^2.7.0", "egg": "^2.29.1", - "egg-scripts": "^2.13.0", - "midwayjs-cool-core": "file:/Users/ap/Documents/srcs/cool-admin/midway-core/core/dist", - "mysql": "^2.18.1" + "egg-scripts": "^2.13.0" }, "devDependencies": { - "@midwayjs/cli": "^1.2.39", + "@midwayjs/cli": "^1.2.40", "@midwayjs/egg-ts-helper": "^1.0.5", + "@midwayjs/luckyeye": "^1.0.2", "@midwayjs/mock": "^2.7.2", "@types/jest": "^26.0.20", "@types/node": "14", "cross-env": "^7.0.3", - "jest": "^26.6.3", "mwts": "^1.1.2", + "jest": "^26.6.3", "ts-jest": "^26.5.0", "typescript": "^4.1.3" }, @@ -29,8 +26,8 @@ "node": ">=12.0.0" }, "scripts": { - "start": "egg-scripts start --title=midway-server-my_midway_project --framework=@midwayjs/web", - "stop": "egg-scripts stop --title=midway-server-my_midway_project", + "start": "egg-scripts start --daemon --title=my-midway-project --framework=@midwayjs/web", + "stop": "egg-scripts stop --title=my-midway-project", "start_build": "npm run build && cross-env NODE_ENV=development midway-bin dev", "dev": "cross-env ets && cross-env NODE_ENV=local TS_NODE_TYPE_CHECK=false TS_NODE_TRANSPILE_ONLY=true midway-bin dev --ts --port=8001", "test": "midway-bin test", @@ -38,16 +35,22 @@ "lint": "mwts check", "lint:fix": "mwts fix", "ci": "npm run cov", - "build": "midway-bin build -c" + "build": "midway-bin build -c", + "check": "luckyeye" }, "midway-bin-clean": [ ".vscode/.tsbuildinfo", "dist" ], + "midway-luckyeye": { + "packages": [ + "midway_v2" + ] + }, "repository": { "type": "git", "url": "" }, - "author": "anonymous", + "author": "", "license": "MIT" -} +} \ No newline at end of file diff --git a/src/config/config.default.ts b/src/config/config.default.ts index 6670ac7..ffbc1e7 100644 --- a/src/config/config.default.ts +++ b/src/config/config.default.ts @@ -4,12 +4,22 @@ export type DefaultConfig = PowerPartial; export default (appInfo: EggAppInfo) => { const config = {} as DefaultConfig; + // cookie sign key + config.keys = appInfo.name + 'cool-admin-next'; - // use for cookie sign key, should change to your own and keep security - config.keys = appInfo.name + '_1611125891022_7317'; + // 中间件 + //config.middleware = []; - // add your config here - config.middleware = []; + config.security = { + csrf: { + enable: false, + }, + }; + + // 替换成midway的日志 + config.midwayFeature = { + replaceEggLogger: true + } return config; }; diff --git a/src/config/config.local.ts b/src/config/config.local.ts index 41d16f8..e69de29 100644 --- a/src/config/config.local.ts +++ b/src/config/config.local.ts @@ -1,20 +0,0 @@ -import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg'; - -export type DefaultConfig = PowerPartial; - -export default (appInfo: EggAppInfo) => { - const config = {} as DefaultConfig; - - config.orm = { - type: 'mysql', - host: '139.196.196.203', - port: 3306, - username: 'midway', - password: 'Yxd8mJYE4p8BytHF', - database: 'midway', - synchronize: true, - logging: false, - } - - return config; -}; diff --git a/src/configuration.ts b/src/configuration.ts index 3b363a3..d7a3a4f 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -1,57 +1,17 @@ import { App, Configuration } from '@midwayjs/decorator'; -import { ILifeCycle, IMidwayContainer } from '@midwayjs/core'; -import * as orm from '@midwayjs/orm'; -import { listModule } from '@midwayjs/decorator'; +import { ILifeCycle } from '@midwayjs/core'; import { Application } from 'egg'; -import * as moment from 'moment'; -import * as cool from 'midwayjs-cool-core'; -@Configuration({ - imports: [ - // cool官方核心组件 官网:https://cool-js.com - cool, - // typeorm数据库组件 文档地址 https://typeorm.io/, 打不开可以用这个链接 https://typeorm.biunav.com/zh/ - orm - ], -}) +@Configuration() export class ContainerLifeCycle implements ILifeCycle { @App() app: Application; - async onReady(container?: IMidwayContainer): Promise { - //this.app.use(await this.app.generateMiddleware('reportMiddleware')); - // 格式化时间 - Date.prototype.toJSON = function () { - return moment(this).format('YYYY-MM-DD HH:mm:ss'); - }; - // 新增String支持replaceAll方法 - String.prototype['replaceAll'] = function (s1, s2) { - return this.replace(new RegExp(s1, 'gm'), s2); - }; - - console.log('加载配置') - // this.app.use(async (ctx, next) => { - // console.log('这边请求到了') - // await next(); - // }); - - - - - const MODEL_KEY = 'decorator:model'; - - // 可以获取到所有装饰了 @Model 装饰器的 class - const modules = listModule(MODEL_KEY); - for (let mod of modules) { - console.log(666, mod) - // 实现自定义能力 - // 从 mod 上拿元数据,做不同的处理 - // 提前初始化等 app.applicationContext.getAsync(getProvideId(mod)); - } + async onReady() { + this.app.use(async (ctx, next) => { + console.log('这边请求到了') + await next(); + }); } - async onStop?(container?: IMidwayContainer): Promise { - console.log('应用停止') - } - -} \ No newline at end of file +} diff --git a/src/controller/api.ts b/src/controller/api.ts index 774f3d2..c2a8f02 100644 --- a/src/controller/api.ts +++ b/src/controller/api.ts @@ -1,10 +1,6 @@ import { Inject, Controller, Provide, Query, Get } from '@midwayjs/decorator'; import { Context } from 'egg'; import { UserService } from '../service/user'; -import { Repository } from 'typeorm'; -import { User } from '../model/user'; -import { InjectEntityModel } from '@midwayjs/orm'; -import axios from 'axios'; @Provide() @Controller('/api') @@ -15,29 +11,9 @@ export class APIController { @Inject() userService: UserService; - @InjectEntityModel(User) - userModel: Repository; - @Get('/get_user') async getUser(@Query() uid) { - // const user = await this.userService.getUser({ uid }); - - // console.log(await this.userModel.findOne({id: 1})) - - // const result = await axios.get('https://docs-admin-cloud.cool-js.com/api/get_user?uid=1') - let index = 0; - while(true){ - try { - index++; - const startTime = Date.now(); - const result = await axios.get('https://m.cool-js.com/api/get_user?uid=1') - console.log(index,'请求时间', Date.now() - startTime, result.data); - } catch (error) { - console.log(6666666666666666666666666666666666666666) - continue; - } - } - - // return { success: true, message: 'OK', data: {uid, a: result.data} }; + const user = await this.userService.getUser({ uid }); + return { success: true, message: 'OK', data: user }; } } diff --git a/src/controller/controller.ts b/src/controller/controller.ts deleted file mode 100644 index 2562e3e..0000000 --- a/src/controller/controller.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { Scope, ScopeEnum, saveClassMetadata, saveModule, CONTROLLER_KEY, MiddlewareParamArray } from '@midwayjs/decorator'; - -export interface ControllerOption { - prefix: string; - routerOptions: { - sensitive?: boolean; - middleware?: MiddlewareParamArray; - alias?: string[]; - description?: string; - tagName?: string; - }; -} - -export function Controller( - prefix: string, - routerOptions: { - sensitive?: boolean; - middleware?: MiddlewareParamArray; - description?: string; - tagName?: string; - } = { middleware: [], sensitive: true } -): ClassDecorator { - return (target: any) => { - saveModule(CONTROLLER_KEY, target); - console.log(66666666, 'CONTROLLER_KEY', CONTROLLER_KEY) - saveClassMetadata( - CONTROLLER_KEY, - { - prefix, - routerOptions, - } as ControllerOption, - target - ); - Scope(ScopeEnum.Request)(target); - }; -} diff --git a/src/controller/home.ts b/src/controller/home.ts index 6f891de..b0d35f0 100755 --- a/src/controller/home.ts +++ b/src/controller/home.ts @@ -1,26 +1,10 @@ -import { Get, Query, Provide } from '@midwayjs/decorator'; -import { User } from '../model/user'; -import { Repository } from 'typeorm'; -import { InjectEntityModel } from '@midwayjs/orm'; -import { BaseController, Controller } from 'midwayjs-cool-core'; - -//import {Controller} from './controller'; +import { Controller, Get, Provide } from '@midwayjs/decorator'; @Provide() -@Controller() -export class HomeController extends BaseController{ - - @InjectEntityModel(User) - userModel: Repository - - getModel() { - return this.userModel; +@Controller('/') +export class HomeController { + @Get('/') + async home() { + return 'Hello Midwayjs!'; } - - // 根据ID 获得单条信息 - @Get("/info") - async info(@Query() id) { - return id; - } - } diff --git a/src/decorator/CoolController.ts b/src/decorator/CoolController.ts deleted file mode 100644 index b4184a5..0000000 --- a/src/decorator/CoolController.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { Scope, ScopeEnum, saveClassMetadata, saveModule } from '@midwayjs/decorator'; - -const MODEL_KEY = 'decorator:model'; - -export function CoolController(): ClassDecorator { - return (target: any) => { - // 将装饰的类,绑定到该装饰器,用于后续能获取到 class - saveModule(MODEL_KEY, target); - // 保存一些元数据信息,任意你希望存的东西 - saveClassMetadata( - MODEL_KEY, - { - test: 'abc' - }, - target - ); - // 指定 IoC 容器创建实例的作用域,这里注册为请求作用域,这样能取到 ctx - Scope(ScopeEnum.Request)(target); - }; -} \ No newline at end of file diff --git a/src/middleware/report.ts b/src/middleware/report.ts deleted file mode 100644 index cd72705..0000000 --- a/src/middleware/report.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { Provide } from '@midwayjs/decorator'; -import { IWebMiddleware, IMidwayWebNext } from '@midwayjs/web'; -import { Context } from 'egg'; - -@Provide() -export class ReportMiddleware implements IWebMiddleware { - - resolve() { - return async (ctx: Context, next: IMidwayWebNext) => { - const startTime = Date.now(); - await next(); - console.log('请求时间', Date.now() - startTime); - }; - } - -} \ No newline at end of file diff --git a/src/model/user.ts b/src/model/user.ts deleted file mode 100644 index adcfbd3..0000000 --- a/src/model/user.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { BaseModel } from 'midwayjs-cool-core'; -import { EntityModel } from '@midwayjs/orm'; -import { Column } from 'typeorm'; - -@EntityModel('user') -export class User extends BaseModel{ - - @Column({ name: "name" }) - name: string; -} \ No newline at end of file diff --git a/src/service/user.ts b/src/service/user.ts index 2e6a8e1..059480a 100644 --- a/src/service/user.ts +++ b/src/service/user.ts @@ -3,7 +3,6 @@ import { IUserOptions } from '../interface'; @Provide() export class UserService { - async getUser(options: IUserOptions) { return { uid: options.uid,