-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prod.ts
98 lines (85 loc) · 2.55 KB
/
prod.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
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
import { fromEnv } from '@nordicsemiconductor/from-env'
import EventEmitter from 'events'
import * as fs from 'fs'
import { createServer } from 'http'
import path from 'path/posix'
import { URL } from 'url'
import { jsonFileStore } from '../storage/file.js'
import { backend } from './feat/backend.js'
import { setUp as setUpEmails } from './feat/emails.js'
import { startpage } from './feat/startpage.js'
const { originString, cleverCloudFsBucket, appHome, endpointString } = fromEnv({
appHome: 'APP_HOME',
originString: 'ORIGIN',
endpointString: 'ENDPOINT',
cleverCloudFsBucket: 'CC_FS_BUCKET',
})(process.env)
const storageBaseDir = `${appHome}/${cleverCloudFsBucket.split(':')[0]}`
const submissionsDir = path.join(storageBaseDir, 'submission')
const correctionsDir = path.join(storageBaseDir, 'corrections')
const formsDir = path.join(storageBaseDir, 'forms')
for (const d of [formsDir, submissionsDir, correctionsDir]) {
try {
fs.statSync(d)
} catch {
fs.mkdirSync(d, { recursive: true })
}
}
const version = process.env.COMMIT_ID ?? '0.0.0-development'
console.debug(`Launching version ${version}`)
const omnibus = new EventEmitter()
let origin: URL
try {
origin = new URL(originString)
} catch (err) {
console.error(
`Must set ORIGIN, current value is not a URL: "${process.env.ORIGIN}": ${
(err as Error).message
}!`,
)
process.exit(1)
}
let endpoint: URL
try {
endpoint = new URL(endpointString)
} catch (err) {
console.error(
`Must set ENDPOINT, current value is not a URL: "${
process.env.ENDPOINT
}": ${(err as Error).message}!`,
)
process.exit(1)
}
const port = parseInt(process.env.PORT ?? '8080', 10)
const adminEmails = (process.env.ADMIN_EMAILS ?? '')
.split(',')
.filter((e) => e.length > 0)
const app = backend({
omnibus,
origin,
endpoint,
version,
cookieSecret: process.env.COOKIE_SECRET,
cookieLifetimeSeconds:
process.env.COOKIE_LIFETIME_SECONDS !== undefined
? parseInt(process.env.COOKIE_LIFETIME_SECONDS, 10)
: undefined,
adminEmails,
formStorage: jsonFileStore({ directory: formsDir }),
submissionStorage: jsonFileStore({
directory: submissionsDir,
}),
correctionStorage: jsonFileStore({
directory: correctionsDir,
}),
})
startpage(app, endpoint, version)
const httpServer = createServer(app)
httpServer.listen(port, '0.0.0.0', (): void => {
console.debug(`ℹ️ Listening on port:`, port)
console.debug(`ℹ️ Endpoint:`, endpoint.toString())
console.debug(`ℹ️ Origin: `, origin.toString())
console.debug(`ℹ️ Storage: `, storageBaseDir)
})
// Configure email sending
setUpEmails(omnibus, adminEmails)