-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ts
62 lines (56 loc) · 1.19 KB
/
config.ts
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
import Ajv from 'ajv';
import fastifyPlugin from 'fastify-plugin';
import { readFileSync } from 'fs';
import { join, resolve } from 'path';
import { parse } from 'toml';
import * as process from 'process';
const ajv = new Ajv();
interface ConfigType {
server: {
close_delay: number;
host: string;
port: number;
};
}
const validate = ajv.compile({
type: 'object',
required: ['server'],
properties: {
server: {
type: 'object',
properties: {
close_delay: {
type: 'number',
default: 500,
minimum: 0,
},
host: {
type: 'string',
default: 'localhost',
},
port: {
type: 'number',
default: 3000,
minimum: 0,
maximum: 65535,
},
},
},
},
});
export default fastifyPlugin(async (fastify) => {
const config = parse(
readFileSync(resolve(join(process.cwd(), 'config.toml'))).toString(),
);
if (validate(config)) {
fastify.config = config as ConfigType;
} else {
throw validate.errors;
}
});
declare module 'fastify' {
// noinspection JSUnusedGlobalSymbols
interface FastifyInstance {
config: ConfigType;
}
}