mirror of
https://github.com/ragestudio/linebridge.git
synced 2025-06-09 10:34:17 +00:00
define global default variables
This commit is contained in:
parent
9a5296661f
commit
5b86282389
@ -13,11 +13,15 @@ const { randomWord } = require("@corenode/utils")
|
|||||||
|
|
||||||
const { serverManifest } = require("../lib")
|
const { serverManifest } = require("../lib")
|
||||||
|
|
||||||
|
// set globals default variables
|
||||||
global.LOCALHOST_ADDRESS = net.ip.getHostAddress() ?? "localhost"
|
global.LOCALHOST_ADDRESS = net.ip.getHostAddress() ?? "localhost"
|
||||||
|
|
||||||
global.FIXED_HTTP_METHODS = {
|
global.FIXED_HTTP_METHODS = {
|
||||||
"del": "delete"
|
"del": "delete"
|
||||||
}
|
}
|
||||||
|
|
||||||
global.VALID_HTTP_METHODS = ["get", "post", "put", "patch", "del", "delete", "trace", "head", "any", "options", "ws"]
|
global.VALID_HTTP_METHODS = ["get", "post", "put", "patch", "del", "delete", "trace", "head", "any", "options", "ws"]
|
||||||
|
|
||||||
global.DEFAULT_HEADERS = {
|
global.DEFAULT_HEADERS = {
|
||||||
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept, Authorization",
|
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept, Authorization",
|
||||||
"Access-Control-Allow-Origin": "*",
|
"Access-Control-Allow-Origin": "*",
|
||||||
@ -25,7 +29,11 @@ global.DEFAULT_HEADERS = {
|
|||||||
"Access-Control-Allow-Credentials": "true",
|
"Access-Control-Allow-Credentials": "true",
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultMiddlewares = [
|
global.DEFAULT_SERVER_PARAMS = {
|
||||||
|
urlencoded: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
global.DEFAULT_MIDDLEWARES = [
|
||||||
require('cors')({
|
require('cors')({
|
||||||
"origin": "*",
|
"origin": "*",
|
||||||
"methods": DEFAULT_HEADERS["Access-Control-Allow-Methods"],
|
"methods": DEFAULT_HEADERS["Access-Control-Allow-Methods"],
|
||||||
@ -35,7 +43,7 @@ const defaultMiddlewares = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
if (process.env.NODE_ENV !== "production") {
|
if (process.env.NODE_ENV !== "production") {
|
||||||
defaultMiddlewares.push(require("morgan")("dev"))
|
global.DEFAULT_MIDDLEWARES.push(require("morgan")("dev"))
|
||||||
}
|
}
|
||||||
|
|
||||||
function outputServerError({
|
function outputServerError({
|
||||||
@ -48,18 +56,28 @@ function outputServerError({
|
|||||||
|
|
||||||
class Server {
|
class Server {
|
||||||
constructor(params = {}, controllers = [], middlewares = {}) {
|
constructor(params = {}, controllers = [], middlewares = {}) {
|
||||||
this.params = { ...params }
|
this.params = {
|
||||||
this.controllers = [...controllers]
|
...global.DEFAULT_SERVER_PARAMS,
|
||||||
this.middlewares = { ...middlewares }
|
...params
|
||||||
this.headers = { ...DEFAULT_HEADERS, ...this.params.headers }
|
}
|
||||||
|
this.controllers = [
|
||||||
|
...controllers
|
||||||
|
]
|
||||||
|
this.middlewares = {
|
||||||
|
...middlewares
|
||||||
|
}
|
||||||
|
this.headers = {
|
||||||
|
...global.DEFAULT_HEADERS,
|
||||||
|
...this.params.headers
|
||||||
|
}
|
||||||
this.endpointsMap = {}
|
this.endpointsMap = {}
|
||||||
|
|
||||||
this.WSListenPort = this.params.wsPort ?? 3020
|
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
|
||||||
this.HTTPAddress = `http://${LOCALHOST_ADDRESS}:${this.HTTPlistenPort}`
|
this.HTTPAddress = `http://${global.LOCALHOST_ADDRESS}:${this.HTTPlistenPort}`
|
||||||
this.WSAddress = `ws://${LOCALHOST_ADDRESS}:${this.WSListenPort}`
|
this.WSAddress = `ws://${global.LOCALHOST_ADDRESS}:${this.WSListenPort}`
|
||||||
|
|
||||||
//* set server basics
|
//* set server basics
|
||||||
this.httpInterface = global.httpInterface = new HyperExpress.Server()
|
this.httpInterface = global.httpInterface = new HyperExpress.Server()
|
||||||
@ -136,7 +154,7 @@ class Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
initializeMiddlewares = () => {
|
initializeMiddlewares = () => {
|
||||||
const useMiddlewares = [...defaultMiddlewares, ...(this.params.middlewares ?? [])]
|
const useMiddlewares = [...global.DEFAULT_MIDDLEWARES, ...(this.params.middlewares ?? [])]
|
||||||
|
|
||||||
useMiddlewares.forEach((middleware) => {
|
useMiddlewares.forEach((middleware) => {
|
||||||
if (typeof middleware === "function") {
|
if (typeof middleware === "function") {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user