Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
309 lines (263 loc) · 8.33 KB
/
Copy pathserver.js
File metadata and controls
309 lines (263 loc) · 8.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/**
* api-framework core server
* Express + module auto scanner + plugin lifecycle
*/
'use strict'
const fs = require('fs')
const path = require('path')
const express = require('express')
const fileUpload = require('express-fileupload')
const config = require('./config')
const logger = require('./util/logger')
const { cookieToJson } = require('./util/helpers')
const { cacheMiddleware } = require('./util/cache')
// ---- plugin system ----
/**
* Load plugins
* @param {string} pluginsDir
* @returns {object[]}
*/
function loadPlugins(pluginsDir) {
const plugins = []
if (!fs.existsSync(pluginsDir)) return plugins
const files = fs.readdirSync(pluginsDir)
files
.filter((f) => f.endsWith('.js') && !f.startsWith('_'))
.forEach((file) => {
try {
const pluginPath = path.join(pluginsDir, file)
const plugin = require(pluginPath)
const name = file.replace(/\.js$/, '')
plugins.push({ name, ...plugin })
logger.debug(`Loaded plugin: ${name}`)
} catch (err) {
logger.error(`Failed to load plugin: ${file}`, err.message)
}
})
return plugins
}
/**
* Run plugin lifecycle hooks
*/
async function runPluginHook(plugins, hook, context = {}) {
for (const plugin of plugins) {
if (typeof plugin[hook] === 'function') {
try {
await plugin[hook](context)
} catch (err) {
logger.error(`Plugin ${plugin.name} hook ${hook} failed:`, err.message)
}
}
}
}
// ---- module scanner ----
/**
* Scan and load modules
* @param {string} modulesPath
* @param {object} [routeOverrides]
* @returns {Array<{identifier: string, route: string, module: any}>}
*/
function scanModules(modulesPath, routeOverrides = {}) {
const modules = []
if (!fs.existsSync(modulesPath)) {
logger.warn(`Module directory not found: ${modulesPath}`)
return modules
}
const files = fs.readdirSync(modulesPath)
files
.filter((f) => f.endsWith('.js') && !f.startsWith('_'))
.forEach((file) => {
try {
const filePath = path.join(modulesPath, file)
const identifier = file.replace(/\.js$/, '')
const mod = require(filePath)
// detect export format
let route, handler, method
if (typeof mod === 'function') {
// function style: module.exports = async (ctx, core) => {}
route = routeOverrides[file] || `/${identifier.replace(/_/g, '/')}`
handler = mod
method = 'all'
} else if (mod && typeof mod === 'object') {
// object style: module.exports = { route, method, handler }
route = mod.route || routeOverrides[file] || `/${identifier.replace(/_/g, '/')}`
handler = mod.handler
method = (mod.method || 'all').toLowerCase()
} else {
logger.warn(`Skipped invalid module: ${file}`)
return
}
if (typeof handler !== 'function') {
logger.warn(`Module ${file} missing valid handler`)
return
}
modules.push({
identifier,
route,
module: mod,
handler,
method,
})
logger.debug(`Scanned module: ${identifier} -> ${method.toUpperCase()} ${route}`)
} catch (err) {
logger.error(`Failed to load module: ${file}`, err.message)
}
})
return modules
}
// ---- core app factory ----
/**
* Create Express application
* @param {object} [options]
* @param {Array} [options.moduleDefs] custom module definitions (overrides auto-scan)
* @param {Array} [options.plugins] custom plugin list
* @returns {Promise<import('express').Express>}
*/
async function createApp(options = {}) {
const app = express()
// load plugins
const plugins = options.plugins || loadPlugins(config.pluginsDir)
// ---- beforeServer hook ----
await runPluginHook(plugins, 'beforeServer', { app, config })
// ---- middleware ----
// static files
app.use(express.static(config.publicDir))
// CORS
app.use((req, res, next) => {
const allowOrigin = config.corsAllowOrigin === '*'
? '*'
: (req.headers.origin && config.corsAllowOrigin.split(',').map(s => s.trim()).includes(req.headers.origin)
? req.headers.origin
: '*')
res.set({
'Access-Control-Allow-Origin': allowOrigin,
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Headers': 'Content-Type, Authorization, X-Requested-With',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
'Content-Type': 'application/json; charset=utf-8',
})
if (req.method === 'OPTIONS') {
res.status(204).end()
return
}
next()
})
// Cookie parser
app.use((req, _, next) => {
req.cookies = cookieToJson(req.headers.cookie || '')
next()
})
// Body parser
const uploadLimit = `${config.maxUploadSize}mb`
app.use(express.json({ limit: uploadLimit }))
app.use(express.urlencoded({ extended: false, limit: uploadLimit }))
app.use(
fileUpload({
limits: { fileSize: config.maxUploadSize * 1024 * 1024 },
useTempFiles: true,
tempFileDir: path.join(require('os').tmpdir(), 'api-framework-uploads'),
abortOnLimit: true,
parseNested: true,
}),
)
// cache middleware
if (config.enableCache) {
app.use(cacheMiddleware(config.cacheTTL))
}
// ---- scan and register module routes ----
const moduleDefs = options.moduleDefs || scanModules(config.moduleDir)
for (const modDef of moduleDefs) {
const { route, handler, method } = modDef
app[method](route, async (req, res) => {
// ---- beforeRequest hook ----
const hookContext = { req, res, module: modDef }
await runPluginHook(plugins, 'beforeRequest', hookContext)
// build ctx
const ctx = {
query: { ...req.query, ...req.body, ...req.files },
body: req.body || {},
cookies: req.cookies,
files: req.files || {},
headers: req.headers,
method: req.method,
path: req.path,
ip: req.ip,
}
// build core
const request = require('./util/request')
const cache = require('./util/cache')
const core = {
request,
logger,
config,
cache: new cache.MemoryCache(config.cacheTTL),
req,
res,
}
try {
let result
if (typeof modDef.module === 'function') {
// function style: handler(ctx, core)
result = await handler(ctx, core)
} else if (modDef.module && typeof modDef.module === 'object') {
// object style: handler is modDef.handler
result = await handler(ctx, core)
}
// ---- afterRequest hook ----
await runPluginHook(plugins, 'afterRequest', { ...hookContext, result })
// handle response
if (!result) {
res.status(404).json({ code: 404, msg: 'Not Found' })
return
}
// support redirect
if (result.redirectUrl) {
res.redirect(result.status || 302, result.redirectUrl)
return
}
// set Cookie
if (result.cookie && !ctx.query.noCookie) {
const cookies = Array.isArray(result.cookie) ? result.cookie : [result.cookie]
cookies.forEach((c) => {
if (c) res.append('Set-Cookie', c)
})
}
// send response
const statusCode = result.status || 200
res.status(statusCode).json(result.body !== undefined ? result.body : result)
} catch (err) {
logger.error(`[${modDef.identifier}] handler failed:`, err.message)
// ---- onError hook ----
await runPluginHook(plugins, 'onError', { ...hookContext, error: err })
res.status(500).json({
code: 500,
msg: err.message || 'Internal Server Error',
})
}
})
// logger.info(`Registered route: ${method.toUpperCase()} ${route}`)
}
// ---- afterRoutes hook ----
await runPluginHook(plugins, 'afterRoutes', { app, config })
return app
}
/**
* Start HTTP server
* @param {import('express').Express} app
* @param {number} port
* @param {string} host
* @returns {Promise<import('http').Server>}
*/
function startServer(app, port, host) {
return new Promise((resolve) => {
const server = app.listen(port, host, () => {
resolve(server)
})
})
}
module.exports = {
createApp,
startServer,
scanModules,
loadPlugins,
}