From 6d58b99865f17f08b1e3108d73b99188904085f1 Mon Sep 17 00:00:00 2001 From: authcompanion Date: Sun, 10 Sep 2023 12:31:51 -0400 Subject: [PATCH] refactor: improve readability and maintainability --- app.js | 49 ++++++++++++++++++------------------ server.js | 74 +++++++++++++++++++++++++++---------------------------- 2 files changed, 61 insertions(+), 62 deletions(-) diff --git a/app.js b/app.js index f38c606..adc6a7e 100644 --- a/app.js +++ b/app.js @@ -5,37 +5,36 @@ import webRoutes from "./routes/ui.routes.js"; import sqlite3 from "./plugins/db/db.js"; import serverkey from "./plugins/key/server.key.js"; import adminkey from "./plugins/key/admin.key.js"; - import pkg from "./package.json" assert { type: "json" }; -const appVersion = pkg.version; -const buildApp = async function (serverOptions) { - const app = await Fastify(serverOptions); +const appVersion = pkg.version; - //register the sqlite database plugin - await app.register(sqlite3); - //register the server key plugin - await app.register(serverkey); - //register the admin key plugin - await app.register(adminkey); +const buildApp = async (serverOptions) => { + const app = Fastify(serverOptions); - //register the admin api routes - await app.register(adminRoutes, { prefix: "/v1/admin" }); - //register the authentication api routes - await app.register(authRoutes, { prefix: "/v1/auth" }); - //register the frontend routes used for the UI. This is optional - await app.register(webRoutes, { prefix: "/v1/web" }); + try { + // Register the defaul authc plugins and routes + await app + .register(sqlite3) + .register(serverkey) + .register(adminkey) + .register(adminRoutes, { prefix: "/v1/admin" }) + .register(authRoutes, { prefix: "/v1/auth" }) + .register(webRoutes, { prefix: "/v1/web" }) + .register(async (fastify, opts) => { + fastify.get("/", async (req, reply) => { + return `Welcome and hello 👋 - AuthCompanion is serving requests! + Version: ${appVersion}`; + }); + }); - //register a default route welcoming the user - await app.register(async function defaultRoute(fastify, opts) { - fastify.get("/", async (req, reply) => { - return `Welcome and hello 👋 - AuthCompanion is serving requests! - Version: ${appVersion} - `; - }); - }); + await app.ready(); - return app; + return app; + } catch (err) { + console.log("Error building the app:", err); + throw err; // Rethrow the error to indicate app initialization failure + } }; export default buildApp; diff --git a/server.js b/server.js index 2d64d2d..d5a7bc2 100644 --- a/server.js +++ b/server.js @@ -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();