-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
111 lines (93 loc) · 3.66 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"fmt"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
"strconv"
"voting-system/controller"
"voting-system/middleware"
"voting-system/pkg/configuration"
"voting-system/pkg/database"
"voting-system/pkg/exception"
"voting-system/repository"
"voting-system/service"
)
func main() {
config := configuration.NewConfigurationImpl(".env")
// Db config
poolMinConfig, err := strconv.Atoi(config.Get("MYSQL_POOL_MIN"))
exception.PanicIfError(err)
poolMaxConfig, err := strconv.Atoi(config.Get("MYSQL_POOL_MAX"))
exception.PanicIfError(err)
dbConfig := configuration.DbConfig{
User: config.Get("MYSQL_USER"),
Password: config.Get("MYSQL_PASSWORD"),
Host: config.Get("MYSQL_HOST"),
Port: config.Get("MYSQL_PORT"),
Database: config.Get("MYSQL_DATABASE"),
PoolMin: poolMinConfig,
PoolMax: poolMaxConfig,
}
// JWT Config
expirationConfig, err := strconv.Atoi(config.Get("JWT_EXPIRATION_DURATION"))
exception.PanicIfError(err)
closedAt, err := strconv.Atoi(config.Get("CLOSED_AT"))
exception.PanicIfError(err)
startedAt, err := strconv.Atoi(config.Get("CLOSED_AT"))
exception.PanicIfError(err)
jwtConfig := configuration.JWTConfig{
ApplicationName: config.Get("JWT_APPLICATION_NAME"),
SignatureKey: []byte(config.Get("JWT_SIGNATURE_KEY")),
ExpirationDuration: expirationConfig,
ClosedAt: int64(closedAt),
StartedAt: int64(startedAt),
}
// Mail Config
mailEmail := config.Get("MAIL_EMAIL")
mailName := config.Get("MAIL_NAME")
smtpPort, err := strconv.Atoi(config.Get("MAIL_SMTP_PORT"))
exception.PanicIfError(err)
mailConfig := configuration.MailConfig{
SmtpHost: config.Get("MAIL_SMTP_HOST"),
Name: fmt.Sprintf("%s <%s>", mailName, mailEmail),
Email: mailEmail,
Password: config.Get("MAIL_PASSWORD"),
SmtpPort: smtpPort,
}
db := database.NewDatabase(dbConfig)
validate := validator.New()
mailService := service.NewMailServiceImpl(mailConfig)
authService := service.NewAuthServiceImpl(jwtConfig)
authMiddleware := middleware.NewAuthMiddleware(authService)
voterRepository := repository.NewVoterRepositoryImpl()
voterService := service.NewVoterServiceImpl(db, validate, voterRepository, authService)
voterController := controller.NewVoterController(voterService, authService, mailService)
adminRepository := repository.NewAdminRepositoryImpl()
adminService := service.NewAdminServiceImpl(db, validate, adminRepository)
adminController := controller.NewAdminController(adminService, voterService, authService, mailService)
candidateRepository := repository.NewCandidateRepositoryImpl()
candidateService := service.NewCandidateServiceImpl(db, candidateRepository)
candidateController := controller.NewCandidateController(candidateService)
app := fiber.New(configuration.NewFiberConfig())
app.Use(cors.New())
app.Use(logger.New())
app.Use(recover.New())
api := app.Group("/api")
admins := api.Group("/admins", authMiddleware)
voters := api.Group("/voters")
candidates := api.Group("/candidates")
api.Post("/secret-sessions", adminController.Login)
admins.Post("/", adminController.Create)
admins.Get("/", adminController.List)
admins.Get("/profile", adminController.Get)
admins.Delete("/:id", adminController.Delete)
admins.Post("/generate-token", adminController.GenerateVoterToken)
voters.Get("/:token", voterController.Login)
voters.Post("/:token", voterController.Vote)
candidates.Get("/count", candidateController.Count)
err = app.ListenTLS(":8080", "./cert.pem", "./cert.key")
exception.PanicIfError(err)
}