From c9d1939502bff1b8a8104fc281b09a80175c95fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Thu, 24 Sep 2020 15:02:14 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E8=B7=AF=E7=94=B1;=E9=87=8D?= =?UTF-8?q?=E6=9E=84=E6=8E=A7=E5=88=B6=E5=99=A8;=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E8=B7=A8=E5=9F=9F=E4=B8=AD=E9=97=B4=E4=BB=B6;=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E6=97=A5=E5=BF=97=E6=A8=A1=E5=9D=97;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/reg-init.js => config/index.js | 1 - index.js | 65 ++++++++++---------------- lib/log.js | 48 ------------------- middleware/{credentials.js => cors.js} | 0 middleware/router.js | 42 ++++++++--------- package.json | 7 +-- 6 files changed, 48 insertions(+), 115 deletions(-) rename lib/reg-init.js => config/index.js (96%) delete mode 100644 lib/log.js rename middleware/{credentials.js => cors.js} (100%) diff --git a/lib/reg-init.js b/config/index.js similarity index 96% rename from lib/reg-init.js rename to config/index.js index b1c2c2f..488cdfb 100644 --- a/lib/reg-init.js +++ b/config/index.js @@ -23,7 +23,6 @@ export default { website: 'localhost', domain: '', // cookie域, 默认等于website port: 3000, - routeMode: 'action', // action | __main__ env: process.env.NODE_ENV === ENV_PROD ? ENV_PROD : ENV_DEV, debug: process.env.NODE_ENV === ENV_DEV, // debug模式 smtp: { diff --git a/index.js b/index.js index b023f98..da61f4c 100644 --- a/index.js +++ b/index.js @@ -9,20 +9,16 @@ import 'es.shim' // 加载拓展方法 import http from 'http' import path from 'path' import fs from 'iofs' -// import Ioredis from 'ioredis' + import Request from '@gm5/request' import Response from '@gm5/response' -// import Session from '@gm5/session' +import { sessionStore, sessionWare } from '@gm5/session' +import Jwt from '@gm5/jwt' -import init from './lib/reg-init.js' -import Log from './lib/log.js' //基础日志记录工具 +import config from './config/index.js' import routerWare from './middleware/router.js' -import credentialsWare from './middleware/credentials.js' - -// import sessionWare from './module/session.js' - -var log = console.log +import corsWare from './middleware/cors.js' function hideProperty(host, name, value) { Object.defineProperty(host, name, { @@ -35,37 +31,26 @@ function hideProperty(host, name, value) { export default class Five { constructor() { - hideProperty(this, '__FIVE__', Object.assign({}, init)) + hideProperty(this, '__FIVE__', config) hideProperty(this, '__MODULES__', {}) - hideProperty(this, '__MIDDLEWARE__', [credentialsWare]) + hideProperty(this, '__MIDDLEWARE__', [corsWare]) } - __init__() { + __main__() { var { domain, website, session } = this.__FIVE__ domain = domain || website session.domain = session.domain || domain this.set({ domain, session }) - // 这里只创建session的存储器, 而初始化操作在中间件中进行 - // if (session.type === 'redis') { - // hideProperty( - // this, - // '__SESSION_STORE__', - // new Ioredis({ - // host: session.db.host || '127.0.0.1', - // port: session.db.port || 6379, - // db: session.db.db || 0 - // }) - // ) - // } else { - // hideProperty(this, '__SESSION_STORE__', {}) - // } + // 安装jwt + this.install(Jwt) - // 将session和cookie的中间件提到最前 - // 以便用户自定义的中间件可以直接操作session和cookie - // this.__MIDDLEWARE__.unshift(sessionWare) - // this.__MIDDLEWARE__.unshift(credentialsWare) + // 将session中间件提到最前 + // 以便用户自定义的中间件可以直接操作session + this.install(sessionStore) + this.__MIDDLEWARE__.unshift(sessionWare) + // 路由中间件要在最后 this.use(routerWare) } @@ -81,7 +66,7 @@ export default class Five { try { Object.assign(this.__FIVE__[i], obj[i]) } catch (err) { - log(err) + console.error(err) } } } else { @@ -105,7 +90,14 @@ export default class Five { this.__MIDDLEWARE__.push(fn) return this } - throw TypeError('argument must be a callback') + throw TypeError('argument must be a function') + } + + // 注入实例化对象到实例池中 + // 与use方法不同的是, 这个会在server创建之前就已经执行 + install({ name, install }, args) { + this['$$' + name] = install.call(this, args) + return this } // 预加载应用, 缓存以提高性能 @@ -133,19 +125,12 @@ export default class Five { return this } - // 注入实例化对象到实例池中 - // 与use方法不同的是, 这个会在server创建之前就已经执行 - install({ name, install }) { - this['$$' + name] = install.call(this, this.__FIVE__) - return this - } - // 启动http服务 listen(port) { var _this = this var server - this.__init__() + this.__main__() server = http.createServer(function(req, res) { var request = new Request(req, res) diff --git a/lib/log.js b/lib/log.js deleted file mode 100644 index e9cd8a5..0000000 --- a/lib/log.js +++ /dev/null @@ -1,48 +0,0 @@ -/** - * 简单的日志封装 - * @author yutent - * @date 2020/09/18 16:07:26 - */ - -import fs from 'iofs' -import path from 'path' - -export default class Log { - constructor(file = 'run_time.log', dir = './') { - if (!dir) { - throw new Error(`agument dir must be a string, but ${typeof dir} given.`) - } - - if (!fs.exists(dir)) { - fs.mkdir(dir) - } - - this.file = path.resolve(dir, file) - } - - error(str) { - this.save(str, 'error') - } - - info(str) { - this.save(str, 'info') - } - - warn(str) { - this.save(str, 'warning') - } - - debug(str) { - this.save(str, 'debug') - } - - //写入日志文件 - save(str, type) { - type = type || 'debug' - fs.echo( - `[${type}] ${new Date().format('Y-m-d_H:i:s')} ${str} \n`, - this.file, - true - ) - } -} diff --git a/middleware/credentials.js b/middleware/cors.js similarity index 100% rename from middleware/credentials.js rename to middleware/cors.js diff --git a/middleware/router.js b/middleware/router.js index 7d529e2..620676e 100644 --- a/middleware/router.js +++ b/middleware/router.js @@ -6,10 +6,6 @@ export default function(req, res, next) { var debug = this.get('debug') - if (this.__MODULES__.__error__) { - var err = this.__MODULES__.__error__ - return res.error(debug ? err.stack || err : err, err.status || 500) - } // 1. 先判断控制器是否存在 if (!this.__MODULES__[req.app]) { @@ -23,32 +19,32 @@ export default function(req, res, next) { // 3. 实例化控制器 this.__MODULES__[req.app] - .then(({ default: Mod }) => { + .then(async ({ default: Mod }) => { var app var err = '' if (Mod) { - app = new Mod({ ctx: this, req, res }) + app = new Mod() + app.__f_i_v_e__(this, req, res) - // action模式, 则路由自动调用对应的action方法 - // __main__模式, 则路由全部走__main__方法 - if (this.get('routeMode') === 'action') { - var route = req.path.shift() - var act = route + 'Action' - - if (app[act]) { - return app[act].apply(app, req.path) - } else { - err = new Error(`Route [${route}] not found`) - } - } else { - if (app.__main__) { - return app.__main__.apply(app, req.path) - } else { - err = new Error('__main__() not found') + // 4. 优先执行__main__方法 + if (app.__main__) { + try { + await app.__main__() + } catch (err) { + return Promise.reject(err) } } - err.status = 404 + + var route = req.path.shift() + var act = route + 'Action' + + if (app[act]) { + return app[act].apply(app, req.path) + } else { + err = new Error(`Route [${route}] not found`) + err.status = 404 + } } else { err = new Error(`Controller [${req.app}] load error`) err.status = 500 diff --git a/package.json b/package.json index d0c7b2c..1d376aa 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,16 @@ { "name": "@gm5/core", - "version": "3.2.6", + "version": "1.0.0", "type": "module", "description": "Five.js, 一个轻量级的nodejs mvc框架 旨在简单易用, 5分钟即可上手", "author": "yutent ", "main": "index.js", "dependencies": { "@gm5/session": "^1.0.0", - "@gm5/request": "^1.2.0", - "@gm5/response": "^1.3.0", + "@gm5/request": "^1.2.1", + "@gm5/response": "^1.3.1", "@gm5/controller": "^1.0.0", + "@gm5/jwt": "^1.1.0", "crypto.js": "^2.0.1", "es.shim": "^2.0.1", "iofs": "^1.5.0"