aboutsummaryrefslogtreecommitdiffstats
path: root/backend
diff options
context:
space:
mode:
authorMaks Jopek <maksymilian.jopek@prym-soft.pl>2021-04-04 01:53:24 +0200
committerMaks Jopek <maksymilian.jopek@prym-soft.pl>2021-04-04 01:53:24 +0200
commit292257d627bd1dfeafd772ac6457e2f54c2b73ba (patch)
treeb7962648b87ab709e8529075ce912950019adaee /backend
parenteee4ee32dd667ed6efd31ddcf8f64217b7e16b60 (diff)
downloadfia-292257d627bd1dfeafd772ac6457e2f54c2b73ba.tar.gz
fia-292257d627bd1dfeafd772ac6457e2f54c2b73ba.tar.zst
fia-292257d627bd1dfeafd772ac6457e2f54c2b73ba.zip
One step closer to the end
Diffstat (limited to 'backend')
-rw-r--r--backend/ts/api/login.ts49
-rw-r--r--backend/ts/helpers.ts18
-rw-r--r--backend/ts/index.ts22
-rw-r--r--backend/tsconfig.json9
4 files changed, 98 insertions, 0 deletions
diff --git a/backend/ts/api/login.ts b/backend/ts/api/login.ts
new file mode 100644
index 0000000..5b38c62
--- /dev/null
+++ b/backend/ts/api/login.ts
@@ -0,0 +1,49 @@
+import { Request, Response } from "express";
+import { makeQuery } from "../helpers";
+
+interface Colors {
+ blue: number;
+ red: number;
+ green: number;
+ yellow: number;
+ 0: number;
+ 1: number;
+ 2: number;
+ 3: number;
+}
+
+interface fiaTable {
+ id: number;
+ data: Array<object>;
+ started: boolean;
+}
+
+const Color: Colors = {
+ blue: 0,
+ red: 1,
+ green: 2,
+ yellow: 3,
+ 0: 0, 1: 1, 2: 2, 3: 3
+}
+
+export default async function apiLogin(req: Request, res: Response) {
+ if (req.session === undefined) {
+ res.send(/* html */`<script>alert("Something went wrong, try to reload the page")</script>`);
+ return;
+ }
+ req.session.uid = req.session.id;
+ let c = await makeQuery("SELECT * FROM `fia` WHERE `started`= false LIMIT 1", []),
+ row: fiaTable;
+ if (c === "") {
+ let data = { id: req.session.uid, color: Color.blue };
+ makeQuery("INSERT INTO `fia`(`data`, `started`) VALUES (?, ?)", [JSON.stringify(data), "0"]);
+ }
+ else {
+ row = JSON.parse(c);
+ row.data.push({ id: req.session.uid, color: Color[row.data.length as keyof Colors] });
+ makeQuery("UPDATE `fia` SET `data`=? WHERE `id`=?", [JSON.stringify(row.data), row.id.toString()]);
+ if(row.data.length === 4)
+ makeQuery("UPDATE `fia` SET `started` = 1 WHERE `id`=?", [row.id.toString()]);
+ }
+ res.send("[1, 2, 3]");
+}
diff --git a/backend/ts/helpers.ts b/backend/ts/helpers.ts
new file mode 100644
index 0000000..984ce2d
--- /dev/null
+++ b/backend/ts/helpers.ts
@@ -0,0 +1,18 @@
+import fetch from "node-fetch";
+
+export async function makeQuery (query: string, params: Array<string>): Promise<string> {
+ return await (
+ await fetch("https://jopek.eu/maks/szkola/apkKli/fiaFiles/fia.php", {
+ method: "POST",
+ headers: {
+ Accept: "application/json",
+ "Content-Type": "application/json"
+ },
+ body: JSON.stringify({
+ apiKey: process.env.API_KEY,
+ stmt: query,
+ params: params
+ })
+ })
+ ).text();
+} \ No newline at end of file
diff --git a/backend/ts/index.ts b/backend/ts/index.ts
new file mode 100644
index 0000000..fd8f8e1
--- /dev/null
+++ b/backend/ts/index.ts
@@ -0,0 +1,22 @@
+import express from "express";
+import login from "./api/login";
+import session from "express-session";
+import * as dotenv from "dotenv";
+
+dotenv.config();
+const app = express();
+const PORT = 8000;
+
+if (!process.env.SESSION_KEY)
+ throw Error("SESSION_KEY is undefined");
+
+app.use(session({ secret: process.env.SESSION_KEY, cookie: { maxAge: 60000 } }))
+app.use("/", express.static("./public"));
+
+app.get("/favicon.ico", (req, res) => res.redirect("https://lukesmith.xyz/favicon.ico"));
+app.get("/blank", (req, res) => res.send(""));
+app.post("/login", (req, res, next) => login(req, res));
+
+app.listen(PORT, () => {
+ console.log(`[server]: Server is running at https://localhost:${PORT}`);
+});
diff --git a/backend/tsconfig.json b/backend/tsconfig.json
new file mode 100644
index 0000000..bb9a7dd
--- /dev/null
+++ b/backend/tsconfig.json
@@ -0,0 +1,9 @@
+{
+ "extends": "../tsconfig.json",
+ "include": [
+ "ts/**/*"
+ ],
+ "compilerOptions": {
+ "outDir": "./js"
+ }
+} \ No newline at end of file