setup application, server, and configs
This commit is contained in:
48
src/application.ts
Normal file
48
src/application.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { serverConfig } from "@/configs/server.config";
|
||||
import compression from "compression";
|
||||
import cors from "cors";
|
||||
import express from "express";
|
||||
import helmet from "helmet";
|
||||
|
||||
export class Application {
|
||||
private readonly _app: express.Application;
|
||||
|
||||
public constructor() {
|
||||
this._app = express();
|
||||
}
|
||||
|
||||
public initializeMiddlewares() {
|
||||
this._app.use(helmet());
|
||||
this._app.use(cors());
|
||||
this._app.use(compression());
|
||||
this._app.use(express.json());
|
||||
this._app.use(express.urlencoded());
|
||||
}
|
||||
|
||||
public initializeRouters() {
|
||||
// this._app.use("/countries");
|
||||
// this._app.use("/cities");
|
||||
// this._app.use("/airlines");
|
||||
// this._app.use("/airports");
|
||||
// this._app.use("/flights");
|
||||
// this._app.use("/hotel-facilities");
|
||||
// this._app.use("/hotels");
|
||||
// this._app.use("/transportations");
|
||||
// this._app.use("/packages");
|
||||
}
|
||||
|
||||
public initializeErrorHandlers() {}
|
||||
|
||||
public run() {
|
||||
const port = serverConfig.port;
|
||||
const host = serverConfig.host;
|
||||
|
||||
this._app.listen(port, host, (err) => {
|
||||
if (err) {
|
||||
console.log(`Failed to listen server: ${err.message}`);
|
||||
} else {
|
||||
console.log(`Server listening at ${host}:${port}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
20
src/configs/_env.ts
Normal file
20
src/configs/_env.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import z from "zod";
|
||||
|
||||
export const _env = z
|
||||
.object({
|
||||
SERVER_HOST: z.string("Must be string."),
|
||||
SERVER_PORT: z.coerce
|
||||
.number("Must be number.")
|
||||
.int("Must be integer.")
|
||||
.min(0, "Min 0."),
|
||||
|
||||
DATABASE_HOST: z.string("Must be string.").nonempty("Must not empty."),
|
||||
DATABASE_PORT: z.coerce
|
||||
.number("Must be number.")
|
||||
.int("Must be integer.")
|
||||
.min(0, "Min 0."),
|
||||
DATABASE_USERNAME: z.string("Must be string.").nonempty("Must not empty."),
|
||||
DATABASE_PASSWORD: z.string("Must be string.").nonempty("Must not empty."),
|
||||
DATABASE_NAME: z.string("Must be string.").nonempty("Must not empty."),
|
||||
})
|
||||
.parse(Bun.env);
|
||||
9
src/configs/database.config.ts
Normal file
9
src/configs/database.config.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { _env } from "@/configs/_env";
|
||||
|
||||
export const databaseConfig = {
|
||||
host: _env.DATABASE_HOST,
|
||||
port: _env.DATABASE_PORT,
|
||||
username: _env.DATABASE_USERNAME,
|
||||
password: _env.DATABASE_PASSWORD,
|
||||
name: _env.DATABASE_NAME,
|
||||
} as const;
|
||||
6
src/configs/server.config.ts
Normal file
6
src/configs/server.config.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { _env } from "@/configs/_env";
|
||||
|
||||
export const serverConfig = {
|
||||
host: _env.SERVER_HOST,
|
||||
port: _env.SERVER_PORT,
|
||||
} as const;
|
||||
11
src/main.ts
11
src/main.ts
@@ -1,11 +0,0 @@
|
||||
import express from "express";
|
||||
|
||||
const app = express();
|
||||
|
||||
app.listen(3000, "localhost", (err) => {
|
||||
if (err) {
|
||||
console.log("Error starting server:", err);
|
||||
} else {
|
||||
console.log("Server is running at http://localhost:3000");
|
||||
}
|
||||
});
|
||||
20
src/server.ts
Normal file
20
src/server.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { Application } from "@/application";
|
||||
|
||||
process.on("uncaughtException", (err) => {
|
||||
let message = `Uncaught exception: ${err.message || err}`;
|
||||
if (err.stack) {
|
||||
message += `\n- Stack: ${err.stack}`;
|
||||
}
|
||||
|
||||
console.warn(message);
|
||||
});
|
||||
|
||||
process.on("unhandledRejection", (reason) => {
|
||||
console.warn(`Unhandled rejection: ${reason}`);
|
||||
});
|
||||
|
||||
const application = new Application();
|
||||
application.initializeMiddlewares();
|
||||
application.initializeRouters();
|
||||
application.initializeErrorHandlers();
|
||||
application.run();
|
||||
Reference in New Issue
Block a user