added headers & origin resolver

This commit is contained in:
srgooglo 2021-05-31 14:13:41 +02:00
parent b0a5116efb
commit 6f12edfb69
3 changed files with 59 additions and 11 deletions

View File

@ -12,6 +12,8 @@
],
"license": "MIT",
"dependencies": {
"uuid": "^8.3.2",
"corenode": "^0.23.5",
"axios": "^0.21.1",
"websocket": "^1.0.34",
"express": "^4.17.1"

View File

@ -1,10 +1,13 @@
const axios = require("axios")
const express = require("express")
const { objectToArrayMap } = require("@corenode/utils")
const uuid = require("uuid")
const http = require("http")
const wsServer = require('websocket').server
const wsFrame = require('websocket').frame
const SERVER_VERSION = runtime.helpers.getVersion()
class Controller {
constructor(key, exec, params) {
this.params = params
@ -13,25 +16,37 @@ class Controller {
this.exec = exec
}
}
exec(req, res) {
res.send(`Im alive!`)
console.log(`This is an default controller function`)
}
}
class Server {
class RequestServer {
constructor(params) {
this.params = params ?? {}
this.endpoints = {}
this.endpointsAddress = []
this.routes = []
this.headers = {
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept, Authorization",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS, PUT, PATCH, DELETE",
"Access-Control-Allow-Credentials": "true"
}
this._everyRequest = null
this._onRequest = {}
this.httpServer = require("express")()
if (typeof this.params.port === "undefined") {
this.params.port = 3010
}
if (this.params.autoInit) {
this.init()
}
@ -42,7 +57,7 @@ class Server {
this._everyRequest = context
}
}
onRequest = (key, context) => {
if (typeof key === "undefined") {
return false
@ -53,10 +68,14 @@ class Server {
}
registerEndpoint(method, route, controller) {
route = `/${route}`
if (typeof controller === "function") {
controller = new Controller(route, controller)
}
this.routes.push(route)
this.endpoints[route] = { method: method, route: route, controller: controller }
this.endpointsAddress.push(this.endpoints[route])
this.httpServer[method](route, (req, res) => this.httpRequest(req, res, this.endpoints[route]))
}
@ -78,19 +97,41 @@ class Server {
}
init() {
this.httpServer.use(express.json())
this.httpServer.use(express.urlencoded({ extended: true }))
this.httpServer.use((req, res, next) => {
objectToArrayMap(this.headers).forEach((entry) => {
res.setHeader(entry.key, entry.value)
})
next()
})
// todo: read endponts.json and itterate
this.httpServer.listen(3010, () => {
console.log(`Ready!`)
// register root resolver
this.registerEndpoint("get", "/", (req, res) => {
// here server origin resolver
res.json({
uuid: uuid.v4(),
originID: this.params.oid ?? "RelicServer",
version: SERVER_VERSION,
routes: this.routes
})
})
this.httpServer.listen(this.params.port, () => {
console.log(`Ready on port ${this.params.port}!`)
})
}
}
export { Controller, Server }
module.exports = { Controller, Server: RequestServer }
// create default server
const defServer = new Server({ autoInit: true })
const defServer = new RequestServer({ autoInit: true })
defServer.onRequest()

View File

@ -2992,6 +2992,11 @@ utils-merge@1.0.1:
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713"
integrity sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=
uuid@^8.3.2:
version "8.3.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==
value-or-function@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/value-or-function/-/value-or-function-3.0.0.tgz#1c243a50b595c1be54a754bfece8563b9ff8d813"