summaryrefslogtreecommitdiff
path: root/internal/router.go
blob: 79f8d82f181bdfc7fb55a4c6dcca62317d492df6 (plain) (blame)
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
package internal

import (
	"context"
	"log"

	"git.devcara.com/hkgi/database"
	"git.devcara.com/hkgi/internal/routers"
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/basicauth"
	"golang.org/x/crypto/bcrypt"
)

type User struct {
	Username string
	Password string
}

func SetupRoutes(app *fiber.App) {
	app.Use(basicauth.New(basicauth.Config{
		Authorizer: func(username, password string) bool {
			db,err := database.DB.Acquire()

			if (err != nil) {
				log.Fatal(err.Error())
			}

			var user User
			err = db.QueryRow(context.Background(), "SELECT username, password FROM steads WHERE username=$1", username).Scan(&user)

			if (err != nil) {
				return false;
			}
			
			err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))

			if (err == nil) {
				return true
			}

			return false
		},
		Unauthorized: func(c *fiber.Ctx) error {
			return c.SendStatus(403)
		},
	}))

	routers.SetupHkgiRoutes(app)
}