2022-07-04 12:20:24 +05:30
|
|
|
import express, { Response, Request, Express } from "express";
|
|
|
|
import dotenv from "dotenv";
|
2022-07-08 11:12:21 +05:30
|
|
|
import BinRouter from "./routes/bin";
|
|
|
|
import { Client, Pool } from "pg";
|
|
|
|
import { createDataDir, errorHandler, logError, logSuccess } from "./utils";
|
2022-07-04 12:20:24 +05:30
|
|
|
|
|
|
|
dotenv.config();
|
2022-07-08 11:12:21 +05:30
|
|
|
createDataDir();
|
|
|
|
|
|
|
|
// init db
|
|
|
|
const pool = new Pool();
|
|
|
|
const client = new Client();
|
|
|
|
|
|
|
|
client
|
|
|
|
.connect()
|
|
|
|
.then(() => {
|
|
|
|
logSuccess("Connected to database!");
|
|
|
|
})
|
|
|
|
.catch((err: Error) => {
|
|
|
|
logError(`Could not connect to database! \n\n${err.message}`);
|
|
|
|
});
|
2022-07-04 12:20:24 +05:30
|
|
|
|
2022-07-08 11:12:21 +05:30
|
|
|
// start server
|
|
|
|
const app: Express = express();
|
2022-07-04 12:20:24 +05:30
|
|
|
const port = process.env.PORT;
|
|
|
|
|
2022-07-08 11:12:21 +05:30
|
|
|
app.use(express.text());
|
|
|
|
|
2022-07-04 12:20:24 +05:30
|
|
|
app.get("/", (req: Request, res: Response) => {
|
|
|
|
res.send("Hello, World!");
|
|
|
|
});
|
2022-07-08 11:12:21 +05:30
|
|
|
app.use("/bin/", BinRouter(pool));
|
2022-07-04 12:20:24 +05:30
|
|
|
|
2022-07-08 11:12:21 +05:30
|
|
|
app.use(errorHandler);
|
2022-07-04 12:20:24 +05:30
|
|
|
app.listen(port, () => {
|
2022-07-08 11:12:21 +05:30
|
|
|
logSuccess(`Server started on port ${port}`);
|
2022-07-04 12:20:24 +05:30
|
|
|
});
|