-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
95 lines (80 loc) · 3.05 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
import * as path from 'path';
import { cwd } from 'node:process';
import Fastify from 'fastify';
import fastifyStatic from '@fastify/static';
import { staticPublic } from './plugins/static-public.js';
//import { staticTreatmentsArchives } from './plugins/static-treatments-archives.js';
import fastifyBetterSqlite3 from '@punkish/fastify-better-sqlite3';
import { initDb } from './lib/dbconn.js';
import routes from '@fastify/routes';
import favicon from 'fastify-favicon';
import cors from '@fastify/cors';
import fastifySwagger from '@fastify/swagger';
import fastifySwaggerUi from '@fastify/swagger-ui'
import { swaggerOpts } from './plugins/swagger.js';
import fastifyCron from 'fastify-cron';
import { cronJobs } from './plugins/cron.js';
//import fastifyQueries from './plugins/queries.js';
import { docs } from './routes/docs/index.js';
import { tos } from './routes/tos/index.js';
import { install } from './routes/install/index.js';
import { queryHelp } from './routes/query-help/index.js';
import { roadmap } from './routes/roadmap/index.js';
import { about } from './routes/about/index.js';
import { treatmentsArchive } from './routes/treatments-archive/index.js';
import { bins } from './routes/bins/index.js';
// we rename routes to resources because we have already imported a map of the
// routes above
//
import { routes as resources } from './routes/api/index.js';
import view from '@fastify/view';
import { viewOpts } from './plugins/view.js';
export async function server(opts={}) {
const fastify = Fastify(opts);
// register the plugins
//
fastify.register(favicon, {});
fastify.register(cors, {});
fastify.register(routes, {});
await fastify.register(fastifySwagger, swaggerOpts.swagger);
await fastify.register(fastifySwaggerUi, swaggerOpts.swaggerUi);
fastify.register(fastifyStatic, staticPublic);
//fastify.register(fastifyStatic, staticTreatmentsArchives);
fastify.register(view, viewOpts);
const jobs = cronJobs.map(({cronTime, qs}) => {
return {
cronTime,
onTick: async (server) => {
try {
await server.inject(qs);
}
catch(error) {
console.error(error);
}
},
start: true
}
});
fastify.register(fastifyCron, { jobs });
// we initialize the db connection once, and store it in a fastify
// plugin so it can be used everywhere
//
const db = initDb();
const fastifyBetterSqlite3Opts = {
"class": db.class,
"connection": db.conn
};
fastify.register(fastifyBetterSqlite3, fastifyBetterSqlite3Opts);
// register the routes to resources
//
fastify.register(docs);
fastify.register(tos);
fastify.register(install);
fastify.register(queryHelp);
fastify.register(roadmap);
fastify.register(about);
fastify.register(treatmentsArchive);
fastify.register(bins, { prefix: 'v3' });
resources.forEach(resource => fastify.register(resource, { prefix: 'v3' }));
return fastify;
}