Skip to content

Commit

Permalink
refactor: improve readability and maintainability
Browse files Browse the repository at this point in the history
  • Loading branch information
authcompanion committed Sep 10, 2023
1 parent 7afb22c commit 6d58b99
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 62 deletions.
49 changes: 24 additions & 25 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
74 changes: 37 additions & 37 deletions server.js
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();

0 comments on commit 6d58b99

Please sign in to comment.