-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
73 lines (71 loc) · 1.95 KB
/
app.js
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
'use strict';
/**
* @description import
*/
// import Dependencies
const fastify = require('fastify');
const fastifyAuth = require('@fastify/auth');
// import plugin
const fastifyCorsPlugin = require('./src/plugin/cors');
const fastifyRouterPlugin = require('./src/plugin/router');
const fastifyEnvPlugin = require('./src/plugin/env');
const fastifyLoggerPlugin = require('./src/plugin/logger');
const fastifyJwtPlugin = require('./src/plugin/jwt');
const fastifyCasbinPlugin = require('./src/plugin/casbin');
function build() {
// init app
const app = fastify({
// 使用 logger plugin
logger: fastifyLoggerPlugin,
});
/**
* @description plugin
*/
// plugin
// 取得 .env 中的環境變數
app.register(fastifyEnvPlugin).after((err) => {
if (err) console.log(err);
});
app.register(fastifyCorsPlugin);
// 將 http 類型 log 寫入 info 日誌
app.addHook('onResponse', (request, reply, done) => {
fastifyLoggerPlugin.info({
request: {
method: request.method,
url: request.url,
user_agent: request.headers['user-agent'],
hostname: request.hostname,
remoteAddress: request.ip,
remotePort: request.socket.remotePort,
},
reply: {
statusCode: reply.statusCode,
statusMessage: reply.raw.statusMessage,
},
});
done();
});
// auth
app.register(fastifyAuth);
// jwt
app.register(fastifyJwtPlugin);
// casbin
app.register(fastifyCasbinPlugin);
// router
app.register(fastifyRouterPlugin);
return app;
}
const server = build();
// restful api server
server.listen({ port: process.env.PORT, host: '0.0.0.0' }, function (err) {
console.log(
`Welcome to e_shopping_api app listening at http://${process.env.IP_ADDRESS}:${process.env.PORT}`,
);
if (err) {
// 紀錄全域狀態下的 error 到日誌
fastifyLoggerPlugin.error(err);
console.log(err);
}
});
module.exports.build = build;
module.exports.server = server;