diff options
author | Akshay <[email protected]> | 2024-11-13 22:34:03 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2024-11-13 22:34:03 +0000 |
commit | bbe0413ae1aad2516745f6f279225d2aea3555af (patch) | |
tree | 609fc751fb153f9fc0e3fbf111b644d5df5b1025 /src/index.js | |
parent | 81adffe4c1ecd2f0260ec08e73760505dfe4edaa (diff) |
add login and users and all the pizzazz
Diffstat (limited to 'src/index.js')
-rw-r--r-- | src/index.js | 44 |
1 files changed, 16 insertions, 28 deletions
diff --git a/src/index.js b/src/index.js index 6885ee5..6296534 100644 --- a/src/index.js +++ b/src/index.js | |||
@@ -1,35 +1,13 @@ | |||
1 | const express = require("express"); | 1 | const express = require("express"); |
2 | const rateLimit = require("express-rate-limit"); | ||
2 | const path = require("node:path"); | 3 | const path = require("node:path"); |
3 | const geddit = require("./geddit.js"); | 4 | const geddit = require("./geddit.js"); |
4 | const { Database } = require("bun:sqlite"); | 5 | const cookieParser = require("cookie-parser"); |
5 | |||
6 | const db = new Database("readit.db"); | ||
7 | |||
8 | const createUsers = db.query(` | ||
9 | CREATE TABLE IF NOT EXISTS users ( | ||
10 | id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
11 | username TEXT UNIQUE, | ||
12 | password_hash TEXT | ||
13 | ) | ||
14 | `); | ||
15 | |||
16 | createUsers.run(); | ||
17 | |||
18 | const createSubs = db.query(` | ||
19 | CREATE TABLE IF NOT EXISTS subscriptions ( | ||
20 | id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
21 | user_id INTEGER, | ||
22 | subreddit TEXT, | ||
23 | FOREIGN KEY(user_id) REFERENCES users(id), | ||
24 | UNIQUE(user_id, subreddit) | ||
25 | ) | ||
26 | `); | ||
27 | |||
28 | createSubs.run(); | ||
29 | |||
30 | module.exports = { db }; | ||
31 | |||
32 | const app = express(); | 6 | const app = express(); |
7 | const hasher = new Bun.CryptoHasher("sha256", "secret-key"); | ||
8 | const JWT_KEY = hasher.update(Math.random().toString()).digest("hex"); | ||
9 | |||
10 | module.exports = { JWT_KEY }; | ||
33 | 11 | ||
34 | app.set("views", path.join(__dirname, "views")); | 12 | app.set("views", path.join(__dirname, "views")); |
35 | app.set("view engine", "pug"); | 13 | app.set("view engine", "pug"); |
@@ -38,6 +16,16 @@ const routes = require("./routes/index"); | |||
38 | app.use(express.json()); | 16 | app.use(express.json()); |
39 | app.use(express.urlencoded({ extended: true })); | 17 | app.use(express.urlencoded({ extended: true })); |
40 | app.use(express.static(path.join(__dirname, "public"))); | 18 | app.use(express.static(path.join(__dirname, "public"))); |
19 | app.use(cookieParser()); | ||
20 | app.use( | ||
21 | rateLimit({ | ||
22 | windowMs: 15 * 60 * 1000, | ||
23 | max: 100, | ||
24 | message: "Too many requests from this IP, please try again later.", | ||
25 | standardHeaders: true, | ||
26 | legacyHeaders: false, | ||
27 | }), | ||
28 | ); | ||
41 | app.use("/", routes); | 29 | app.use("/", routes); |
42 | 30 | ||
43 | const port = process.env.READIT_PORT; | 31 | const port = process.env.READIT_PORT; |