-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (50 loc) · 1.51 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
// Package main is the entry point of the application
package main
import (
"fmt"
"github.com/gbrayhan/microservices-go/src/infrastructure/repository/config"
errorsController "github.com/gbrayhan/microservices-go/src/infrastructure/rest/controllers/errors"
"github.com/gbrayhan/microservices-go/src/infrastructure/rest/middlewares"
"net/http"
"strings"
"time"
limit "github.com/aviddiviner/gin-limit"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"github.com/gbrayhan/microservices-go/src/infrastructure/rest/routes"
)
func main() {
router := gin.Default()
router.Use(limit.MaxAllowed(200))
router.Use(cors.Default())
var err error
DB, err := config.GormOpen()
if err != nil {
_ = fmt.Errorf("fatal error in database file: %s", err)
panic(err)
}
router.Use(middlewares.GinBodyLogMiddleware)
router.Use(errorsController.Handler)
routes.ApplicationV1Router(router, DB)
startServer(router)
}
func startServer(router http.Handler) {
viper.SetConfigFile("config.json")
if err := viper.ReadInConfig(); err != nil {
_ = fmt.Errorf("fatal error in config file: %s", err.Error())
panic(err)
}
serverPort := fmt.Sprintf(":%s", viper.GetString("ServerPort"))
s := &http.Server{
Addr: serverPort,
Handler: router,
ReadTimeout: 18000 * time.Second,
WriteTimeout: 18000 * time.Second,
MaxHeaderBytes: 1 << 20,
}
if err := s.ListenAndServe(); err != nil {
_ = fmt.Errorf("fatal error description: %s", strings.ToLower(err.Error()))
panic(err)
}
}