Compare commits
2 Commits
04069523d3
...
f939dcca39
Author | SHA1 | Date |
---|---|---|
yutent | f939dcca39 | |
yutent | 26bc8622d2 |
|
@ -51,8 +51,8 @@ export default {
|
|||
origin: [], // ['abc.com', 'a.foo.com']
|
||||
maxAge: 14400
|
||||
},
|
||||
// 常用正则
|
||||
regexp: {
|
||||
// 常用正则
|
||||
email: /^[\w\.\-]+@\w+([\.\-]\w+)*\.\w+$/,
|
||||
uname: /^[A-Za-z\d_]{4,16}$/,
|
||||
passwd: /^\S{6,20}$/,
|
||||
|
|
141
index.js
141
index.js
|
@ -11,77 +11,75 @@ 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 {
|
||||
constructor() {
|
||||
hideProperty(this, '__FIVE__', config)
|
||||
hideProperty(this, '__MODULES__', {})
|
||||
hideProperty(this, '__MIDDLEWARE__', [Cors])
|
||||
}
|
||||
class Five {
|
||||
#config = config
|
||||
#modules = {}
|
||||
#middlewares = [Cors]
|
||||
|
||||
__main__() {
|
||||
var { domain, website, session, jwt } = this.__FIVE__
|
||||
#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.__MIDDLEWARE__.unshift(sessionConnect)
|
||||
}
|
||||
// 开启jwt
|
||||
if (jwt) {
|
||||
this.install(jwtPackage)
|
||||
this.__MIDDLEWARE__.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对象
|
||||
set(obj) {
|
||||
for (let i in obj) {
|
||||
if (typeof obj[i] === 'object' && !Array.isArray(obj[i])) {
|
||||
if (!this.__FIVE__[i]) {
|
||||
this.__FIVE__[i] = obj[i]
|
||||
if (!this.#config[i]) {
|
||||
this.#config[i] = obj[i]
|
||||
} else {
|
||||
try {
|
||||
Object.assign(this.__FIVE__[i], obj[i])
|
||||
Object.assign(this.#config[i], obj[i])
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
this.__FIVE__[i] = obj[i]
|
||||
this.#config[i] = obj[i]
|
||||
}
|
||||
}
|
||||
return this
|
||||
|
@ -89,7 +87,7 @@ export default class Five {
|
|||
|
||||
// 获取全局配置
|
||||
get(key) {
|
||||
return this.__FIVE__[key]
|
||||
return this.#config[key]
|
||||
}
|
||||
|
||||
// 加载中间件
|
||||
|
@ -98,7 +96,7 @@ export default class Five {
|
|||
// 而是框架内部封装过的,可通过origin属性访问原生的对象
|
||||
use(fn) {
|
||||
if (typeof fn === 'function') {
|
||||
this.__MIDDLEWARE__.push(fn)
|
||||
this.#middlewares.push(fn)
|
||||
return this
|
||||
}
|
||||
throw TypeError('argument must be a function')
|
||||
|
@ -127,50 +125,63 @@ 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 }
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
$load(name) {
|
||||
return this.#modules[name]
|
||||
}
|
||||
|
||||
// 启动http服务
|
||||
listen(port) {
|
||||
var _this = this
|
||||
var server
|
||||
this.set({ port })
|
||||
return this
|
||||
}
|
||||
|
||||
this.__main__()
|
||||
run() {
|
||||
this.#loadBuildIn()
|
||||
|
||||
server = http.createServer(function (req, res) {
|
||||
var request = new Request(req, res)
|
||||
var response = new Response(req, res)
|
||||
let server = http.createServer()
|
||||
|
||||
var middleware = _this.__MIDDLEWARE__.concat()
|
||||
var fn = middleware.shift()
|
||||
this.server = server
|
||||
|
||||
if (response.rendered) {
|
||||
return
|
||||
}
|
||||
server
|
||||
.on('request', (req, res) => {
|
||||
let request = new Request(req, res)
|
||||
let response = new Response(req, res)
|
||||
|
||||
response.set('X-Powered-By', _this.get('X-Powered-By') || 'Five.js')
|
||||
if (response.rendered) {
|
||||
return
|
||||
}
|
||||
|
||||
if (fn) {
|
||||
;(async function next() {
|
||||
await fn.call(_this, request, response, function () {
|
||||
fn = middleware.shift()
|
||||
if (fn) {
|
||||
next()
|
||||
}
|
||||
})
|
||||
})()
|
||||
}
|
||||
})
|
||||
response.set('X-Powered-By', this.get('X-Powered-By') || 'Five.js')
|
||||
|
||||
server.listen(port || this.get('port'))
|
||||
this.#loop(request, response)
|
||||
})
|
||||
.on('listening', _ => {
|
||||
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'))
|
||||
|
||||
return server
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
export function createApp() {
|
||||
return new Five()
|
||||
}
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
/**
|
||||
* {}
|
||||
* @author yutent<yutent.io@gmail.com>
|
||||
* @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
|
||||
})
|
||||
}
|
|
@ -4,12 +4,14 @@
|
|||
* @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')
|
||||
|
||||
// 1. 先判断控制器是否存在
|
||||
if (!this.__MODULES__[spa ? 'index' : req.app]) {
|
||||
if (!this.$load(spa ? 'index' : req.app)) {
|
||||
return res.error(`Controller [${req.app}] not found`, 404)
|
||||
}
|
||||
|
||||
|
@ -19,19 +21,23 @@ export default function (req, res, next) {
|
|||
}
|
||||
|
||||
// 3. 实例化控制器
|
||||
this.__MODULES__[spa ? 'index' : req.app]
|
||||
.then(async ({ default: Mod }) => {
|
||||
let app, route, act
|
||||
this.$load(spa ? 'index' : req.app)
|
||||
.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)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@gm5/core",
|
||||
"version": "1.1.5",
|
||||
"version": "2.0.0",
|
||||
"type": "module",
|
||||
"description": "Five.js, 一个轻量级的nodejs mvc框架 旨在简单易用, 5分钟即可上手",
|
||||
"author": "yutent <yutent.io@gmail.com>",
|
||||
|
|
Loading…
Reference in New Issue