-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
89 lines (72 loc) · 2 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
package main
import (
"context"
"log"
"os"
"os/signal"
"syscall"
"time"
"tests_app/internal/config"
"tests_app/internal/handler"
"tests_app/internal/repository"
"tests_app/internal/server"
"tests_app/internal/service"
"tests_app/pkg/hash"
"tests_app/pkg/token"
_ "github.com/lib/pq"
)
// @title API Server
// @version 0.1.0
// @description API Server
// @host localhost:8080
// @BasePath /api
// @accept json
// @produce json
// @schemes http
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
func main() {
config, err := config.Init("./")
if err != nil {
log.Fatalf("fatal error config file: %s", err.Error())
}
// wait mongo container
time.Sleep(3 * time.Second)
// connect to postgres
db, err := repository.NewPostgresDB(repository.Config{
Host: config.DB.Host,
Port: config.DB.Port,
Username: config.DB.Username,
DBName: config.DB.DBName,
SSLMode: config.DB.SSLMode,
Password: config.DB.Password,
})
if err != nil {
log.Fatalf("failed to initialize postgres: %s", err.Error())
}
hasher := hash.NewSHA512Hasher(config.PasswordSalt)
tokenManager := token.NewManager(config.JWT.AccessTokenTTL, config.JWT.SigningKey)
repos := repository.New(db)
services := service.New(repos, tokenManager, hasher)
handlers := handler.New(services, *config)
srv := new(server.Server)
c := handlers.InitApiRoutes()
handlers.InitFileRoutes(c)
go func() {
if err := srv.Run(config.Server, c); err != nil {
log.Fatalf("error occured while running http server: %s", err.Error())
}
}()
log.Print("App Started")
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT)
<-quit
log.Print("App Shutting Down")
if err := srv.Shutdown(context.Background()); err != nil {
log.Printf("error occured on server shutting down: %s", err.Error())
}
if err := db.Close(); err != nil {
log.Printf("error occured on postgres connection close: %s", err.Error())
}
}