core/index.js

177 lines
4.1 KiB
JavaScript
Raw Permalink Normal View History

2020-09-15 18:35:00 +08:00
/**
* 框架核心
2020-09-28 10:03:27 +08:00
* @author yutent<yutent.io@gmail.com>
* @date 2020/09/28 10:01:47
2020-09-15 18:35:00 +08:00
*/
2020-09-15 19:06:27 +08:00
import 'es.shim' // 加载拓展方法
import http from 'http'
import path from 'path'
2020-09-18 18:14:47 +08:00
import fs from 'iofs'
2020-09-18 18:14:47 +08:00
import Request from '@gm5/request'
import Response from '@gm5/response'
2020-09-27 19:40:09 +08:00
import Views from '@gm5/views'
import { sessionPackage, sessionConnect } from '@gm5/session'
import { jwtPackage, jwtConnect } from '@gm5/jwt'
2020-09-15 19:06:27 +08:00
import config from './config/index.js'
2020-09-15 19:06:27 +08:00
import Routers from './middleware/router.js'
import Cors from './middleware/cors.js'
2020-09-15 18:35:00 +08:00
function hideProperty(host, name, value) {
Object.defineProperty(host, name, {
value: value,
2022-12-21 11:47:18 +08:00
enumerable: false
2020-09-15 18:35:00 +08:00
})
}
2022-12-21 11:47:18 +08:00
process.on('uncaughtException', err => {
console.error('UncaughtException: ', err)
})
2020-09-15 19:06:27 +08:00
export default class Five {
2020-09-15 18:35:00 +08:00
constructor() {
hideProperty(this, '__FIVE__', config)
2020-09-22 19:58:29 +08:00
hideProperty(this, '__MODULES__', {})
hideProperty(this, '__MIDDLEWARE__', [Cors])
2020-09-15 18:35:00 +08:00
}
__main__() {
var { domain, website, session, jwt } = this.__FIVE__
2020-09-30 14:25:08 +08:00
2020-09-15 18:35:00 +08:00
domain = domain || website
session.domain = session.domain || domain
2020-09-30 14:25:08 +08:00
2020-09-15 18:35:00 +08:00
this.set({ domain, session })
2020-09-27 19:40:09 +08:00
// 安装模板引擎
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)
}
2020-09-15 18:35:00 +08:00
// 路由中间件要在最后
this.use(Routers)
2020-09-15 18:35:00 +08:00
}
/*------------------------------------------------------------------------*/
// 注册属性到全局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]
} else {
try {
Object.assign(this.__FIVE__[i], obj[i])
} catch (err) {
console.error(err)
2020-09-15 18:35:00 +08:00
}
}
} else {
this.__FIVE__[i] = obj[i]
}
}
return this
}
// 获取全局配置
get(key) {
2020-09-22 19:58:29 +08:00
return this.__FIVE__[key]
2020-09-15 18:35:00 +08:00
}
2020-09-22 19:58:29 +08:00
// 加载中间件
2020-09-15 18:35:00 +08:00
// 与别的中间件用法有些不一样, 回调的传入参数中的req和res,
// 并非原生的request对象和response对象,
// 而是框架内部封装过的,可通过origin属性访问原生的对象
2020-09-22 19:58:29 +08:00
use(fn) {
if (typeof fn === 'function') {
this.__MIDDLEWARE__.push(fn)
return this
2020-09-15 18:35:00 +08:00
}
throw TypeError('argument must be a function')
}
// 注入实例化对象到实例池中
// 与use方法不同的是, 这个会在server创建之前就已经执行
install({ name, install }, args) {
this['$$' + name] = install.call(this, args)
return this
2020-09-15 18:35:00 +08:00
}
2020-09-22 19:58:29 +08:00
// 预加载应用, 缓存以提高性能
2020-09-15 18:35:00 +08:00
preload(dir) {
2020-09-16 20:10:24 +08:00
var list = fs.ls(dir)
2020-09-15 18:35:00 +08:00
if (list) {
2020-09-27 19:40:09 +08:00
list.forEach(item => {
2020-09-22 19:58:29 +08:00
var { name } = path.parse(item)
2020-09-15 18:35:00 +08:00
if (name.startsWith('.')) {
return
}
2020-09-22 19:58:29 +08:00
// 如果是目录,则默认加载index.js, 其他文件不加载
// 交由index.js自行处理, 用于复杂的应用
if (fs.isdir(item)) {
item = path.join(item, './index.js')
2020-09-15 18:35:00 +08:00
}
2020-09-22 19:58:29 +08:00
2020-09-27 19:40:09 +08:00
this.__MODULES__[name] = import(item).catch(err => {
2020-09-28 10:38:47 +08:00
console.error(err)
2020-09-22 19:58:29 +08:00
return { default: null }
})
2020-09-15 18:35:00 +08:00
})
}
return this
}
// 启动http服务
listen(port) {
var _this = this
2020-09-18 18:14:47 +08:00
var server
2020-09-15 18:35:00 +08:00
this.__main__()
2020-09-15 18:35:00 +08:00
2022-07-04 14:57:29 +08:00
server = http.createServer(function (req, res) {
2020-09-15 18:35:00 +08:00
var request = new Request(req, res)
2020-09-23 19:03:44 +08:00
var response = new Response(req, res)
2020-09-15 18:35:00 +08:00
var middleware = _this.__MIDDLEWARE__.concat()
var fn = middleware.shift()
2020-09-23 19:03:44 +08:00
if (response.rendered) {
return
}
2022-07-04 16:13:03 +08:00
response.set('X-Powered-By', _this.get('X-Powered-By') || 'Five.js')
2020-09-23 19:03:44 +08:00
2020-09-15 18:35:00 +08:00
if (fn) {
;(async function next() {
2022-07-04 14:57:29 +08:00
await fn.call(_this, request, response, function () {
2020-09-15 18:35:00 +08:00
fn = middleware.shift()
if (fn) {
next()
}
})
})()
}
})
server.listen(port || this.get('port'))
return server
}
}