-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve readability and maintainability
- Loading branch information
1 parent
7afb22c
commit 6d58b99
Showing
2 changed files
with
61 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,54 @@ | ||
import buildApp from "./app.js"; | ||
import config from "./config.js"; | ||
|
||
const server = await buildApp({ | ||
logger: true, | ||
forceCloseConnections: true, | ||
}); | ||
|
||
/** | ||
* Let's run the AuthCompanion server! | ||
*/ | ||
function startServer() { | ||
server.listen( | ||
{ | ||
async function startServer() { | ||
try { | ||
const server = await buildApp({ | ||
logger: true, | ||
forceCloseConnections: true, | ||
}); | ||
|
||
await server.listen({ | ||
port: config.AUTHPORT, | ||
host: "0.0.0.0", | ||
}, | ||
(err) => { | ||
if (err) { | ||
console.log("Error starting server:", err); | ||
process.exit(1); | ||
} | ||
} | ||
); | ||
}); | ||
|
||
printStartupMessage(config.AUTHPORT); | ||
setupGracefulShutdown(server); | ||
} catch (error) { | ||
console.error("Error starting server:", error); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
function printStartupMessage(port) { | ||
console.log(` | ||
########################################################### | ||
The AuthCompanion Server has started | ||
🖥️ Client UI on: http://localhost:${config.AUTHPORT}/v1/web/login | ||
🚀 Admin UI on: http://localhost:${config.AUTHPORT}/v1/admin/login | ||
🖥️ Client UI on: http://localhost:${port}/v1/web/login | ||
🚀 Admin UI on: http://localhost:${port}/v1/admin/login | ||
########################################################### | ||
`); | ||
console.log("Use CTRL-C to shutdown AuthCompanion"); | ||
console.log("Use CTRL-C to shut down AuthCompanion"); | ||
} | ||
|
||
//setup for gracefully exiting the AuthCompanion server | ||
async function handleSignal() { | ||
try { | ||
await server.close(); | ||
console.log(` | ||
AuthCompanion has exited, Good bye👋`); | ||
process.exit(0); | ||
} catch (error) { | ||
console.log("Error shutting down server:", error); | ||
process.exit(1); | ||
} | ||
} | ||
function setupGracefulShutdown(server) { | ||
const handleSignal = async () => { | ||
try { | ||
await server.close(); | ||
console.log("AuthCompanion has exited. Goodbye! 👋"); | ||
process.exit(0); | ||
} catch (error) { | ||
console.error("Error shutting down server:", error); | ||
process.exit(1); | ||
} | ||
}; | ||
|
||
process.on("SIGTERM", handleSignal); | ||
process.on("SIGINT", handleSignal); | ||
process.on("SIGUSR2", handleSignal); | ||
process.on("SIGTERM", handleSignal); | ||
process.on("SIGINT", handleSignal); | ||
process.on("SIGUSR2", handleSignal); | ||
} | ||
|
||
startServer(); |