-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (62 loc) · 1.57 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
const app = require("fastify")({ logger: false });
const Ajv = require("ajv");
const { Container } = require("./src/container.js");
//validation configs for fastify requests
const schemaCompilers = {
body: new Ajv({
removeAdditional: true,
coerceTypes: false,
allErrors: false,
}),
params: new Ajv({
removeAdditional: true,
coerceTypes: true,
allErrors: false,
}),
querystring: new Ajv({
removeAdditional: true,
coerceTypes: true,
allErrors: false,
}),
headers: new Ajv({
removeAdditional: true,
coerceTypes: true,
allErrors: false,
}),
};
app.setValidatorCompiler((req) => {
if (!req.httpPart) {
throw new Error("Missing httpPart");
}
const compiler = schemaCompilers[req.httpPart];
if (!compiler) {
throw new Error(`Missing compiler for ${req.httpPart}`);
}
return compiler.compile(req.schema);
});
//Aplying middleware in app request
app.decorateRequest("container", null);
app.addHook("onRequest", async (req, reply) => {
req.container = Container;
});
//Routes
const opts = {
schema: {
response: {
200: {
type: "object",
properties: {
message: { type: "string" },
},
},
},
},
};
app.get("/api/", opts, (req, reply) => {
reply.send({ message: "Let's go!" });
});
app.register(require("./src/interfaces/http/routes/contracts.js"));
app.register(require("./src/interfaces/http/routes/pilots.js"));
app.register(require("./src/interfaces/http/routes/ships.js"));
app.register(require("./src/interfaces/http/routes/reports.js"));
module.exports = app;