forked from labring/laf
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
50 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,53 @@ | ||
import * as express from 'express' | ||
import { parseToken } from './lib/utils/token' | ||
import { parseToken, splitBearerToken } from './lib/utils/token' | ||
import { v4 as uuidv4 } from 'uuid' | ||
import Config from './config' | ||
import { router } from './router/index' | ||
import { Globals } from './lib/globals' | ||
import { deployFunctions } from './api/function' | ||
import { deployTriggers } from './api/trigger' | ||
|
||
const logger = Globals.logger | ||
const server = express() | ||
server.use(express.json()) | ||
|
||
// 服务端开放跨域 | ||
server.all('*', function (req, res, next) { | ||
res.header('Access-Control-Allow-Origin', '*') | ||
res.header('Access-Control-Allow-Headers', 'Authorization, Content-Type') | ||
res.header('Access-Control-Allow-Methods', '*') | ||
req['requestId'] = uuidv4() | ||
next() | ||
}) | ||
|
||
// 解析 Bearer Token | ||
server.use(function (req, _res, next) { | ||
const bearer = req.headers['authorization'] ?? '' | ||
const splitted = bearer.split(' ') | ||
const token = splitted.length === 2 ? splitted[1] : '' | ||
const token = splitBearerToken(req.headers['authorization'] ?? '') | ||
const auth = parseToken(token) || null | ||
req['auth'] = auth | ||
|
||
const requestId = req['requestId'] || uuidv4() | ||
const requestId = req['requestId'] = uuidv4() | ||
logger.info(`[${requestId}] ${req.path} start request`) | ||
logger.debug(`[${requestId}] auth: ` + JSON.stringify(auth)) | ||
next() | ||
}) | ||
|
||
server.use(router) | ||
|
||
server.listen(Config.PORT, () => console.log(`listened on ${Config.PORT}`)) | ||
server.listen(Config.PORT, () => console.log(`listened on ${Config.PORT}`)) | ||
|
||
|
||
|
||
|
||
/** | ||
* 监听云函数、触发器变化 & 部署 | ||
*/ | ||
{ | ||
const acc = Globals.sys_accessor | ||
acc.ready.then(() => { | ||
acc.db | ||
.watch([], { fullDocument: 'updateLookup' }) | ||
.on('change', doc => { | ||
const coll = doc.ns.coll | ||
if(coll === '__functions') { | ||
deployFunctions() | ||
} | ||
|
||
if(coll === '__triggers') { | ||
deployTriggers() | ||
} | ||
}) | ||
}) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters