-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
103 lines (87 loc) · 3.35 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
96
97
98
99
100
101
102
103
import express from 'express'
import fs from 'fs'
const app = express()
import bodyParser from 'body-parser'
import fileUpload from 'express-fileupload'
import cors from 'cors'
import { v4 as uuid } from 'uuid';
import dotenv from 'dotenv'
dotenv.config()
import * as auth from "./modules/authenticator.mjs"
import * as store from "./modules/store.mjs"
import * as crypto from "./modules/crypto.mjs"
import log from "./modules/log.mjs"
import {reqLog} from './modules/log.mjs'
app.use(fileUpload({
limits: {fileSize: 1024 * 1024 * 1024},
useTempFiles: true,
tempFileDir: "/tmp/"
}
))
app.use(cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(reqLog())
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = `${dirname(__filename)}/`;
const port = process.env.PI_PORT || 3000
class Server {
constructor() {
this.schema = 2
this.id = uuid()
this.default_group = null
}
async initialize() {
this.keys = await crypto.getKeypair(this.id)
}
}
async function createEndpoint(endpoint) {
let ep = await import(`./modules/api/${endpoint.path}.mjs`)
app[endpoint.type](endpoint.point, async (req, res) => {
if (endpoint.auth) {
if (!await auth.allowed(req)) {
res.status(403).send("Authentication Failed")
return
}
}
let start = new Date()
let response = await ep.default(req)
res.set("Processing-Time", new Date() - start)
res.status(response.code)
if (response.headers) for (let header of Object.keys(response.headers)) res.header(header, response.headers[header])
if (response.type) res.type(response.type)
if (response.path) res.sendFile(__dirname + response.path)
else res.send(response.data)
})
log("MAIN", `Created ${endpoint.type.toUpperCase()} mapping ${endpoint.point} --> ${endpoint.path}`)
}
app.listen(port, async () => {
let start = new Date()
log("MAIN", "Initializing storage")
await store.initialize()
let server = await new Promise((resolve) => {
store.readJSON("data/server.json").then((res) => resolve(res)).catch(async (err) => {
log("MAIN", "No server configuration", 2)
let server = new Server()
await server.initialize()
log("MAIN", "Creating default group")
let gp = await import("./modules/api/new/general.mjs") // a bit hacky, maybe find a better solution
let default_group = await gp.default({
"params": {"type":"group"},
"body":{"name":"Default"}
});
server.default_group = default_group.data
await store.writeJSON("data/server.json", server);
resolve(server);
})
});
log("MAIN", "Initializing components")
await crypto.initialize()
await auth.initialize()
log("MAIN", "Creating endpoints")
let endpoints = JSON.parse(await fs.promises.readFile("./endpoints.json", "utf-8"))
for (let endpoint of endpoints) await createEndpoint(endpoint)
log("MAIN", `Initialized in ${new Date()-start} ms`)
});