diff --git a/index.js b/index.js index 3b52c6f..3b21f73 100644 --- a/index.js +++ b/index.js @@ -11,58 +11,58 @@ import fs from 'iofs' import Request from '@gm5/request' import Response from '@gm5/response' -import Views from '@gm5/views' -import { sessionPackage, sessionConnect } from '@gm5/session' -import { jwtPackage, jwtConnect } from '@gm5/jwt' + +// import { sessionPackage, sessionConnect } from '@gm5/session' +// import { jwtPackage, jwtConnect } from '@gm5/jwt' import config from './config/index.js' import Routers from './middleware/router.js' import Cors from './middleware/cors.js' -function hideProperty(host, name, value) { - Object.defineProperty(host, name, { - value: value, - enumerable: false - }) -} - process.on('uncaughtException', err => { console.error('UncaughtException: ', err) }) -export default class Five { +class Five { #config = config #modules = {} #middlewares = [Cors] - #start() { - var { domain, website, session, jwt } = this.#config + #loadBuildIn() { + let { domain, website, session, jwt } = this.#config domain = domain || website session.domain = session.domain || domain this.set({ domain, session }) - // 安装模板引擎 - this.install(Views) - // 将jwt & session中间件提到最前 // 以便用户自定义的中间件可以直接操作session - if (session.enabled) { - this.install(sessionPackage) - this.#middlewares.unshift(sessionConnect) - } - // 开启jwt - if (jwt) { - this.install(jwtPackage) - this.#middlewares.unshift(jwtConnect) - } + // if (session.enabled) { + // this.install(sessionPackage) + // this.#middlewares.unshift(sessionConnect) + // } + // // 开启jwt + // if (jwt) { + // this.install(jwtPackage) + // this.#middlewares.unshift(jwtConnect) + // } // 路由中间件要在最后 this.use(Routers) } + async #loop(req, res, idx = 0) { + let fn = this.#middlewares[idx] + if (fn) { + await fn.call(this, req, res, _ => { + idx++ + this.#loop(req, res, idx) + }) + } + } + /*------------------------------------------------------------------------*/ // 注册属性到全局Five对象 @@ -125,10 +125,12 @@ export default class Five { item = path.join(item, './index.js') } - this.#modules[name] = import(item).catch(err => { - console.error(err) - return { default: null } - }) + this.#modules[name] = import(item) + .then(r => r.default) + .catch(err => { + console.error(err) + return { default: null } + }) }) } @@ -146,49 +148,36 @@ export default class Five { } run() { - var _this = this - var server + this.#loadBuildIn() - this.#start() + let server = http.createServer() - server = http.createServer() + this.server = server server - .on('request', function (req, res) { - var request = new Request(req, res) - var response = new Response(req, res) - - var middleware = _this.#middlewares.concat() - var fn = middleware.shift() + .on('request', (req, res) => { + let request = new Request(req, res) + let response = new Response(req, res) if (response.rendered) { return } - response.set('X-Powered-By', _this.get('X-Powered-By') || 'Five.js') + response.set('X-Powered-By', this.get('X-Powered-By') || 'Five.js') - if (fn) { - ;(async function next() { - await fn.call(_this, request, response, function () { - fn = middleware.shift() - if (fn) { - next() - } - }) - })() - } + this.#loop(request, response) }) .on('listening', _ => { - console.log('启动成功, 可通过以下地址访问') - console.log(' 本地: %s://%s:%d', 'http', '127.0.0.1', this.get('port')) + if(this.get('debug')){ + console.log('Server successfully started ...') + console.log('%s://%s:%d\n', 'http', '127.0.0.1', this.get('port')) + } }) .on('error', err => { console.error(err) }) + .listen(this.get('port')) - server.listen(this.get('port')) - - this.server = server return this } } diff --git a/lib.js b/lib.js new file mode 100644 index 0000000..4a3a014 --- /dev/null +++ b/lib.js @@ -0,0 +1,17 @@ +/** + * {} + * @author yutent + * @date 2023/10/24 18:23:21 + */ + +export function noop() {} + +export function readonlyProp(host, name, value) { + Object.defineProperty(host, name, { + get() { + return value + }, + set(vale) {}, + enumerable: false + }) +} diff --git a/middleware/router.js b/middleware/router.js index 4c83715..ce01128 100644 --- a/middleware/router.js +++ b/middleware/router.js @@ -4,6 +4,8 @@ * @date 2020/09/18 15:16:29 */ +import { readonlyProp } from '../lib.js' + export default function (req, res, next) { var debug = this.get('debug') var spa = this.get('spa') @@ -20,18 +22,22 @@ export default function (req, res, next) { // 3. 实例化控制器 this.$load(spa ? 'index' : req.app) - .then(async ({ default: Mod }) => { - let app, route, act + .then(async Module => { + let mod, route, act let err = '' - if (Mod) { - app = new Mod() - app.__f_i_v_e__(this, req, res) + if (Module) { + mod = new Module() + + readonlyProp(mod, 'context', this) + readonlyProp(mod, 'request', req) + readonlyProp(mod, 'response', res) + readonlyProp(mod, 'name', req.app) // 4. 优先执行__main__方法 - if (app.__main__) { + if (mod.__main__) { try { - let r = await app.__main__() + let r = await mod.__main__() if (r === false) { return } @@ -41,13 +47,13 @@ export default function (req, res, next) { } if (spa) { - return app.indexAction.apply(app, req.path) + return mod.indexAction.apply(mod, req.path) } else { route = req.path.shift() act = route + 'Action' - if (app[act]) { - return app[act].apply(app, req.path) + if (mod[act]) { + return mod[act].apply(mod, req.path) } else { err = new Error(`Action [${route}] not found`) err.status = 404 @@ -61,6 +67,7 @@ export default function (req, res, next) { return Promise.reject(err) }) .catch(err => { + console.error(err) res.error(debug ? err.stack || err : err, err.status || 500) }) }