core/index.js

192 lines
4.5 KiB
JavaScript
Raw 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 'node:http'
import { parse, join } from 'node: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'
2023-10-25 18:48:55 +08:00
// import { sessionPackage, sessionConnect } from '@gm5/session'
// import { jwtPackage, jwtConnect } from '@gm5/jwt'
2023-10-27 19:16:59 +08:00
import { noop, readonlyProp } from './lib.js'
import config from './config/index.js'
2020-09-15 19:06:27 +08:00
2023-10-27 19:16:59 +08:00
import { createRouter } from './middleware/router.js'
import { createCors } from './middleware/cors.js'
2020-09-15 18:35:00 +08:00
2022-12-21 11:47:18 +08:00
process.on('uncaughtException', err => {
console.error('UncaughtException: ', err)
})
2023-10-25 18:48:55 +08:00
class Five {
2023-10-24 15:24:55 +08:00
#config = config
#modules = {}
2023-10-27 19:16:59 +08:00
#middlewares = [createCors()]
2020-09-30 14:25:08 +08:00
2023-10-27 19:16:59 +08:00
constructor() {
readonlyProp(this, 'state', Object.create(null))
}
2020-09-15 18:35:00 +08:00
2023-10-27 19:16:59 +08:00
#loadBuildIn() {
// let { domain, website, session, jwt } = this.#config
// domain = domain || website
// session.domain = session.domain || domain
// this.set({ domain, session })
// 将jwt & session中间件提到最前
// 以便用户自定义的中间件可以直接操作session
2023-10-25 18:48:55 +08:00
// if (session.enabled) {
// this.install(sessionPackage)
// this.#middlewares.unshift(sessionConnect)
// }
// // 开启jwt
// if (jwt) {
// this.install(jwtPackage)
// this.#middlewares.unshift(jwtConnect)
// }
// 路由中间件要在最后
2023-10-27 19:16:59 +08:00
// this.use(createRouter())
2020-09-15 18:35:00 +08:00
}
2023-10-25 18:48:55 +08:00
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)
})
}
}
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])) {
2023-10-24 15:24:55 +08:00
if (!this.#config[i]) {
this.#config[i] = obj[i]
2020-09-15 18:35:00 +08:00
} else {
try {
2023-10-24 15:24:55 +08:00
Object.assign(this.#config[i], obj[i])
2020-09-15 18:35:00 +08:00
} catch (err) {
console.error(err)
2020-09-15 18:35:00 +08:00
}
}
} else {
2023-10-24 15:24:55 +08:00
this.#config[i] = obj[i]
2020-09-15 18:35:00 +08:00
}
}
return this
}
// 获取全局配置
get(key) {
2023-10-24 15:24:55 +08:00
return this.#config[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属性访问原生的对象
2023-10-27 19:16:59 +08:00
use(fn = noop) {
2020-09-22 19:58:29 +08:00
if (typeof fn === 'function') {
2023-10-24 15:24:55 +08:00
this.#middlewares.push(fn)
2020-09-22 19:58:29 +08:00
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) {
let 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 => {
let { name } = 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 = join(item, './index.js')
2020-09-15 18:35:00 +08:00
}
2020-09-22 19:58:29 +08:00
2023-10-25 18:48:55 +08:00
this.#modules[name] = import(item)
.then(r => r.default)
.catch(err => {
console.error(err)
2023-10-27 19:16:59 +08:00
return null
2023-10-25 18:48:55 +08:00
})
2020-09-15 18:35:00 +08:00
})
}
return this
}
2023-10-24 15:24:55 +08:00
$load(name) {
return this.#modules[name]
}
2020-09-15 18:35:00 +08:00
// 启动http服务
listen(port) {
2023-10-24 15:24:55 +08:00
this.set({ port })
return this
}
run() {
2023-10-25 18:48:55 +08:00
let server = http.createServer()
2020-09-15 18:35:00 +08:00
2023-10-25 18:48:55 +08:00
this.server = server
2020-09-15 18:35:00 +08:00
2023-10-27 19:16:59 +08:00
// this.#loadBuildIn()
// 路由中间件要在最后
this.use(createRouter())
2023-10-24 15:24:55 +08:00
server
2023-10-25 18:48:55 +08:00
.on('request', (req, res) => {
let request = new Request(req, res)
let response = new Response(req, res)
2020-09-23 19:03:44 +08:00
if (response.ended) {
2023-10-24 15:24:55 +08:00
return
}
2020-09-15 18:35:00 +08:00
2023-10-25 18:48:55 +08:00
response.set('X-Powered-By', this.get('X-Powered-By') || 'Five.js')
this.#loop(request, response)
2023-10-24 15:24:55 +08:00
})
.on('listening', _ => {
2023-10-27 19:16:59 +08:00
if (this.get('debug')) {
2023-10-25 18:48:55 +08:00
console.log('Server successfully started ...')
console.log('%s://%s:%d\n', 'http', '127.0.0.1', this.get('port'))
}
2023-10-24 15:24:55 +08:00
})
.on('error', err => {
console.error(err)
})
2023-10-25 18:48:55 +08:00
.listen(this.get('port'))
2020-09-15 18:35:00 +08:00
2023-10-24 15:24:55 +08:00
return this
2020-09-15 18:35:00 +08:00
}
}
2023-10-24 15:24:55 +08:00
export function createApp(conf = {}) {
let app = new Five()
app.set(conf)
return app
2023-10-24 15:24:55 +08:00
}