controllers loader

This commit is contained in:
srgooglo 2021-06-03 17:30:26 +02:00
parent 6f12edfb69
commit fb7391e196

View File

@ -3,11 +3,33 @@ const express = require("express")
const { objectToArrayMap } = require("@corenode/utils") const { objectToArrayMap } = require("@corenode/utils")
const uuid = require("uuid") const uuid = require("uuid")
const fs = require("fs")
const path = require("path")
const http = require("http") const http = require("http")
const wsServer = require('websocket').server const wsServer = require('websocket').server
const wsFrame = require('websocket').frame const wsFrame = require('websocket').frame
const SERVER_VERSION = runtime.helpers.getVersion() const SERVER_VERSION = runtime.helpers.getVersion()
function fetchController(key) {
try {
const controllersPath = global.controllersPath ?? path.resolve(process.cwd(), `controllers`)
const controllerPath = path.join(controllersPath, key)
if (fs.existsSync(controllerPath)) {
import(controllerPath)
.then((controller) => {
return controller
})
}
} catch (error) {
runtime.logger.dump(error)
console.error(`Failed to load controller [${key}] > ${error.message}`)
}
}
class Controller { class Controller {
constructor(key, exec, params) { constructor(key, exec, params) {
this.params = params this.params = params
@ -19,19 +41,20 @@ class Controller {
exec(req, res) { exec(req, res) {
res.send(`Im alive!`) res.send(`Im alive!`)
console.log(`This is an default controller function`)
} }
} }
class RequestServer { class RequestServer {
constructor(params) { constructor(params, endpoints) {
this.usid = uuid.v4() // unique session identifier
this.params = params ?? {} this.params = params ?? {}
this.endpoints = {} this.endpoints = { ...endpoints }
this.endpointsAddress = [] this.endpointsAddress = []
this.routes = [] this.routes = []
this.headers = { this.headers = {
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept, Authorization", "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept, Authorization",
"Access-Control-Allow-Origin": "*", "Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS, PUT, PATCH, DELETE", "Access-Control-Allow-Methods": "GET, POST, OPTIONS, PUT, PATCH, DELETE",
@ -96,7 +119,21 @@ class RequestServer {
} }
} }
getLocalEndpoints = () => {
try {
const localEndpointsFile = path.resolve(process.cwd(), `endpoints.json`)
if (fs.existsSync(localEndpointsFile)) {
return JSON.parse(fs.readFileSync(localEndpointsFile, 'utf-8'))
}
return false
} catch (error) {
return false
}
}
init() { init() {
const localEndpoints = this.getLocalEndpoints()
this.httpServer.use(express.json()) this.httpServer.use(express.json())
this.httpServer.use(express.urlencoded({ extended: true })) this.httpServer.use(express.urlencoded({ extended: true }))
@ -108,15 +145,31 @@ class RequestServer {
next() next()
}) })
// todo: read endponts.json and itterate if (localEndpoints && Array.isArray(localEndpoints)) {
localEndpoints.forEach((endpoint) => {
try {
const { method, route, controller } = endpoint
fetchController(controller)
this.registerEndpoint(method, route, controller)
} catch (error) {
}
})
}
// register root resolver // register root resolver
this.registerEndpoint("get", "/", (req, res) => { this.registerEndpoint("get", "/", (req, res) => {
// here server origin resolver // here server origin resolver
res.json({ res.json({
uuid: uuid.v4(), usid: this.usid,
originID: this.params.oid ?? "RelicServer", originID: this.params.oid ?? "RelicServer",
version: SERVER_VERSION, version: SERVER_VERSION
})
})
this.registerEndpoint("get", "/map", (req, res) => {
res.json({
routes: this.routes routes: this.routes
}) })
}) })