-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (62 loc) · 1.97 KB
/
main.go
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
69
70
71
72
73
74
75
76
package main
import (
"encoding/json"
"log"
"os"
jwtware "github.com/gofiber/contrib/jwt"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/golang-jwt/jwt/v5"
"todoapp.com/application/services"
"todoapp.com/configuration"
"todoapp.com/domain/models"
"todoapp.com/infrastructure/connectors"
"todoapp.com/infrastructure/environments"
"todoapp.com/infrastructure/repositories"
"todoapp.com/presentation/controllers"
)
func main() {
error := environments.LoadEnv()
if error != nil {
log.Fatal("Error loading .env file.")
}
var config models.Config
error = configuration.LoadCfg(&config)
if error != nil {
log.Fatal("Error decoding loaded configuration file.")
}
encodedKey, error := os.ReadFile(config.PrivateKeyPath)
if error != nil {
log.Fatal("Error accessing OPENSSH public key.")
}
privateKey, error := jwt.ParseRSAPrivateKeyFromPEM(encodedKey)
if error != nil {
log.Fatal("Error parsing OPENSSH public key.")
}
app := fiber.New(fiber.Config{JSONEncoder: json.Marshal, JSONDecoder: json.Unmarshal})
db := connectors.Postgre{}.Connect()
// repositories
todosRepository := repositories.NewTodosRepository(db)
usersRepository := repositories.NewUsersRepository(db)
// services
todosService := services.NewTodosService(todosRepository)
usersService := services.NewUsersService(usersRepository)
loginService := services.NewLoginService(usersRepository, &config)
// controllers
todosController := controllers.NewTodosController(todosService)
usersController := controllers.NewUsersController(usersService)
loginController := controllers.NewLoginController(loginService)
// unsecured routes
loginController.Route(app.Group("/api", logger.New()))
// secured routes
app.Use(jwtware.New(jwtware.Config{
SigningKey: jwtware.SigningKey{
JWTAlg: jwtware.RS256,
Key: privateKey.Public(),
},
}))
api := app.Group("/api", logger.New())
todosController.Route(api)
usersController.Route(api)
app.Listen(":3000")
}