mirror of
https://github.com/ragestudio/linebridge.git
synced 2025-06-09 10:34:17 +00:00
use single server lib
This commit is contained in:
parent
fca44f53bb
commit
9070dcaad9
@ -3,10 +3,10 @@ const corenode = require("corenode")
|
||||
|
||||
corenode.runInNewRuntime(() => {
|
||||
const { randomWord } = require("@corenode/utils")
|
||||
const lib = require("../server/index.js")
|
||||
const server = require("../server/index.js")
|
||||
|
||||
// create server
|
||||
new lib.HttpServer({
|
||||
new server({
|
||||
autoInit: true,
|
||||
id: process.env.serverID ?? randomWord.generate(),
|
||||
})
|
||||
|
@ -1,291 +0,0 @@
|
||||
const fs = require("fs")
|
||||
const http = require("nanoexpress")
|
||||
const bodyParser = require('@nanoexpress/middleware-body-parser/cjs')
|
||||
|
||||
const tokenizer = require("corenode/libs/tokenizer")
|
||||
const net = require("corenode/net")
|
||||
|
||||
const nethub = require("../../lib/nethub")
|
||||
const { getLocalEndpoints, fetchController, serverManifest } = require("../../lib")
|
||||
const hostAddress = net.ip.getHostAddress() ?? "localhost"
|
||||
|
||||
const defaultHeaders = {
|
||||
"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, DEL",
|
||||
"Access-Control-Allow-Credentials": "true",
|
||||
}
|
||||
|
||||
const defaultMiddlewares = [
|
||||
require('cors')({
|
||||
"origin": "*",
|
||||
"methods": defaultHeaders["Access-Control-Allow-Methods"],
|
||||
"preflightContinue": false,
|
||||
"optionsSuccessStatus": 204
|
||||
}),
|
||||
require('morgan')("dev"),
|
||||
]
|
||||
|
||||
const helpers = process.runtime.helpers ?? require('@corenode/helpers')
|
||||
|
||||
//* set globals
|
||||
global.SERVER_VERSION = helpers.getVersion()
|
||||
|
||||
const FixedMethods = {
|
||||
"delete": "del",
|
||||
}
|
||||
const ValidMethods = ["get", "post", "put", "patch", "del", "trace", "head", "any", "options", "ws"]
|
||||
|
||||
class Server {
|
||||
constructor(params, endpoints, middlewares) {
|
||||
this.params = params ?? {}
|
||||
this.port = this.params.port ?? 3010
|
||||
|
||||
// handle endpoints && middlewares
|
||||
const localEndpoints = getLocalEndpoints()
|
||||
if (typeof endpoints !== "undefined" && Array.isArray(endpoints)) {
|
||||
this.params.endpoints = [...this.params.endpoints ?? [], ...endpoints]
|
||||
}
|
||||
if (localEndpoints && Array.isArray(localEndpoints)) {
|
||||
this.params.endpoints = [...this.params.endpoints ?? [], ...localEndpoints]
|
||||
}
|
||||
|
||||
//* set params jails
|
||||
this.endpoints = {}
|
||||
this.serverMiddlewares = [...this.params.serverMiddlewares ?? [], ...defaultMiddlewares]
|
||||
this.middlewares = { ...this.params.middlewares, ...middlewares }
|
||||
this.controllers = { ...this.params.controllers }
|
||||
this.headers = { ...defaultHeaders, ...this.params.headers }
|
||||
|
||||
//* set server basics
|
||||
this.httpServer = http()
|
||||
|
||||
//* set id's
|
||||
this.id = this.params.id ?? process.runtime?.helpers?.getRootPackage()?.name ?? "unavailable"
|
||||
this.usid = tokenizer.generateUSID()
|
||||
this.oskid = "unloaded"
|
||||
|
||||
this.localOrigin = `http://${hostAddress}:${this.port}`
|
||||
this.nethubOrigin = ""
|
||||
|
||||
//? check if origin.server exists
|
||||
if (!fs.existsSync(serverManifest.filepath)) {
|
||||
serverManifest.create()
|
||||
}
|
||||
|
||||
//? check origin.server integrity
|
||||
const MANIFEST_DATA = global.MANIFEST_DATA = serverManifest.get()
|
||||
const MANIFEST_STAT = global.MANIFEST_STAT = serverManifest.stat()
|
||||
|
||||
if (typeof MANIFEST_DATA.created === "undefined") {
|
||||
console.warn("Server generation file not contains an creation date")
|
||||
serverManifest.write({ created: Date.parse(MANIFEST_STAT.birthtime) })
|
||||
}
|
||||
|
||||
if (typeof MANIFEST_DATA.serverToken === "undefined") {
|
||||
console.warn("Missing server token!")
|
||||
serverManifest.create()
|
||||
}
|
||||
|
||||
this.reloadOskid()
|
||||
|
||||
this.preInitialization()
|
||||
if (this.params.autoInit) {
|
||||
this.init()
|
||||
}
|
||||
}
|
||||
|
||||
reloadOskid() {
|
||||
this.oskid = serverManifest.get("serverToken")
|
||||
}
|
||||
|
||||
register = (controller) => {
|
||||
if (typeof controller === "undefined") {
|
||||
console.error(`Invalid endpoint, missing parameters!`)
|
||||
return false
|
||||
}
|
||||
|
||||
// check and fix method
|
||||
controller.method = controller.method?.toLowerCase() ?? "get"
|
||||
|
||||
if (FixedMethods[controller.method]) {
|
||||
controller.method = FixedMethods[controller.method]
|
||||
}
|
||||
|
||||
// validate method
|
||||
if (!ValidMethods.includes(controller.method)) {
|
||||
throw new Error(`Invalid endpoint method: ${controller.method}`)
|
||||
}
|
||||
|
||||
// fulfill an undefined fn
|
||||
if (typeof controller.fn === "undefined") {
|
||||
controller.fn = (req, res, next) => {
|
||||
return next()
|
||||
}
|
||||
}
|
||||
|
||||
// fetchController function if needed
|
||||
if (typeof controller.fn === "string") {
|
||||
let stack = []
|
||||
const resolverKeys = controller.fn.split(".")
|
||||
|
||||
resolverKeys.forEach((key, index) => {
|
||||
if (index === 0) {
|
||||
if (typeof this.controllers[key] !== "undefined") {
|
||||
stack.push(this.controllers[key])
|
||||
} else {
|
||||
stack.push(fetchController(key, this.params.controllersPath))
|
||||
}
|
||||
|
||||
} else {
|
||||
stack.push(stack[index - 1][key])
|
||||
}
|
||||
|
||||
|
||||
if (resolverKeys.length === index + 1) {
|
||||
let resolved = stack[index]
|
||||
|
||||
if (typeof resolved !== "function" && typeof resolved[controller.method] === "function") {
|
||||
resolved = resolved[controller.method]
|
||||
}
|
||||
|
||||
return controller.fn = resolved
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// extend main fn
|
||||
controller._exec = async (req, res, next) => {
|
||||
try {
|
||||
await controller.fn(req, res, next)
|
||||
} catch (error) {
|
||||
return res.status(500).json({ error: error.message, endpoint: controller.route })
|
||||
}
|
||||
}
|
||||
|
||||
// set endpoint registry
|
||||
if (typeof this.endpoints[controller.method] === "undefined") {
|
||||
this.endpoints[controller.method] = Object()
|
||||
}
|
||||
|
||||
this.endpoints[controller.method][controller.route] = controller
|
||||
|
||||
// create routeModel
|
||||
const routeModel = [controller.route]
|
||||
|
||||
// query middlewares
|
||||
if (typeof controller.middleware !== "undefined") {
|
||||
let query = []
|
||||
|
||||
if (typeof controller.middleware === "string") {
|
||||
query.push(controller.middleware)
|
||||
}
|
||||
if (Array.isArray(controller.middleware)) {
|
||||
query = controller.middleware
|
||||
}
|
||||
|
||||
query.forEach((middleware) => {
|
||||
if (typeof this.middlewares[middleware] === "function") {
|
||||
routeModel.push(this.middlewares[middleware])
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// query main endpoint function
|
||||
if (typeof controller._exec === "function") {
|
||||
routeModel.push(controller._exec)
|
||||
}
|
||||
|
||||
// append to router
|
||||
this.httpServer[controller.method](...routeModel)
|
||||
}
|
||||
|
||||
preInitialization() {
|
||||
// set middlewares
|
||||
this.httpServer.use(bodyParser())
|
||||
|
||||
if (Array.isArray(this.serverMiddlewares)) {
|
||||
this.serverMiddlewares.forEach((middleware) => {
|
||||
if (typeof middleware === "function") {
|
||||
this.httpServer.use(middleware)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// set headers
|
||||
this.httpServer.use((req, res, next) => {
|
||||
Object.keys(this.headers).forEach((key) => {
|
||||
res.setHeader(key, this.headers[key])
|
||||
})
|
||||
|
||||
next()
|
||||
})
|
||||
|
||||
// register root resolver
|
||||
this.register({
|
||||
method: "get",
|
||||
route: "/",
|
||||
fn: (req, res) => {
|
||||
return res.json({
|
||||
id: this.id,
|
||||
usid: this.usid,
|
||||
oskid: this.oskid,
|
||||
time: new Date().getTime(),
|
||||
version: SERVER_VERSION
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
this.register({
|
||||
method: "get",
|
||||
route: "/map",
|
||||
fn: (req, res) => {
|
||||
const map = {}
|
||||
|
||||
Object.keys(this.endpoints).forEach((method) => {
|
||||
if (typeof map[method] === "undefined") {
|
||||
map[method] = []
|
||||
}
|
||||
|
||||
Object.keys(this.endpoints[method]).forEach((route) => {
|
||||
map[method].push({
|
||||
route: route,
|
||||
method: method
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
return res.json(map)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
init = async () => {
|
||||
// write lastStart
|
||||
serverManifest.write({ lastStart: Date.now() })
|
||||
|
||||
// load and set endpoints
|
||||
if (Array.isArray(this.params.endpoints)) {
|
||||
this.params.endpoints.forEach((endpoint, index) => {
|
||||
try {
|
||||
// append to server
|
||||
this.register(endpoint)
|
||||
} catch (error) {
|
||||
console.error(`🆘 [${endpoint.route}[${index}]] Failed to load endpoint > ${error.message}`)
|
||||
process.runtime.logger.dump(error)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
await this.httpServer.listen(this.port, this.params.listen ?? '0.0.0.0')
|
||||
|
||||
//? register to nethub
|
||||
if (this.params.onlineNethub) {
|
||||
nethub.registerOrigin({ entry: "/", oskid: this.oskid, id: this.id })
|
||||
}
|
||||
|
||||
console.log(`✅ Ready on port ${this.port}!`)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Server
|
@ -1,4 +1,291 @@
|
||||
const HttpServer = require('./http')
|
||||
const WSServer = require('./websocket')
|
||||
const fs = require("fs")
|
||||
const http = require("nanoexpress")
|
||||
const bodyParser = require('@nanoexpress/middleware-body-parser/cjs')
|
||||
|
||||
module.exports = { HttpServer, WSServer }
|
||||
const tokenizer = require("corenode/libs/tokenizer")
|
||||
const net = require("corenode/net")
|
||||
|
||||
const nethub = require("../lib/nethub")
|
||||
const { getLocalEndpoints, fetchController, serverManifest } = require("../lib")
|
||||
const hostAddress = net.ip.getHostAddress() ?? "localhost"
|
||||
|
||||
const defaultHeaders = {
|
||||
"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, DEL",
|
||||
"Access-Control-Allow-Credentials": "true",
|
||||
}
|
||||
|
||||
const defaultMiddlewares = [
|
||||
require('cors')({
|
||||
"origin": "*",
|
||||
"methods": defaultHeaders["Access-Control-Allow-Methods"],
|
||||
"preflightContinue": false,
|
||||
"optionsSuccessStatus": 204
|
||||
}),
|
||||
require('morgan')("dev"),
|
||||
]
|
||||
|
||||
const helpers = process.runtime.helpers ?? require('@corenode/helpers')
|
||||
|
||||
//* set globals
|
||||
global.SERVER_VERSION = helpers.getVersion()
|
||||
|
||||
const FixedMethods = {
|
||||
"delete": "del",
|
||||
}
|
||||
const ValidMethods = ["get", "post", "put", "patch", "del", "trace", "head", "any", "options", "ws"]
|
||||
|
||||
class Server {
|
||||
constructor(params, endpoints, middlewares) {
|
||||
this.params = params ?? {}
|
||||
this.port = this.params.port ?? 3010
|
||||
|
||||
// handle endpoints && middlewares
|
||||
const localEndpoints = getLocalEndpoints()
|
||||
if (typeof endpoints !== "undefined" && Array.isArray(endpoints)) {
|
||||
this.params.endpoints = [...this.params.endpoints ?? [], ...endpoints]
|
||||
}
|
||||
if (localEndpoints && Array.isArray(localEndpoints)) {
|
||||
this.params.endpoints = [...this.params.endpoints ?? [], ...localEndpoints]
|
||||
}
|
||||
|
||||
//* set params jails
|
||||
this.endpoints = {}
|
||||
this.serverMiddlewares = [...this.params.serverMiddlewares ?? [], ...defaultMiddlewares]
|
||||
this.middlewares = { ...this.params.middlewares, ...middlewares }
|
||||
this.controllers = { ...this.params.controllers }
|
||||
this.headers = { ...defaultHeaders, ...this.params.headers }
|
||||
|
||||
//* set server basics
|
||||
this.httpServer = http()
|
||||
|
||||
//* set id's
|
||||
this.id = this.params.id ?? process.runtime?.helpers?.getRootPackage()?.name ?? "unavailable"
|
||||
this.usid = tokenizer.generateUSID()
|
||||
this.oskid = "unloaded"
|
||||
|
||||
this.localOrigin = `http://${hostAddress}:${this.port}`
|
||||
this.nethubOrigin = ""
|
||||
|
||||
//? check if origin.server exists
|
||||
if (!fs.existsSync(serverManifest.filepath)) {
|
||||
serverManifest.create()
|
||||
}
|
||||
|
||||
//? check origin.server integrity
|
||||
const MANIFEST_DATA = global.MANIFEST_DATA = serverManifest.get()
|
||||
const MANIFEST_STAT = global.MANIFEST_STAT = serverManifest.stat()
|
||||
|
||||
if (typeof MANIFEST_DATA.created === "undefined") {
|
||||
console.warn("Server generation file not contains an creation date")
|
||||
serverManifest.write({ created: Date.parse(MANIFEST_STAT.birthtime) })
|
||||
}
|
||||
|
||||
if (typeof MANIFEST_DATA.serverToken === "undefined") {
|
||||
console.warn("Missing server token!")
|
||||
serverManifest.create()
|
||||
}
|
||||
|
||||
this.reloadOskid()
|
||||
|
||||
this.preInitialization()
|
||||
if (this.params.autoInit) {
|
||||
this.init()
|
||||
}
|
||||
}
|
||||
|
||||
reloadOskid() {
|
||||
this.oskid = serverManifest.get("serverToken")
|
||||
}
|
||||
|
||||
register = (controller) => {
|
||||
if (typeof controller === "undefined") {
|
||||
console.error(`Invalid endpoint, missing parameters!`)
|
||||
return false
|
||||
}
|
||||
|
||||
// check and fix method
|
||||
controller.method = controller.method?.toLowerCase() ?? "get"
|
||||
|
||||
if (FixedMethods[controller.method]) {
|
||||
controller.method = FixedMethods[controller.method]
|
||||
}
|
||||
|
||||
// validate method
|
||||
if (!ValidMethods.includes(controller.method)) {
|
||||
throw new Error(`Invalid endpoint method: ${controller.method}`)
|
||||
}
|
||||
|
||||
// fulfill an undefined fn
|
||||
if (typeof controller.fn === "undefined") {
|
||||
controller.fn = (req, res, next) => {
|
||||
return next()
|
||||
}
|
||||
}
|
||||
|
||||
// fetchController function if needed
|
||||
if (typeof controller.fn === "string") {
|
||||
let stack = []
|
||||
const resolverKeys = controller.fn.split(".")
|
||||
|
||||
resolverKeys.forEach((key, index) => {
|
||||
if (index === 0) {
|
||||
if (typeof this.controllers[key] !== "undefined") {
|
||||
stack.push(this.controllers[key])
|
||||
} else {
|
||||
stack.push(fetchController(key, this.params.controllersPath))
|
||||
}
|
||||
|
||||
} else {
|
||||
stack.push(stack[index - 1][key])
|
||||
}
|
||||
|
||||
|
||||
if (resolverKeys.length === index + 1) {
|
||||
let resolved = stack[index]
|
||||
|
||||
if (typeof resolved !== "function" && typeof resolved[controller.method] === "function") {
|
||||
resolved = resolved[controller.method]
|
||||
}
|
||||
|
||||
return controller.fn = resolved
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// extend main fn
|
||||
controller._exec = async (req, res, next) => {
|
||||
try {
|
||||
await controller.fn(req, res, next)
|
||||
} catch (error) {
|
||||
return res.status(500).json({ error: error.message, endpoint: controller.route })
|
||||
}
|
||||
}
|
||||
|
||||
// set endpoint registry
|
||||
if (typeof this.endpoints[controller.method] === "undefined") {
|
||||
this.endpoints[controller.method] = Object()
|
||||
}
|
||||
|
||||
this.endpoints[controller.method][controller.route] = controller
|
||||
|
||||
// create routeModel
|
||||
const routeModel = [controller.route]
|
||||
|
||||
// query middlewares
|
||||
if (typeof controller.middleware !== "undefined") {
|
||||
let query = []
|
||||
|
||||
if (typeof controller.middleware === "string") {
|
||||
query.push(controller.middleware)
|
||||
}
|
||||
if (Array.isArray(controller.middleware)) {
|
||||
query = controller.middleware
|
||||
}
|
||||
|
||||
query.forEach((middleware) => {
|
||||
if (typeof this.middlewares[middleware] === "function") {
|
||||
routeModel.push(this.middlewares[middleware])
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// query main endpoint function
|
||||
if (typeof controller._exec === "function") {
|
||||
routeModel.push(controller._exec)
|
||||
}
|
||||
|
||||
// append to router
|
||||
this.httpServer[controller.method](...routeModel)
|
||||
}
|
||||
|
||||
preInitialization() {
|
||||
// set middlewares
|
||||
this.httpServer.use(bodyParser())
|
||||
|
||||
if (Array.isArray(this.serverMiddlewares)) {
|
||||
this.serverMiddlewares.forEach((middleware) => {
|
||||
if (typeof middleware === "function") {
|
||||
this.httpServer.use(middleware)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// set headers
|
||||
this.httpServer.use((req, res, next) => {
|
||||
Object.keys(this.headers).forEach((key) => {
|
||||
res.setHeader(key, this.headers[key])
|
||||
})
|
||||
|
||||
next()
|
||||
})
|
||||
|
||||
// register root resolver
|
||||
this.register({
|
||||
method: "get",
|
||||
route: "/",
|
||||
fn: (req, res) => {
|
||||
return res.json({
|
||||
id: this.id,
|
||||
usid: this.usid,
|
||||
oskid: this.oskid,
|
||||
time: new Date().getTime(),
|
||||
version: SERVER_VERSION
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
this.register({
|
||||
method: "get",
|
||||
route: "/map",
|
||||
fn: (req, res) => {
|
||||
const map = {}
|
||||
|
||||
Object.keys(this.endpoints).forEach((method) => {
|
||||
if (typeof map[method] === "undefined") {
|
||||
map[method] = []
|
||||
}
|
||||
|
||||
Object.keys(this.endpoints[method]).forEach((route) => {
|
||||
map[method].push({
|
||||
route: route,
|
||||
method: method
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
return res.json(map)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
init = async () => {
|
||||
// write lastStart
|
||||
serverManifest.write({ lastStart: Date.now() })
|
||||
|
||||
// load and set endpoints
|
||||
if (Array.isArray(this.params.endpoints)) {
|
||||
this.params.endpoints.forEach((endpoint, index) => {
|
||||
try {
|
||||
// append to server
|
||||
this.register(endpoint)
|
||||
} catch (error) {
|
||||
console.error(`🆘 [${endpoint.route}[${index}]] Failed to load endpoint > ${error.message}`)
|
||||
process.runtime.logger.dump(error)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
await this.httpServer.listen(this.port, this.params.listen ?? '0.0.0.0')
|
||||
|
||||
//? register to nethub
|
||||
if (this.params.onlineNethub) {
|
||||
nethub.registerOrigin({ entry: "/", oskid: this.oskid, id: this.id })
|
||||
}
|
||||
|
||||
console.log(`✅ Ready on port ${this.port}!`)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Server
|
@ -1,58 +0,0 @@
|
||||
const http = require('http')
|
||||
const socketIo = require('socket.io')
|
||||
|
||||
class WSServer {
|
||||
constructor(params) {
|
||||
this.params = { ...params }
|
||||
this.io = socketIo({
|
||||
serveClient: false,
|
||||
})
|
||||
|
||||
this.sockets = {}
|
||||
this.namespaces = {}
|
||||
|
||||
this.listenPort = this.params.listenPort ?? 3005
|
||||
this.httpServer = http.createServer()
|
||||
|
||||
this.io.on('connection', (socket) => {
|
||||
console.log(`New connection => ${socket.id}`)
|
||||
|
||||
this.sockets[socket.id] = socket
|
||||
|
||||
socket.on("disconnect", (reason) => {
|
||||
console.log(`Disconnect => ${socket.id} [${reason}]`)
|
||||
delete this.sockets[socket.id]
|
||||
})
|
||||
})
|
||||
|
||||
this.init()
|
||||
}
|
||||
|
||||
init() {
|
||||
if (typeof this.params.namespaces !== "undefined") {
|
||||
if (Array.isArray(this.params.namespaces)) {
|
||||
this.params.namespaces.forEach((namespace) => {
|
||||
this.attachNamespace(namespace)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
this.io.attach(this.httpServer, {
|
||||
pingInterval: 10000,
|
||||
pingTimeout: 5000,
|
||||
cookie: false
|
||||
})
|
||||
|
||||
this.httpServer.listen(this.listenPort)
|
||||
console.log("WSS Listen on " + this.listenPort)
|
||||
}
|
||||
|
||||
attachNamespace = (namespace) => {
|
||||
this.namespaces[namespace] = this.io.of(`/${namespace}`)
|
||||
|
||||
return this.namespaces[namespace]
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = WSServer
|
Loading…
x
Reference in New Issue
Block a user