-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetup.js
More file actions
124 lines (101 loc) · 3.53 KB
/
Setup.js
File metadata and controls
124 lines (101 loc) · 3.53 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
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
124
import fs from 'fs'
import express from 'express'
import { fileURLToPath, pathToFileURL } from 'url'
import { dirname, join } from 'path'
import BaseController from './src/utils/BaseController.js'
import { logger } from './src/utils/Logger.js'
import { createOpenAPISpec } from './src/utils/openAPI.js'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const CONTROLLERS = []
const HANDLERS = []
let ROUTE_PREFIX = process.env.ROUTE_PREFIX || ''
if (ROUTE_PREFIX && ROUTE_PREFIX[0] != '/') {
ROUTE_PREFIX = '/' + ROUTE_PREFIX
}
export class Paths {
static get Public() {
return join(__dirname, 'www')
}
static get Server() {
return join(__dirname, 'src')
}
static get Controllers() {
return this.Server + '/controllers'
}
static get Handlers() {
return this.Server + '/handlers'
}
}
export async function RegisterControllers(router, subdir = '') {
const directory = subdir ? join(Paths.Controllers, subdir) : Paths.Controllers
const controllers = fs.readdirSync(directory)
await Promise.allSettled(controllers.map(loadController))
logger.info('Controllers Registered', controllers.length)
async function loadController(filename) {
try {
if (!filename.endsWith('.js')) return
const controllerPath = pathToFileURL(join(directory, filename)).href
const fileHandler = await import(controllerPath)
let controllerClass = fileHandler[filename.slice(0, -3)]
if (controllerClass === undefined) {
throw new Error(`Exported class does not match the name of file, ${filename}`)
}
if (fileHandler.default) {
controllerClass = fileHandler.default
}
const controller = new controllerClass()
if (!(controller instanceof BaseController)) {
logger.warn('Skipped Controller since it is not a BaseController', controllerClass.name)
return
}
CONTROLLERS.push(controller)
router.use(controller.mount, controller.router)
} catch (e) {
console.error(
'[CONTROLLER ERROR] Unable to load controller, potential duplication, review mount path and controller class name',
filename,
e
)
}
}
}
export async function RegisterSocketHandlers() {
const directory = Paths.Handlers
const handlers = fs.readdirSync(directory)
handlers.forEach(async (filename) => {
try {
if (!filename.endsWith('.js')) { return }
const handlerPath = pathToFileURL(join(Paths.Handlers, filename)).href
const fileHandler = await import(handlerPath)
let handlerClass = fileHandler[filename.slice(0, -3)]
if (fileHandler.default) {
handlerClass = fileHandler.default
}
HANDLERS.push(handlerClass)
} catch (e) {
logger.error(
'[SOCKET_HANDLER_ERROR] unable to attach socket handler, potential duplication, review mount path and controller class name, and see error below',
filename,
e
)
}
})
}
export async function attachHandlers(io, socket, user, profile) {
if (socket._handlers && user && profile) {
return socket._handlers.forEach(handler => handler.attachUser(user, profile))
}
socket._handlers = HANDLERS.map(Handler => new Handler(io, socket))
}
export function UseStaticPages(router) {
router.use(ROUTE_PREFIX, express.static(Paths.Public))
}
export function generateOpenAPISpec() {
try {
const spec = createOpenAPISpec(CONTROLLERS)
fs.writeFileSync('./swagger.json', JSON.stringify(spec, null, 2));
} catch (e) {
logger.error('Error generating OpenAPI Spec', e)
}
}