mirror of
https://github.com/ragestudio/linebridge.git
synced 2025-06-09 18:44:17 +00:00
added headers & origin resolver
This commit is contained in:
parent
b0a5116efb
commit
6f12edfb69
@ -12,6 +12,8 @@
|
|||||||
],
|
],
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"uuid": "^8.3.2",
|
||||||
|
"corenode": "^0.23.5",
|
||||||
"axios": "^0.21.1",
|
"axios": "^0.21.1",
|
||||||
"websocket": "^1.0.34",
|
"websocket": "^1.0.34",
|
||||||
"express": "^4.17.1"
|
"express": "^4.17.1"
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
const axios = require("axios")
|
const axios = require("axios")
|
||||||
|
const express = require("express")
|
||||||
|
const { objectToArrayMap } = require("@corenode/utils")
|
||||||
|
const uuid = require("uuid")
|
||||||
|
|
||||||
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()
|
||||||
class Controller {
|
class Controller {
|
||||||
constructor(key, exec, params) {
|
constructor(key, exec, params) {
|
||||||
this.params = params
|
this.params = params
|
||||||
@ -20,18 +23,30 @@ class Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Server {
|
class RequestServer {
|
||||||
constructor(params) {
|
constructor(params) {
|
||||||
this.params = params ?? {}
|
this.params = params ?? {}
|
||||||
|
|
||||||
this.endpoints = {}
|
this.endpoints = {}
|
||||||
this.endpointsAddress = []
|
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._everyRequest = null
|
||||||
this._onRequest = {}
|
this._onRequest = {}
|
||||||
|
|
||||||
this.httpServer = require("express")()
|
this.httpServer = require("express")()
|
||||||
|
|
||||||
|
if (typeof this.params.port === "undefined") {
|
||||||
|
this.params.port = 3010
|
||||||
|
}
|
||||||
|
|
||||||
if (this.params.autoInit) {
|
if (this.params.autoInit) {
|
||||||
this.init()
|
this.init()
|
||||||
}
|
}
|
||||||
@ -53,7 +68,11 @@ class Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
registerEndpoint(method, route, controller) {
|
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.endpoints[route] = { method: method, route: route, controller: controller }
|
||||||
this.endpointsAddress.push(this.endpoints[route])
|
this.endpointsAddress.push(this.endpoints[route])
|
||||||
|
|
||||||
@ -78,19 +97,41 @@ class Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
init() {
|
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
|
// todo: read endponts.json and itterate
|
||||||
|
|
||||||
this.httpServer.listen(3010, () => {
|
// register root resolver
|
||||||
console.log(`Ready!`)
|
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
|
// create default server
|
||||||
const defServer = new Server({ autoInit: true })
|
const defServer = new RequestServer({ autoInit: true })
|
||||||
|
defServer.onRequest()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -2992,6 +2992,11 @@ utils-merge@1.0.1:
|
|||||||
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713"
|
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713"
|
||||||
integrity sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=
|
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:
|
value-or-function@^3.0.0:
|
||||||
version "3.0.0"
|
version "3.0.0"
|
||||||
resolved "https://registry.yarnpkg.com/value-or-function/-/value-or-function-3.0.0.tgz#1c243a50b595c1be54a754bfece8563b9ff8d813"
|
resolved "https://registry.yarnpkg.com/value-or-function/-/value-or-function-3.0.0.tgz#1c243a50b595c1be54a754bfece8563b9ff8d813"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user