summaryrefslogtreecommitdiff
path: root/backend/src/main.zig
blob: ff9054005ce5c876be0c00b8988d95091134ba3e (plain)
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
const std = @import("std");
const backend = @import("backend");
const httpz = @import("httpz");
const db = @import("Database/Connection.zig");
const handler = @import("Handler.zig");
const tokens = @import("Authentication/Tokens.zig");
const redis = @import("Redis/Connection.zig");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const allocator = gpa.allocator();

    try tokens.Init();
   
    // PSQL connection
    try db.Connect(allocator);
    defer db.Disconnect();

    try redis.Connect();
    defer redis.Disconnect();

    var httpHandler = handler.Handler {
    };

    var server = try httpz.Server(*handler.Handler).init(allocator, .{
        .address = .all(6969),
    }, &httpHandler);

    defer {
        server.stop();
        server.deinit();
    }
   
    var router = try server.router(.{});
    router.get("/", index, .{});

    @import("API/WeaponsAPI.zig").RegisterEndpoints(router);
    @import("API/AuthenticationAPI.zig").RegisterEndpoints(router);

    try server.listen();
}

fn index(data: *handler.RequestData, _: *httpz.Request, res: *httpz.Response) !void {
    if (data.User == null) {
        try res.json(.{.status = "Unauthnticated"}, .{});
        return;
    }
    try res.json(data.User, .{});
}

test "TestRunner" {
    _ = @import("Models/RangedWeapon.zig");
    _ = @import("Models/User.zig");
    _ = @import("Authentication/Password.zig");
    _ = redis;
}