一波重构

v2
yutent 2023-10-27 19:16:59 +08:00
parent f939dcca39
commit 170ac89ba7
4 changed files with 110 additions and 108 deletions

View File

@ -10,7 +10,6 @@ const ENV_DEV = 'development'
export default { export default {
db: {}, db: {},
session: { session: {
enabled: false,
ttl: 3600 * 24 * 7, ttl: 3600 * 24 * 7,
domain: '', // NODESSID域, 默认等于domain domain: '', // NODESSID域, 默认等于domain
level: 0, // 校验级别, 0: 不校验客户端, 2: ua, 4: ip, 6: ua + ip level: 0, // 校验级别, 0: 不校验客户端, 2: ua, 4: ip, 6: ua + ip
@ -21,8 +20,6 @@ export default {
} }
}, },
jwt: { jwt: {
// jwt 开关
enabled: false,
ttl: 3600 * 24 * 7, ttl: 3600 * 24 * 7,
level: 0, // 校验级别, 0: 不校验客户端, 2: ua, 4: ip, 6: ua + ip level: 0, // 校验级别, 0: 不校验客户端, 2: ua, 4: ip, 6: ua + ip
secret: 'it_is_secret_key' // jwt密钥, 使用时请修改 secret: 'it_is_secret_key' // jwt密钥, 使用时请修改
@ -41,7 +38,6 @@ export default {
passwd: '' passwd: ''
}, },
views: { views: {
enabled: false,
dir: '', dir: '',
ext: '.htm' ext: '.htm'
}, },

View File

@ -14,11 +14,11 @@ import Response from '@gm5/response'
// import { sessionPackage, sessionConnect } from '@gm5/session' // import { sessionPackage, sessionConnect } from '@gm5/session'
// import { jwtPackage, jwtConnect } from '@gm5/jwt' // import { jwtPackage, jwtConnect } from '@gm5/jwt'
import { noop, readonlyProp } from './lib.js'
import config from './config/index.js' import config from './config/index.js'
import Routers from './middleware/router.js' import { createRouter } from './middleware/router.js'
import Cors from './middleware/cors.js' import { createCors } from './middleware/cors.js'
process.on('uncaughtException', err => { process.on('uncaughtException', err => {
console.error('UncaughtException: ', err) console.error('UncaughtException: ', err)
@ -27,16 +27,17 @@ process.on('uncaughtException', err => {
class Five { class Five {
#config = config #config = config
#modules = {} #modules = {}
#middlewares = [Cors] #middlewares = [createCors()]
constructor() {
readonlyProp(this, 'state', Object.create(null))
}
#loadBuildIn() { #loadBuildIn() {
let { domain, website, session, jwt } = this.#config // let { domain, website, session, jwt } = this.#config
// domain = domain || website
domain = domain || website // session.domain = session.domain || domain
session.domain = session.domain || domain // this.set({ domain, session })
this.set({ domain, session })
// 将jwt & session中间件提到最前 // 将jwt & session中间件提到最前
// 以便用户自定义的中间件可以直接操作session // 以便用户自定义的中间件可以直接操作session
// if (session.enabled) { // if (session.enabled) {
@ -48,9 +49,8 @@ class Five {
// this.install(jwtPackage) // this.install(jwtPackage)
// this.#middlewares.unshift(jwtConnect) // this.#middlewares.unshift(jwtConnect)
// } // }
// 路由中间件要在最后 // 路由中间件要在最后
this.use(Routers) // this.use(createRouter())
} }
async #loop(req, res, idx = 0) { async #loop(req, res, idx = 0) {
@ -94,7 +94,7 @@ class Five {
// 与别的中间件用法有些不一样, 回调的传入参数中的req和res, // 与别的中间件用法有些不一样, 回调的传入参数中的req和res,
// 并非原生的request对象和response对象, // 并非原生的request对象和response对象,
// 而是框架内部封装过的,可通过origin属性访问原生的对象 // 而是框架内部封装过的,可通过origin属性访问原生的对象
use(fn) { use(fn = noop) {
if (typeof fn === 'function') { if (typeof fn === 'function') {
this.#middlewares.push(fn) this.#middlewares.push(fn)
return this return this
@ -129,7 +129,7 @@ class Five {
.then(r => r.default) .then(r => r.default)
.catch(err => { .catch(err => {
console.error(err) console.error(err)
return { default: null } return null
}) })
}) })
} }
@ -148,12 +148,14 @@ class Five {
} }
run() { run() {
this.#loadBuildIn()
let server = http.createServer() let server = http.createServer()
this.server = server this.server = server
// this.#loadBuildIn()
// 路由中间件要在最后
this.use(createRouter())
server server
.on('request', (req, res) => { .on('request', (req, res) => {
let request = new Request(req, res) let request = new Request(req, res)

View File

@ -4,19 +4,20 @@
* @date 2020/09/18 14:55:49 * @date 2020/09/18 14:55:49
*/ */
import url from 'url' import { parse } from 'node:url'
export default function (req, res, next) { export function createCors() {
var CORS = this.get('cors') return function (req, res, next) {
var opts = this.get('cors')
if (CORS.enabled) { if (opts.enabled) {
var origin = req.header('origin') || req.header('referer') || '' var origin = req.header('origin') || req.header('referer') || ''
var headers = req.header('access-control-request-headers') var headers = req.header('access-control-request-headers')
var { hostname, host, protocol } = url.parse(origin) var { hostname, host, protocol } = parse(origin)
if (CORS.origin.length && hostname) { if (opts.origin.length && hostname) {
var pass = false var pass = false
for (let it of CORS.origin) { for (let it of opts.origin) {
if (hostname.endsWith(it)) { if (hostname.endsWith(it)) {
pass = true pass = true
break break
@ -26,19 +27,19 @@ export default function (req, res, next) {
return res.end('') return res.end('')
} }
} }
if (CORS.credentials) { if (opts.credentials) {
res.set('Access-Control-Allow-Credentials', 'true') res.set('Access-Control-Allow-Credentials', 'true')
} }
res.set('Access-Control-Allow-Origin', `${protocol}//${host}`) res.set('Access-Control-Allow-Origin', `${protocol}//${host}`)
res.set('Access-Control-Allow-Methods', 'GET,HEAD,POST,PUT,DELETE,PATCH') res.set('Access-Control-Allow-Methods', req.method)
if (headers) { if (headers) {
res.set('Access-Control-Allow-Headers', headers) res.set('Access-Control-Allow-Headers', headers)
} }
if (CORS.maxAge) { if (opts.maxAge) {
res.set('Access-Control-Max-Age', CORS.maxAge) res.set('Access-Control-Max-Age', opts.maxAge)
} }
if (req.method === 'OPTIONS') { if (req.method === 'OPTIONS') {
@ -47,3 +48,4 @@ export default function (req, res, next) {
} }
next() next()
} }
}

View File

@ -6,7 +6,8 @@
import { readonlyProp } from '../lib.js' import { readonlyProp } from '../lib.js'
export default function (req, res, next) { export function createRouter() {
return function (req, res, next) {
var debug = this.get('debug') var debug = this.get('debug')
var spa = this.get('spa') var spa = this.get('spa')
@ -71,3 +72,4 @@ export default function (req, res, next) {
res.error(debug ? err.stack || err : err, err.status || 500) res.error(debug ? err.stack || err : err, err.status || 500)
}) })
} }
}