-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
133 lines (115 loc) · 3.23 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
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
"use strict";
const path = require('path')
const AutoLoad = require('@fastify/autoload');
const env = require('@fastify/env');
const models = require('./models')
module.exports = async (fastify, opts) => {
fastify.decorate("resource", null);
fastify.decorate("gRPC", require('./gRPC'));
fastify.decorate("schemas", require('./schemas'));
const schema = {
type: 'object',
required: [ 'POSTGRES_DATABASE_NAME', 'POSTGRES_URL', 'POSTGRES_PORT', 'POSTGRES_USER', 'POSTGRES_PASSWORD', 'SECRET', 'API' ],
properties: {
POSTGRES_DATABASE_NAME: {
type: 'string'
},
POSTGRES_URL: {
type: 'string'
},
POSTGRES_PORT: {
type: 'string',
default: '5432'
},
POSTGRES_USER: {
type: 'string'
},
POSTGRES_PASSWORD: {
type: 'string'
},
SECRET: {
type: 'string'
},
API: {
type: 'string'
},
GRPC_SERVER: {
type: 'string',
default: '0.0.0.0'
},
GRPC_PORT: {
type: 'string',
default: '50051'
},
CURIES: {
type: 'string'
}
}
}
const options = {
schema: schema,
data: process.env,
dotenv: true
};
fastify.register(env, options).after((err) => {
if (err) console.error(err);
if (process.env.SERVER === 'docker') {
const newConfig = {
POSTGRES_DATABASE_NAME: process.env.POSTGRES_DATABASE_NAME,
POSTGRES_URL: process.env.POSTGRES_URL,
POSTGRES_PORT: process.env.POSTGRES_PORT,
POSTGRES_USER: process.env.POSTGRES_USER,
POSTGRES_PASSWORD: process.env.POSTGRES_PASSWORD
};
fastify.config = {
...newConfig
}
};
});
/**
*
*/
fastify.addHook('onReady', (done) => {
models.init(fastify.config)
/** start server gRPC */
if (fastify.config.GRPC_PORT !== null && fastify.config.GRPC_PORT !== undefined && parseInt(fastify.config.GRPC_PORT) !== 0) {
fastify.gRPC.server.start(fastify.config);
fastify.log.info(`Server gRPC started on PORT ${fastify.config.GRPC_SERVER}:${fastify.config.GRPC_PORT}`);
if (fastify.gRPC.client !== null && fastify.gRPC.client !== undefined) {
fastify.gRPC.client.waitForReady(Infinity, (err) => {
fastify.log.info(err !== null && err !== undefined ? err : `Client connected to gRPC Server ${fastify.config.GRPC_SERVER}:${fastify.config.GRPC_PORT}`);
})
}
};
done();
});
/**
*
*/
fastify.addHook('onClose', async () => {
/** stop gRPC server */
fastify.log.info(`shutdown gRPC Server ${fastify.config.GRPC_SERVER}:${fastify.config.GRPC_PORT}`);
if (fastify.gRPC.server !== null && fastify.gRPC.server !== undefined) {
fastify.gRPC.client.close();
await fastify.gRPC.server.shutdown();
}
});
/**
* This loads all plugins defined in plugins
*/
fastify.register(AutoLoad, {
dir: path.join(__dirname, "plugins"),
options: Object.assign({}, opts),
});
/**
* This loads all plugins defined in routes
*/
fastify.register(AutoLoad, {
dir: path.join(__dirname, "routes/v1"),
autoHooks: true,
options: {
...opts,
prefix: "/v1",
},
});
};