-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
88 lines (74 loc) · 2.61 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
package main
import (
"context"
"fmt"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"go-boilerplate/db/postgresql"
MiddlewareCustom "go-boilerplate/middleware"
"net/http"
"os"
"os/signal"
"time"
_articleHttpDelivery "go-boilerplate/article/delivery/http"
_articlePostgreRepository "go-boilerplate/article/repository/postgresql"
_articleUsecase "go-boilerplate/article/usecase"
_userHttDelivery "go-boilerplate/user/delivery/http"
_userPostgreRepository "go-boilerplate/user/repository/postgresql"
_userUsecase "go-boilerplate/user/usecase"
)
func init() {
viper.SetConfigName("config")
viper.AddConfigPath(".")
viper.SetConfigType("yml")
if err := viper.ReadInConfig(); err != nil {
viper.AutomaticEnv()
panic(fmt.Errorf("fatal error config file: %s", err))
}
}
func main() {
server := &http.Server{
Addr: ":" + viper.GetString("app_port"),
ReadTimeout: time.Duration(viper.GetInt("READ_TIMEOUT")) * time.Second,
WriteTimeout: time.Duration(viper.GetInt("WRITE_TIMEOUT")) * time.Second,
}
postgreSQL := postgresql.Connect()
timeoutCtx := time.Duration(viper.GetInt("CTX_TIMEOUT")) * time.Second
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowMethods: []string{http.MethodGet, http.MethodHead, http.MethodPut, http.MethodPatch, http.MethodPost, http.MethodDelete},
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept},
}))
CustomMiddleware := MiddlewareCustom.Init()
e.HTTPErrorHandler = CustomMiddleware.ErrorHandler
MiddlewareCustom.Logger = logrus.New()
e.Logger = MiddlewareCustom.GetEchoLogger()
e.Use(MiddlewareCustom.Hook())
go func() {
if err := e.StartServer(server); err != nil {
e.Logger.Info("Shutting down the server")
}
}()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Server up!")
})
userRepo := _userPostgreRepository.NewPsqlUserRepository(postgreSQL)
userUsecase := _userUsecase.NewUserUsecase(userRepo, timeoutCtx)
_userHttDelivery.NewUserHandler(e, userUsecase)
articleRepo := _articlePostgreRepository.NewPsqlArticleRepository(postgreSQL)
articleUsecase := _articleUsecase.NewArticleUsecase(articleRepo, timeoutCtx)
_articleHttpDelivery.NewArticleHandler(e, articleUsecase)
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := e.Shutdown(ctx); err != nil {
e.Logger.Fatal(err)
}
}