diff options
author | LarsVomMars | 2020-07-30 20:16:54 +0200 |
---|---|---|
committer | LarsVomMars | 2020-07-30 20:16:54 +0200 |
commit | f02c4054984fb8c12bfa4af9560a5b2be38810c0 (patch) | |
tree | d4519a92b739640e061847831d221e3e2488a83c | |
parent | 86c68c3648c94ca9f66f3eb408973368bed28681 (diff) |
Added views and server setup
-rw-r--r-- | src/handler/admin.ts | 12 | ||||
-rw-r--r-- | src/main.ts | 21 | ||||
-rw-r--r-- | src/public/js/login.js | 18 | ||||
-rw-r--r-- | src/public/js/setup.js | 21 | ||||
-rw-r--r-- | src/util/server.ts | 14 | ||||
-rw-r--r-- | src/views/admin.ejs | 32 | ||||
-rw-r--r-- | src/views/index.ejs | 18 | ||||
-rw-r--r-- | src/views/login.ejs | 24 |
8 files changed, 144 insertions, 16 deletions
diff --git a/src/handler/admin.ts b/src/handler/admin.ts new file mode 100644 index 0000000..a626e50 --- /dev/null +++ b/src/handler/admin.ts @@ -0,0 +1,12 @@ +import type { HandlerFunc, Context } from "https://deno.land/x/abc@master/mod.ts"; +import * as log from "https://deno.land/std/log/mod.ts"; +import { isAdmin } from "../util/user.ts"; +import { isSetup } from "../util/server.ts"; + +export const render: HandlerFunc = async (c: Context) => { + if (await isAdmin(c)) + return await c.render("./src/views/admin.ejs", { initial: false }); + else if (!(await isSetup())) + return await c.render("./src/views/admin.ejs", { initial: true }); + return c.redirect("/"); +} diff --git a/src/main.ts b/src/main.ts index 99f605c..bf5309c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,11 +3,13 @@ import { Application } from "https://deno.land/x/abc@master/mod.ts"; import type { Context } from "https://deno.land/x/abc@master/mod.ts"; import { renderFile } from "https://deno.land/x/dejs/mod.ts"; import * as groups from "./groups/index.ts"; +import * as log from "https://deno.land/std/log/mod.ts"; import { handlePath } from "./handler/fileView.ts"; +import { render as renderAdmin } from "./handler/admin.ts"; import DBController from "./db/DBController.ts"; +import { getCurrentUser } from "./util/user.ts"; -// Ugly solution -(async () => await new DBController().init())(); +new DBController().init().then(); const port = parseInt(Deno.env.get("PORT") || "8080"); const app = new Application(); @@ -18,16 +20,19 @@ app.renderer = { }, }; -app.static("/public/", "./src/public/"); // Manage static files -app.get("/", async (c: Context) => await c.render("./src/views/index.html")); // Render index on / +app.static("/", "./src/public/"); +app.get("/", async (c: Context) => await c.render("./src/views/index.ejs", { + files: undefined, + user: getCurrentUser(c) +})); app.get("/files/*", handlePath); app.get("/files/", handlePath); +app.get("/admin/", renderAdmin); + // Load groups dynamically -// deno-lint-ignore ban-ts-comment -// @ts-ignore -for (let groupName in groups) groups[groupName](app.group(groupName)); +for (const groupName in groups) (groups as { [key: string]: Function })[groupName](app.group(groupName)); app.start({ port }); -console.log(`Server listening on http://localhost:${port}`); +log.info(`Server listening on http://localhost:${port}`); diff --git a/src/public/js/login.js b/src/public/js/login.js new file mode 100644 index 0000000..681b02f --- /dev/null +++ b/src/public/js/login.js @@ -0,0 +1,18 @@ +const form = document.getElementById("login-form"); + +form.addEventListener('submit', async e => { + e.preventDefault(); + const username = document.getElementById("username").value; + const password = document.getElementById("password").value; + + const body = JSON.stringify({username, password}); + + const resp = await fetch("/user/login", { + method: "POST", + headers: {'Content-Type': 'application/json'}, + body, + }); + const res = await resp.json(); + if (res.success) location.replace("/"); + else alert("ASJHDOAISJDLKAJSD"); +})
\ No newline at end of file diff --git a/src/public/js/setup.js b/src/public/js/setup.js new file mode 100644 index 0000000..ef31e18 --- /dev/null +++ b/src/public/js/setup.js @@ -0,0 +1,21 @@ +const form = document.getElementById("setup-form"); + +form.addEventListener('submit', async e => { + e.preventDefault(); + const username = document.getElementById('username').value; + const email = document.getElementById('email').value; + const password = document.getElementById('password').value; + const admin = true; + + const body = JSON.stringify({username, email, password, admin}); + + const resp = await fetch("/user/register", { + method: "POST", + headers: {'Content-Type': 'application/json'}, + body, + }) + const res = await resp.json(); + if (res.success) location.replace("/user/login"); + else alert("ASJHDOAISJDLKAJSD"); + +})
\ No newline at end of file diff --git a/src/util/server.ts b/src/util/server.ts new file mode 100644 index 0000000..665c14f --- /dev/null +++ b/src/util/server.ts @@ -0,0 +1,14 @@ +import DBController from "../db/DBController.ts"; +import * as log from "https://deno.land/std/log/mod.ts"; + +const controller = new DBController(); + +export const isSetup = async (): Promise<boolean> => { + try { + const users = await controller.query("SELECT id FROM users"); + return users.length > 0 + } catch (e) { + log.error(e); + return false; + } +} diff --git a/src/views/admin.ejs b/src/views/admin.ejs new file mode 100644 index 0000000..009b029 --- /dev/null +++ b/src/views/admin.ejs @@ -0,0 +1,32 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" + content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> + <meta http-equiv="X-UA-Compatible" content="ie=edge"> + <title>Admin</title> +</head> +<body> +<% if (initial) { %> + <form id="setup-form"> + <div> + <label for="username">Username:</label> + <input type="text" name="username" id="username"> + </div> + <div> + <label for="email">Email:</label> + <input type="email" name="email" id="email"> + </div> + <div> + <label for="password">Password:</label> + <input type="password" name="password" id="password"> + </div> + <input type="submit" value="Setup"> + </form> + <script src="/js/setup.js"></script> +<% } else { %> + <!-- TODO: Render default admin page --> +<% } %> +</body> +</html> diff --git a/src/views/index.ejs b/src/views/index.ejs index 0e3ab11..daca490 100644 --- a/src/views/index.ejs +++ b/src/views/index.ejs @@ -1,11 +1,13 @@ <!DOCTYPE html> <html> - <head> - <meta charset="UTF-8" /> - <meta name="viewport" content="width=device-width" /> - <title>Index</title> - </head> - <body> - <% if (files) { %> <%= files %>! <% } %> - </body> +<head> + <meta charset="UTF-8"/> + <meta name="viewport" content="width=device-width"/> + <title>Index</title> +</head> +<body> +<% if (files) { %> + <%= files %> +<% } %> +</body> </html> diff --git a/src/views/login.ejs b/src/views/login.ejs new file mode 100644 index 0000000..607d099 --- /dev/null +++ b/src/views/login.ejs @@ -0,0 +1,24 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" + content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> + <meta http-equiv="X-UA-Compatible" content="ie=edge"> + <title>Login</title> +</head> +<body> +<form class="login-form" id="login-form"> + <div> + <label for="username">Username:</label> + <input type="text" id="username" placeholder="Username"> + </div> + <div> + <label for="password">Password:</label> + <input type="password" id="password" placeholder="Password"> + </div> + <input type="submit" value="Login"> +</form> +<script src="/js/login.js"></script> +</body> +</html> |