reimplement http

This commit is contained in:
srgooglo 2022-08-04 16:54:39 +02:00
parent 53c112d7c4
commit e102d82037
2 changed files with 35 additions and 15 deletions

View File

@ -23,6 +23,8 @@ global.DEFAULT_HEADERS = {
global.DEFAULT_SERVER_PARAMS = { global.DEFAULT_SERVER_PARAMS = {
urlencoded: true, urlencoded: true,
httpEngine: "express", httpEngine: "express",
protocol: "http",
wsProtocol: "ws",
} }
global.DEFAULT_MIDDLEWARES = [ global.DEFAULT_MIDDLEWARES = [

View File

@ -1,5 +1,8 @@
const fs = require("fs") const fs = require("fs")
const http = require("http")
const https = require("https")
const io = require("socket.io") const io = require("socket.io")
const tokenizer = require("corenode/libs/tokenizer") const tokenizer = require("corenode/libs/tokenizer")
@ -10,6 +13,11 @@ const InternalConsole = global.InternalInternalConsole = internalConsole
const builtInMiddlewares = [] const builtInMiddlewares = []
const HTTPProtocolsInstances = {
http: http,
https: https,
}
const HTTPEngines = { const HTTPEngines = {
"hyper-express": () => { "hyper-express": () => {
InternalConsole.warn("Hyper-Express is not fully supported yet") InternalConsole.warn("Hyper-Express is not fully supported yet")
@ -67,17 +75,26 @@ class Server {
} }
this.endpointsMap = {} this.endpointsMap = {}
this.WSListenPort = this.params.wsPort ?? 3020 this.listenPort = this.params.port ?? 3010
this.HTTPlistenPort = this.params.port ?? 3010
// TODO: Handle HTTPS and WSS // TODO: Handle HTTPS and WSS
this.HTTPAddress = `http://${global.LOCALHOST_ADDRESS}:${this.HTTPlistenPort}` this.HTTPAddress = `${this.params.protocol}://${global.LOCALHOST_ADDRESS}:${this.listenPort}`
this.WSAddress = `ws://${global.LOCALHOST_ADDRESS}:${this.WSListenPort}` this.WSAddress = `${this.params.wsProtocol}://${global.LOCALHOST_ADDRESS}:${this.listenPort}`
//* set server basics //* set server basics
this.httpInterface = global.httpInterface = HTTPEngines[this.params.httpEngine]() // check if engine is supported
if (typeof HTTPProtocolsInstances[this.params.protocol].createServer !== "function") {
throw new Error("Invalid HTTP protocol (Missing createServer function)")
}
this.engineInstance = global.engineInstance = HTTPEngines[this.params.httpEngine]()
this.httpInstance = global.httpInstance = HTTPProtocolsInstances[this.params.protocol].createServer({
...this.engineInstance,
key: this.params.key,
cert: this.params.cert,
})
this.wsInterface = global.wsInterface = { this.wsInterface = global.wsInterface = {
io: new io.Server(this.WSListenPort), io: new io.Server(this.httpInstance),
map: {}, map: {},
eventsChannels: [], eventsChannels: [],
} }
@ -139,7 +156,7 @@ class Server {
this.wsInterface.io.on("connection", this.handleWSClientConnection) this.wsInterface.io.on("connection", this.handleWSClientConnection)
// initialize http server // initialize http server
await this.httpInterface.listen(this.HTTPlistenPort, this.params.listen ?? "0.0.0.0") await this.httpInstance.listen(this.listenPort, this.params.listen ?? "0.0.0.0")
// output server info // output server info
InternalConsole.log(`✅ Server is up and running!`) InternalConsole.log(`✅ Server is up and running!`)
@ -151,7 +168,7 @@ class Server {
} }
initializeHeaders = () => { initializeHeaders = () => {
this.httpInterface.use((req, res, next) => { this.engineInstance.use((req, res, next) => {
Object.keys(this.headers).forEach((key) => { Object.keys(this.headers).forEach((key) => {
res.setHeader(key, this.headers[key]) res.setHeader(key, this.headers[key])
}) })
@ -165,7 +182,7 @@ class Server {
useMiddlewares.forEach((middleware) => { useMiddlewares.forEach((middleware) => {
if (typeof middleware === "function") { if (typeof middleware === "function") {
this.httpInterface.use(middleware) this.engineInstance.use(middleware)
} }
}) })
} }
@ -216,7 +233,7 @@ class Server {
} }
// check if method is supported // check if method is supported
if (typeof this.httpInterface[endpoint.method] !== "function") { if (typeof this.engineInstance[endpoint.method] !== "function") {
throw new Error(`Method [${endpoint.method}] is not supported!`) throw new Error(`Method [${endpoint.method}] is not supported!`)
} }
@ -236,7 +253,7 @@ class Server {
const routeModel = [endpoint.route, ...middlewares, this.createHTTPRequestHandler(endpoint)] const routeModel = [endpoint.route, ...middlewares, this.createHTTPRequestHandler(endpoint)]
// register endpoint to http interface router // register endpoint to http interface router
this.httpInterface[endpoint.method](...routeModel) this.engineInstance[endpoint.method](...routeModel)
// extend to map // extend to map
this.endpointsMap[endpoint.method] = { this.endpointsMap[endpoint.method] = {
@ -315,8 +332,8 @@ class Server {
cleanupProcess = () => { cleanupProcess = () => {
this.log("🛑 Stopping server...") this.log("🛑 Stopping server...")
if (typeof this.httpInterface.close === "function") { if (typeof this.engineInstance.close === "function") {
this.httpInterface.close() this.engineInstance.close()
} }
this.wsInterface.io.close() this.wsInterface.io.close()
@ -401,10 +418,11 @@ class Server {
"ID": this.id, "ID": this.id,
"HTTPEngine": this.params.httpEngine, "HTTPEngine": this.params.httpEngine,
"Version": LINEBRIDGE_SERVER_VERSION, "Version": LINEBRIDGE_SERVER_VERSION,
"WS Protocol": this.params.wsProtocol,
"Protocol": this.params.protocol,
"HTTP address": this.HTTPAddress, "HTTP address": this.HTTPAddress,
"WS address": this.WSAddress, "WS address": this.WSAddress,
"WS port": this.WSListenPort, "Listen port": this.listenPort,
"HTTP port": this.HTTPlistenPort,
}) })
} }