-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (68 loc) · 2.24 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
package main
import (
"fmt"
"log"
"net/http"
"time"
"github.com/eibrahimarisoy/car_rental/pkg/config"
db "github.com/eibrahimarisoy/car_rental/pkg/database"
graceful "github.com/eibrahimarisoy/car_rental/pkg/graceful"
logger "github.com/eibrahimarisoy/car_rental/pkg/logging"
router "github.com/eibrahimarisoy/car_rental/pkg/router"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
// @title Car Rental API
// @version 1.0
// @description This is a Car Rental API implementation.
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email eibrahimarisoy@gmail.com
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8080
// @BasePath /api/v1
func main() {
cfg, err := config.LoadConfig("./pkg/config/config-local")
if err != nil {
fmt.Println("Failed to load config:", err)
}
// Set logger
logger.NewLogger(cfg)
defer logger.Close()
// Connect to database
DB := db.NewPsqlDB(cfg)
r := gin.Default()
srv := &http.Server{
Addr: fmt.Sprintf(":%s", cfg.ServerConfig.Port),
Handler: r,
ReadTimeout: time.Duration(cfg.ServerConfig.ReadTimeoutSecs) * time.Second,
WriteTimeout: time.Duration(cfg.ServerConfig.WriteTimeoutSecs) * time.Second,
}
rootRouter := r.Group(cfg.ServerConfig.RoutePrefix)
router.InitiliazeRoutes(rootRouter, DB, cfg)
rootRouter.GET("/healthz", func(c *gin.Context) {
c.JSON(http.StatusOK, nil)
})
rootRouter.GET("/readyz", func(c *gin.Context) {
db, err := DB.DB()
if err != nil {
zap.L().Fatal("cannot get sql database instance", zap.Error(err))
fmt.Println("cannot get sql database instance", err)
}
if err := db.Ping(); err != nil {
zap.L().Fatal("cannot ping database", zap.Error(err))
fmt.Println("cannot ping database", err)
}
c.JSON(http.StatusOK, nil)
})
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Listen error: %v", err)
}
}()
log.Println("Car Rental API service started")
// Wait for interrupt signal to gracefully shutdown the server with
graceful.ShutdownGin(srv, time.Duration(cfg.ServerConfig.TimeoutSecs*int64(time.Second)))
}