This repository has been archived by the owner on Jan 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
57 lines (46 loc) · 1.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const express = require("express");
const morgan = require("morgan");
const { makePGClient } = require("./clients/pg");
const { makeIndexRouter } = require("./routes");
const { makeCreateRouter } = require("./routes/create");
const { makeDeleteRouter } = require("./routes/delete");
const { makeReadRouter } = require("./routes/read.js");
const { makeUpdateRouter } = require("./routes/update");
const SERVER_PORT = process.env.PORT;
const CGAAP_BASE_PATH = process.env.CGAAP_BASE_PATH || "/";
const DB_HOST = process.env.DB_HOST;
const DB_NAME = process.env.DB_NAME;
const DB_PASSWORD = process.env.DB_PASSWORD;
const DB_PORT = process.env.DB_PORT;
const DB_USER = process.env.DB_USER;
const PG_CONNECTION_STRING = `postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}`;
const pgClient = makePGClient(PG_CONNECTION_STRING);
pgClient
.init()
.then(() => {
const context = {
basePath: CGAAP_BASE_PATH,
};
const app = express();
// Configuring express template engine
app.set("view engine", "pug");
// Middlewares
app.use(express.urlencoded({ extended: true }));
app.use(morgan("dev"));
// Create main router
const router = express.Router();
// Routes
router.use("/", makeIndexRouter(context, pgClient));
router.use("/create", makeCreateRouter(context, pgClient));
router.use("/read", makeReadRouter(context, pgClient));
router.use("/update", makeUpdateRouter(context, pgClient));
router.use("/delete", makeDeleteRouter(context, pgClient));
app.use(CGAAP_BASE_PATH, router);
// Start server
app.listen(SERVER_PORT, () => {
console.log(
`Application started, listening on port ${SERVER_PORT} with base path ${CGAAP_BASE_PATH}`
);
});
})
.catch((e) => console.log("Error starting the application", e));