-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.du
68 lines (50 loc) · 1.68 KB
/
run.du
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
import Hashlib;
from "framework/app.du" import App;
from "framework/response.du" import JsonResponse, ErrorResponse;
from "framework/session.du" import SessionHandler;
from "database/sqlite.du" import SqliteConnection;
const app = App("0.0.0.0", 8080, true);
app.key = '12345';
app.database = SqliteConnection("users.db");
app.addSessionHandler(SessionHandler(app));
def getStaticContent(path) {
with("static/{}".format(path), "r") {
return file.read();
}
}
app.get("/", def (request) => {
return getStaticContent("views/index.html");
});
app.post("/login", def (request) => {
const username = request.body.get("username", false);
const password = request.body.get("password", false);
if (!username or !password) {
return ErrorResponse("Invalid username or password", 400);
}
const user = app.database.execute("SELECT rowid, password FROM users WHERE username = ?", [
username
]);
if (!user) {
return ErrorResponse("Invalid username or password", 401);
}
const [userId, hashedPassword] = user[0];
if (!Hashlib.bcryptVerify(password, hashedPassword)) {
return ErrorResponse("Invalid username or password", 401);
}
request.session["logged_in"] = true;
return "Hello {}!".format(username);
});
app.get("/home", def (request) => {
if (!request.session.get("logged_in", false)) {
return ErrorResponse("You are not authorised!", 401);
}
return getStaticContent("views/home.html");
});
app.get("/logout", def (request) => {
request.session["logged_in"] = false;
return "Logged out!";
});
app.get("/api/test", def (request) => {
return JsonResponse([1, 2, 3]);
});
app.start();