Merge pull request #2 from ragestudio/hyper-express-update

Hyper express update
This commit is contained in:
srgooglo 2022-05-05 10:06:30 +02:00 committed by GitHub
commit f1d83f663d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 15 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -1,4 +1,4 @@
{ {
"version": "0.10.14", "version": "0.11.0",
"fixedMainScript": "./client/index.js" "fixedMainScript": "./client/index.js"
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "linebridge", "name": "linebridge",
"version": "0.10.14", "version": "0.11.0",
"description": "A simple, fast, and powerful REST API interface library", "description": "A simple, fast, and powerful REST API interface library",
"author": "RageStudio", "author": "RageStudio",
"main": "./dist/client/index.js", "main": "./dist/client/index.js",
@ -19,15 +19,14 @@
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@corenode/utils": "0.28.26", "@corenode/utils": "0.28.26",
"@nanoexpress/middleware-body-parser": "1.2.2", "axios": "0.27.2",
"axios": "0.26.0",
"cors": "2.8.5", "cors": "2.8.5",
"hyper-express": "^6.1.1",
"md5": "2.3.0", "md5": "2.3.0",
"nanoexpress": "5.1.2",
"nanoid": "3.3.1",
"socket.io-client": "4.4.1",
"socket.io": "4.4.1",
"morgan": "1.10.0", "morgan": "1.10.0",
"nanoid": "3.3.4",
"socket.io": "4.5.0",
"socket.io-client": "4.5.0",
"uuid": "8.3.2" "uuid": "8.3.2"
}, },
"devDependencies": { "devDependencies": {

View File

@ -4,7 +4,9 @@ const corenode = require("corenode")
corenode.runInNewRuntime(() => { corenode.runInNewRuntime(() => {
const server = require("../server/index.js") const server = require("../server/index.js")
new server({ const instance = new server({
id: process.env.serverID, id: process.env.serverID,
}) })
instance.initialize()
}) })

View File

@ -2,7 +2,7 @@ const path = require("path")
const fs = require("fs") const fs = require("fs")
const net = require("corenode/net") const net = require("corenode/net")
const http = require("nanoexpress") const HyperExpress = require("hyper-express")
const io = require("socket.io") const io = require("socket.io")
const packageJSON = require(path.resolve(module.path, "../../package.json")) const packageJSON = require(path.resolve(module.path, "../../package.json"))
@ -26,7 +26,6 @@ global.DEFAULT_HEADERS = {
} }
const defaultMiddlewares = [ const defaultMiddlewares = [
require("@nanoexpress/middleware-body-parser/cjs")(),
require('cors')({ require('cors')({
"origin": "*", "origin": "*",
"methods": DEFAULT_HEADERS["Access-Control-Allow-Methods"], "methods": DEFAULT_HEADERS["Access-Control-Allow-Methods"],
@ -40,7 +39,7 @@ const FixedMethods = {
} }
if (process.env.NODE_ENV !== "production") { if (process.env.NODE_ENV !== "production") {
defaultMiddlewares.push(require('morgan')("dev")) defaultMiddlewares.push(require("morgan")("dev"))
} }
class Server { class Server {
@ -51,7 +50,7 @@ class Server {
this.headers = { ...DEFAULT_HEADERS, ...this.params.headers } this.headers = { ...DEFAULT_HEADERS, ...this.params.headers }
this.endpointsMap = {} this.endpointsMap = {}
this.WSListenPort = this.params.wsPort ?? 3011 this.WSListenPort = this.params.wsPort ?? 3020
this.HTTPlistenPort = this.params.port ?? 3010 this.HTTPlistenPort = this.params.port ?? 3010
// TODO: Handle HTTPS and WSS // TODO: Handle HTTPS and WSS
@ -59,7 +58,7 @@ class Server {
this.WSAddress = `ws://${LOCALHOST_ADDRESS}:${this.WSListenPort}` this.WSAddress = `ws://${LOCALHOST_ADDRESS}:${this.WSListenPort}`
//* set server basics //* set server basics
this.httpInterface = global.httpInterface = http() this.httpInterface = global.httpInterface = new HyperExpress.Server()
this.wsInterface = global.wsInterface = { this.wsInterface = global.wsInterface = {
io: new io.Server(this.WSListenPort), io: new io.Server(this.WSListenPort),
map: {}, map: {},
@ -120,7 +119,14 @@ class Server {
// initialize http server // initialize http server
await this.httpInterface.listen(this.HTTPlistenPort, this.params.listen ?? "0.0.0.0") await this.httpInterface.listen(this.HTTPlistenPort, this.params.listen ?? "0.0.0.0")
console.log(`✅ Ready on port ${this.HTTPlistenPort}!`) // output server info
console.log(`✅ Server is up and running!`)
this.consoleOutputServerInfo()
// handle exit events
process.on("SIGTERM", this.cleanupProcess)
process.on("SIGINT", this.cleanupProcess)
process.on("exit", this.cleanupProcess)
} }
handleWSClientConnection = async (socket) => { handleWSClientConnection = async (socket) => {
@ -275,6 +281,25 @@ class Server {
} }
}) })
} }
consoleOutputServerInfo = () => {
console.log(`🌐 Server info:`)
console.table({
"ID": this.id,
"Version": LINEBRIDGE_SERVER_VERSION,
"HTTP address": this.HTTPAddress,
"WS address": this.WSAddress,
"WS port": this.WSListenPort,
"HTTP port": this.HTTPlistenPort,
})
}
cleanupProcess = () => {
console.log("🔴 Stopping server...")
this.httpInterface.close()
this.wsInterface.io.close()
}
} }
module.exports = Server module.exports = Server