-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
123 lines (112 loc) · 3.77 KB
/
main.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
import './helpers/processWarnings.js'
import process from 'node:process'
import { resolve } from 'node:path'
import { Log, isDevEnv } from 'cmd430-utils'
import { customAlphabet } from 'nanoid'
import Fastify from 'fastify'
import serveStatic from '@fastify/static'
import multipart from '@fastify/multipart'
import formbody from '@fastify/formbody'
import cookie from '@fastify/cookie'
import session from '@fastify/session'
import cfTurnstile from 'fastify-cloudflare-turnstile'
import { evaluate } from 'mathjs'
import SqliteStore from 'fastify-session-better-sqlite3-store'
import view from '@fastify/view'
import totp from 'fastify-totp'
import websocket from '@fastify/websocket'
import handlebars from 'handlebars'
import { config } from './config/config.js'
import { sessions } from './sessions/sessions.js'
import temporaryUploadsGC from './plugins/temporaryUploadsGC.js'
import fastifyLoadHooks from './plugins/fastifyLoadHooks.js'
import loadRoutes from './plugins/loadRoutes.js'
import disableCache from './plugins/disableCache.js'
import errorPage from './plugins/errorPage.js'
import fastifyAbort from './plugins/fastifyAbort.js'
import fastifyLogger from './helpers/fastifyLogger.js'
import fastifyLoadPartials from './helpers/fastifyLoadPartials.js'
import { getDatabaseInterface } from './interfaces/database.js'
import { getStorageInterface } from './interfaces/storage.js'
import './helpers/handlebarsHelpers.js'
// eslint-disable-next-line no-unused-vars
const { log, debug, info, warn, error } = new Log('Main')
const nanoid = customAlphabet('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz')
const DataStore = await getDatabaseInterface(config.database.store)
const FileStore = await getStorageInterface(config.storage.store)
try {
const fastify = Fastify({
logger: fastifyLogger,
trustProxy: true,
ignoreTrailingSlash: true,
genReqId: () => nanoid(4)
})
// Make config accessable as fastify.config
fastify.decorate('config', config)
// Make database data accessable as fastify.db
fastify.decorate('db', new DataStore())
// Make storage accessable as fastify.storage
fastify.decorate('storage', new FileStore())
// Fastify Plugins
fastify.register(cookie)
fastify.register(session, {
store: new SqliteStore(sessions),
secret: config.session.secret,
cookieName: config.session.cookieName,
cookie: {
maxAge: config.session.maxAge,
secure: isDevEnv() ? 'auto' : true,
httpOnly: true,
sameSite: 'strict'
},
saveUninitialized: false,
rolling: true
})
fastify.register(serveStatic, {
root: resolve('./public')
})
fastify.register(multipart, {
limits: {
fileSize: evaluate(config.uploads.limits.fileSize), // Max file size in bytes
files: config.uploads.limits.files // Max number of file uploads in one go
}
})
fastify.register(formbody)
fastify.register(cfTurnstile,{
sitekey: config.captcha.siteKey,
privatekey: config.captcha.secretKey
})
fastify.register(view, {
engine: {
handlebars: handlebars
},
root: resolve('./views'),
viewExt: 'hbs',
defaultContext: {
env: isDevEnv() ? 'dev' : 'prod'
},
options: {
partials: await fastifyLoadPartials(),
useDataVariables: true
}
})
fastify.register(websocket)
fastify.register(totp)
fastify.register(disableCache)
fastify.register(errorPage)
fastify.register(fastifyAbort)
// Hooks
fastify.register(await fastifyLoadHooks)
// Register routes
fastify.register(await loadRoutes)
// Setup Temp file removing and session clean up tasks
fastify.register(temporaryUploadsGC)
await fastify.db.connect()
await fastify.listen({
port: config.fastify.port,
host: config.fastify.bind
})
} catch (err) {
error(err.stack)
process.exit(1)
}