Skip to content

Commit 6d58b99

Browse files
committed
refactor: improve readability and maintainability
1 parent 7afb22c commit 6d58b99

File tree

2 files changed

+61
-62
lines changed

2 files changed

+61
-62
lines changed

app.js

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,36 @@ import webRoutes from "./routes/ui.routes.js";
55
import sqlite3 from "./plugins/db/db.js";
66
import serverkey from "./plugins/key/server.key.js";
77
import adminkey from "./plugins/key/admin.key.js";
8-
98
import pkg from "./package.json" assert { type: "json" };
10-
const appVersion = pkg.version;
119

12-
const buildApp = async function (serverOptions) {
13-
const app = await Fastify(serverOptions);
10+
const appVersion = pkg.version;
1411

15-
//register the sqlite database plugin
16-
await app.register(sqlite3);
17-
//register the server key plugin
18-
await app.register(serverkey);
19-
//register the admin key plugin
20-
await app.register(adminkey);
12+
const buildApp = async (serverOptions) => {
13+
const app = Fastify(serverOptions);
2114

22-
//register the admin api routes
23-
await app.register(adminRoutes, { prefix: "/v1/admin" });
24-
//register the authentication api routes
25-
await app.register(authRoutes, { prefix: "/v1/auth" });
26-
//register the frontend routes used for the UI. This is optional
27-
await app.register(webRoutes, { prefix: "/v1/web" });
15+
try {
16+
// Register the defaul authc plugins and routes
17+
await app
18+
.register(sqlite3)
19+
.register(serverkey)
20+
.register(adminkey)
21+
.register(adminRoutes, { prefix: "/v1/admin" })
22+
.register(authRoutes, { prefix: "/v1/auth" })
23+
.register(webRoutes, { prefix: "/v1/web" })
24+
.register(async (fastify, opts) => {
25+
fastify.get("/", async (req, reply) => {
26+
return `Welcome and hello 👋 - AuthCompanion is serving requests!
27+
Version: ${appVersion}`;
28+
});
29+
});
2830

29-
//register a default route welcoming the user
30-
await app.register(async function defaultRoute(fastify, opts) {
31-
fastify.get("/", async (req, reply) => {
32-
return `Welcome and hello 👋 - AuthCompanion is serving requests!
33-
Version: ${appVersion}
34-
`;
35-
});
36-
});
31+
await app.ready();
3732

38-
return app;
33+
return app;
34+
} catch (err) {
35+
console.log("Error building the app:", err);
36+
throw err; // Rethrow the error to indicate app initialization failure
37+
}
3938
};
4039

4140
export default buildApp;

server.js

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,54 @@
11
import buildApp from "./app.js";
22
import config from "./config.js";
33

4-
const server = await buildApp({
5-
logger: true,
6-
forceCloseConnections: true,
7-
});
8-
9-
/**
10-
* Let's run the AuthCompanion server!
11-
*/
12-
function startServer() {
13-
server.listen(
14-
{
4+
async function startServer() {
5+
try {
6+
const server = await buildApp({
7+
logger: true,
8+
forceCloseConnections: true,
9+
});
10+
11+
await server.listen({
1512
port: config.AUTHPORT,
1613
host: "0.0.0.0",
17-
},
18-
(err) => {
19-
if (err) {
20-
console.log("Error starting server:", err);
21-
process.exit(1);
22-
}
23-
}
24-
);
14+
});
15+
16+
printStartupMessage(config.AUTHPORT);
17+
setupGracefulShutdown(server);
18+
} catch (error) {
19+
console.error("Error starting server:", error);
20+
process.exit(1);
21+
}
22+
}
23+
24+
function printStartupMessage(port) {
2525
console.log(`
2626
###########################################################
2727
The AuthCompanion Server has started
2828
29-
🖥️ Client UI on: http://localhost:${config.AUTHPORT}/v1/web/login
30-
🚀 Admin UI on: http://localhost:${config.AUTHPORT}/v1/admin/login
29+
🖥️ Client UI on: http://localhost:${port}/v1/web/login
30+
🚀 Admin UI on: http://localhost:${port}/v1/admin/login
3131
3232
###########################################################
3333
`);
34-
console.log("Use CTRL-C to shutdown AuthCompanion");
34+
console.log("Use CTRL-C to shut down AuthCompanion");
3535
}
3636

37-
//setup for gracefully exiting the AuthCompanion server
38-
async function handleSignal() {
39-
try {
40-
await server.close();
41-
console.log(`
42-
AuthCompanion has exited, Good bye👋`);
43-
process.exit(0);
44-
} catch (error) {
45-
console.log("Error shutting down server:", error);
46-
process.exit(1);
47-
}
48-
}
37+
function setupGracefulShutdown(server) {
38+
const handleSignal = async () => {
39+
try {
40+
await server.close();
41+
console.log("AuthCompanion has exited. Goodbye! 👋");
42+
process.exit(0);
43+
} catch (error) {
44+
console.error("Error shutting down server:", error);
45+
process.exit(1);
46+
}
47+
};
4948

50-
process.on("SIGTERM", handleSignal);
51-
process.on("SIGINT", handleSignal);
52-
process.on("SIGUSR2", handleSignal);
49+
process.on("SIGTERM", handleSignal);
50+
process.on("SIGINT", handleSignal);
51+
process.on("SIGUSR2", handleSignal);
52+
}
5353

5454
startServer();

0 commit comments

Comments
 (0)