implement silent mode

This commit is contained in:
srgooglo 2022-06-22 16:49:45 +02:00
parent 94cec32026
commit 1691e24580
2 changed files with 31 additions and 12 deletions

View File

@ -3,7 +3,7 @@ function outputServerError({
description, description,
ref = "SERVER", ref = "SERVER",
}) { }) {
console.error(`\n\x1b[41m\x1b[37m🆘 [${ref}] ${message}\x1b[0m ${description ? `\n ${description}` : ""} \n`) InternalConsole.error(`\n\x1b[41m\x1b[37m🆘 [${ref}] ${message}\x1b[0m ${description ? `\n ${description}` : ""} \n`)
} }
module.exports = outputServerError module.exports = outputServerError

View File

@ -5,13 +5,14 @@ const io = require("socket.io")
const tokenizer = require("corenode/libs/tokenizer") const tokenizer = require("corenode/libs/tokenizer")
const { randomWord } = require("@corenode/utils") const { randomWord } = require("@corenode/utils")
const { serverManifest, outputServerError } = require("./lib") const { serverManifest, outputServerError, internalConsole } = require("./lib")
const InternalConsole = global.InternalInternalConsole = internalConsole
const builtInMiddlewares = [] const builtInMiddlewares = []
const HTTPEngines = { const HTTPEngines = {
"hyper-express": () => { "hyper-express": () => {
console.warn("Hyper-Express is not fully supported yet") InternalConsole.warn("Hyper-Express is not fully supported yet")
const engine = require("hyper-express") const engine = require("hyper-express")
return new engine.Server() return new engine.Server()
@ -91,12 +92,12 @@ class Server {
const MANIFEST_STAT = global.MANIFEST_STAT = serverManifest.stat() const MANIFEST_STAT = global.MANIFEST_STAT = serverManifest.stat()
if (typeof MANIFEST_DATA.created === "undefined") { if (typeof MANIFEST_DATA.created === "undefined") {
console.warn("Server generation file not contains an creation date") InternalConsole.warn("Server generation file not contains an creation date")
serverManifest.write({ created: Date.parse(MANIFEST_STAT.birthtime) }) serverManifest.write({ created: Date.parse(MANIFEST_STAT.birthtime) })
} }
if (typeof MANIFEST_DATA.serverToken === "undefined") { if (typeof MANIFEST_DATA.serverToken === "undefined") {
console.warn("Missing server token!") InternalConsole.warn("Missing server token!")
serverManifest.create() serverManifest.create()
} }
@ -106,6 +107,18 @@ class Server {
serverManifest.write({ lastStart: Date.now() }) serverManifest.write({ lastStart: Date.now() })
// handle silent mode
global.consoleSilent = this.params.silent
if (global.consoleSilent) {
// find morgan middleware and remove it
const morganMiddleware = global.DEFAULT_MIDDLEWARES.find(middleware => middleware.name === "logger")
if (morganMiddleware) {
global.DEFAULT_MIDDLEWARES.splice(global.DEFAULT_MIDDLEWARES.indexOf(morganMiddleware), 1)
}
}
return this return this
} }
@ -129,8 +142,8 @@ class 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")
// output server info // output server info
console.log(`✅ Server is up and running!`) InternalConsole.log(`✅ Server is up and running!`)
this.consoleOutputServerInfo() this.InternalConsoleOutputServerInfo()
// handle exit events // handle exit events
process.on("SIGTERM", this.cleanupProcess) process.on("SIGTERM", this.cleanupProcess)
@ -164,7 +177,7 @@ class Server {
} }
if (controller.disabled) { if (controller.disabled) {
console.warn(`⏩ Controller [${controller.name}] is disabled! Initialization skipped...`) InternalConsole.warn(`⏩ Controller [${controller.name}] is disabled! Initialization skipped...`)
continue continue
} }
@ -288,8 +301,14 @@ class Server {
return middlewaresArray return middlewaresArray
} }
log = (...args) => {
if (!this.params.silent) {
InternalConsole.log(...args)
}
}
cleanupProcess = () => { cleanupProcess = () => {
console.log("🛑 Stopping server...") this.log("🛑 Stopping server...")
if (typeof this.httpInterface.close === "function") { if (typeof this.httpInterface.close === "function") {
this.httpInterface.close() this.httpInterface.close()
@ -371,9 +390,9 @@ class Server {
} }
// public methods // public methods
consoleOutputServerInfo = () => { InternalConsoleOutputServerInfo = () => {
console.log(`🌐 Server info:`) InternalConsole.log(`🌐 Server info:`)
console.table({ InternalConsole.table({
"ID": this.id, "ID": this.id,
"HTTPEngine": this.params.httpEngine, "HTTPEngine": this.params.httpEngine,
"Version": LINEBRIDGE_SERVER_VERSION, "Version": LINEBRIDGE_SERVER_VERSION,