refactor: migrate to Tencent Cloud backend
这个提交包含在:
@@ -0,0 +1,34 @@
|
||||
const Router = require('./lib/router')
|
||||
const { createContext } = require('./lib/request')
|
||||
const { ok, fail, http } = require('./lib/response')
|
||||
|
||||
const router = new Router()
|
||||
|
||||
require('./routes/auth')(router)
|
||||
require('./routes/user')(router)
|
||||
require('./routes/device')(router)
|
||||
require('./routes/subscription')(router)
|
||||
require('./routes/treatment')(router)
|
||||
require('./routes/admin')(router)
|
||||
require('./routes/firmware')(router)
|
||||
|
||||
async function handle(event) {
|
||||
const ctx = createContext(event || {})
|
||||
if (ctx.method === 'OPTIONS') return http(204, {})
|
||||
if (ctx.path === '/health') return http(200, ok({ status: 'ok' }))
|
||||
|
||||
const match = router.match(ctx.method, ctx.path)
|
||||
if (!match) return http(404, fail(404, 'not_found'))
|
||||
|
||||
ctx.params = match.params
|
||||
|
||||
try {
|
||||
const body = await match.handler(ctx)
|
||||
return http(200, body)
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return http(500, fail(3001, 'server_error'))
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { handle }
|
||||
在新工单中引用
屏蔽一个用户