-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.mjs
More file actions
69 lines (60 loc) · 1.96 KB
/
Copy pathindex.mjs
File metadata and controls
69 lines (60 loc) · 1.96 KB
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
import url from 'url'
import path from 'path'
import fs from 'fs'
import express from 'express'
import especial from 'especial'
import { dbpath } from './config.mjs'
import schema from './schema.mjs'
import { SQLiteConnector } from 'anondb/node.js'
import Ceremony from './daemons/ceremony.mjs'
import Backup from './daemons/backup.mjs'
import DeviceAuth from './daemons/deviceFlowAuth.mjs'
const __dirname = path.dirname(url.fileURLToPath(import.meta.url))
// the application db
const db = await SQLiteConnector.create(schema, dbpath('app.db'))
// initialize websocket server
let wsApp, httpApp
{
const app = especial()
const port = process.env.WS_PORT ?? 8001
app.listen(port, () => console.log(`Websocket on port ${port}`))
app.handle('ping', (_, send) => send('pong'))
wsApp = app
}
// initialize http server
{
const app = express()
const port = process.env.HTTP_PORT ?? 8000
app.listen(port, () => console.log(`HTTP on port ${port}`))
app.use('*', (req, res, next) => {
res.set('access-control-allow-origin', '*')
res.set('access-control-allow-headers', '*')
next()
})
app.use(express.json())
app.use('/static', express.static(path.join(__dirname, 'static')))
httpApp = app
}
try {
const backup = new Backup(db)
backup.start()
} catch (err) {
console.log('Error starting backup system, aborting:', err)
}
const deviceAuth = new DeviceAuth(db)
deviceAuth.start()
const state = { app: httpApp, wsApp, db }
const ceremony = new Ceremony(state)
state.ceremony = ceremony
await importFunctionDirectory('ws', state)
await importFunctionDirectory('http', state)
// name relative to file location
async function importFunctionDirectory(dirname, state) {
// import all non-index files from __dirname/name this folder
const routeDir = path.join(__dirname, dirname)
const routes = await fs.promises.readdir(routeDir)
for (const routeFile of routes) {
const { default: route } = await import(path.join(routeDir, routeFile))
route(state)
}
}