-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ts
55 lines (49 loc) · 1.03 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
import Ajv from 'ajv';
import { readFileSync } from 'fs';
import fastifyPlugin from 'fastify-plugin';
import { join, resolve } from 'path';
import { parse } from 'toml';
const ajv = new Ajv();
type ConfigType = {
server: {
host: string;
port: number;
};
};
const validate = ajv.compile({
type: 'object',
required: ['server'],
properties: {
server: {
type: 'object',
properties: {
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;
}
}