diff options
author | LarsVomMars | 2020-07-12 18:07:59 +0200 |
---|---|---|
committer | LarsVomMars | 2020-07-12 18:07:59 +0200 |
commit | e39dbbaeaee6d3dcf40bf74aba932aacfa34fa18 (patch) | |
tree | 09a40a99e40f765f569d14a7ce678df9135f27a5 | |
parent | 7023dc2a6920aa6389ac2b28bcdbaacdb4413e00 (diff) |
Example code
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | src/db/DBController.ts | 31 | ||||
-rw-r--r-- | src/db/connector.ts | 17 | ||||
-rw-r--r-- | src/db/tables.ts | 1 | ||||
-rw-r--r-- | src/db/user.ts | 6 | ||||
-rw-r--r-- | src/groups/index.ts | 1 | ||||
-rw-r--r-- | src/groups/user.ts | 6 | ||||
-rw-r--r-- | src/handler/.gitkeep | 0 | ||||
-rw-r--r-- | src/handler/user.ts | 4 | ||||
-rw-r--r-- | src/main.ts | 16 | ||||
-rw-r--r-- | src/router/.gitkeep | 0 |
11 files changed, 64 insertions, 20 deletions
@@ -1,4 +1,4 @@ .idea/ .vscode/* !.vscode/launch.json -src/.env
\ No newline at end of file +.env
\ No newline at end of file diff --git a/src/db/DBController.ts b/src/db/DBController.ts new file mode 100644 index 0000000..67c6afc --- /dev/null +++ b/src/db/DBController.ts @@ -0,0 +1,31 @@ +import { Client } from "https://deno.land/x/mysql/mod.ts"; +import tables from "./tables.ts"; + +export default class DBController { + async init() { + const conn = await this.connect(); + try { + tables.forEach((table) => conn.execute(table)); + console.log("Tables created"); + } catch (e) { + console.error("Could not create tables"); + throw e; + } finally { + conn.close(); + } + } + + async connect(): Promise<Client> { + try { + return await new Client().connect({ + hostname: Deno.env.get("DBHost"), + username: Deno.env.get("DBUser"), + db: Deno.env.get("DBName"), + password: Deno.env.get("DBPassword"), + }); + } catch (e) { + console.error("Could not connect to database"); + throw e; + } + } +} diff --git a/src/db/connector.ts b/src/db/connector.ts deleted file mode 100644 index 6b9bfec..0000000 --- a/src/db/connector.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { Client } from "https://deno.land/x/mysql/mod.ts"; - -export default class Connector { - async connect(): Promise<Client> { - try { - return await new Client().connect({ - hostname: Deno.env.get("DBHost"), - username: Deno.env.get("DBUser"), - db: Deno.env.get("DBName"), - password: Deno.env.get("DBPassword"), - }); - } catch (e) { - console.error("Could not connect to database!"); - throw e; - } - } -} diff --git a/src/db/tables.ts b/src/db/tables.ts new file mode 100644 index 0000000..d6d1738 --- /dev/null +++ b/src/db/tables.ts @@ -0,0 +1 @@ +export default []; diff --git a/src/db/user.ts b/src/db/user.ts new file mode 100644 index 0000000..c245b5f --- /dev/null +++ b/src/db/user.ts @@ -0,0 +1,6 @@ +import DBController from "./DBController.ts"; + +class User extends DBController { +} + +export default new User(); diff --git a/src/groups/index.ts b/src/groups/index.ts new file mode 100644 index 0000000..403a035 --- /dev/null +++ b/src/groups/index.ts @@ -0,0 +1 @@ +export { default as user } from "./user.ts"; diff --git a/src/groups/user.ts b/src/groups/user.ts new file mode 100644 index 0000000..ae6d2ad --- /dev/null +++ b/src/groups/user.ts @@ -0,0 +1,6 @@ +import type { Group, Context } from "https://deno.land/x/abc@v1/mod.ts"; +import * as handlers from "../handler/user.ts"; + +export default function (g: Group) { + g.get("/:name", handlers.index); +} diff --git a/src/handler/.gitkeep b/src/handler/.gitkeep deleted file mode 100644 index e69de29..0000000 --- a/src/handler/.gitkeep +++ /dev/null diff --git a/src/handler/user.ts b/src/handler/user.ts new file mode 100644 index 0000000..33b96a2 --- /dev/null +++ b/src/handler/user.ts @@ -0,0 +1,4 @@ +import type { HandlerFunc, Context } from "https://deno.land/x/abc@v1/mod.ts"; +import db from "../db/user.ts"; + +export const index: HandlerFunc = async (c: Context) => c.params.name; diff --git a/src/main.ts b/src/main.ts index e87e14b..5a4e2d3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,12 @@ -import { Application, Context } from "https://deno.land/x/abc@v1/mod.ts"; -import { renderFile } from "https://deno.land/x/dejs/mod.ts"; import "https://deno.land/x/dotenv/load.ts"; +import { Application } from "https://deno.land/x/abc@v1/mod.ts"; +import type { Context } from "https://deno.land/x/abc@v1/mod.ts"; +import { renderFile } from "https://deno.land/x/dejs/mod.ts"; +import * as groups from "./groups/index.ts"; +import DBController from "./db/DBController.ts"; + +// Ugly solution +(async () => await (new DBController()).init())(); const port = parseInt(Deno.env.get("PORT") || "8080"); const app = new Application(); @@ -14,5 +20,11 @@ app.renderer = { app.static("/", "./src/public/"); // Manage static files app.get("/", async (c: Context) => await c.render("./src/public/index.html")); // Render index on / +// Load groups dynamically +for (let groupName in groups) { + // @ts-ignore + groups[groupName](app.group(groupName)); +} + app.start({ port }); console.log(`Server listening on http://localhost:${port}`); diff --git a/src/router/.gitkeep b/src/router/.gitkeep deleted file mode 100644 index e69de29..0000000 --- a/src/router/.gitkeep +++ /dev/null |