-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·137 lines (107 loc) · 2.85 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env node
// https://www.youtube.com/watch?v=pldDuqnPP1s
const mariadb = require("mariadb");
const consola = require("consola");
const express = require("express");
const bodyParser = require("body-parser");
const dbSettings = {
host: "rikardo-db",
user: "rikardo",
password: "passw0rd",
database: "rikardo_todo",
connectionLimit: 5,
connectTimeout: 5000,
};
const serverSettings = {
host: "0.0.0.0",
port: 9001,
};
async function getDbConn() {
return await mariadb.createConnection(dbSettings);
}
async function queryDb(sql, args) {
let conn;
try {
conn = await getDbConn();
const result = await conn.query(sql, args);
conn.end();
return result;
} catch (e) {
if (conn) {
conn.end();
}
throw e;
}
}
async function testDatabaseConnection() {
consola.info("|> Spajam se na MariaDB server... ");
let conn;
let success = false;
try {
const query = await queryDb("SELECT 1 as val");
success = true;
consola.success("Uspjeh!");
} catch (err) {
consola.error(String(err));
} finally {
if (conn) {
conn.end();
}
return success;
}
}
async function startApp() {
const app = express();
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
await queryDb(
"create table if not exists `ricardo_todo` (`id` int unsigned not null auto_increment, `description` text not null, `done` tinyint(1) not null, primary key (`id`) )"
);
app.get("/", async (req, res) => {
const tasks = [...(await queryDb("select * from `ricardo_todo`"))];
res.render("index", { tasks });
});
app.post("/task", async (req, res) => {
const { description } = req.body || {};
if (description) {
await queryDb(
"insert into `ricardo_todo` (description, done) values (?, 0)",
[description]
);
}
res.redirect("/");
});
app.post("/task/complete", async (req, res) => {
const { id } = req.body || {};
if (id) {
await queryDb("update `ricardo_todo` set done = ? where id = ?", [1, id]);
}
res.redirect("/");
});
app.post("/task/uncomplete", async (req, res) => {
const { id } = req.body || {};
if (id) {
await queryDb("update `ricardo_todo` set done = ? where id = ?", [0, id]);
}
res.redirect("/");
});
app.post("/task/delete", async (req, res) => {
const { id } = req.body || {};
if (id) {
await queryDb("delete from `ricardo_todo` where id = ?", [id]);
}
res.redirect("/");
});
app.listen(serverSettings.port, serverSettings.host, () => {
consola.info(
`Aplikacija je spremna i vrti se na http://${serverSettings.host}:${serverSettings.port}`
);
});
}
testDatabaseConnection().then((success) => {
if (!success) {
consola.warn("Prvo je potrebno osposobiti pristup bazi!");
process.exit(1);
}
startApp();
});