-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (71 loc) · 2.25 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
package main
import (
"github.com/defval/inject"
"github.com/gghcode/go-gin-starterkit/api"
"github.com/gghcode/go-gin-starterkit/api/auth"
"github.com/gghcode/go-gin-starterkit/api/common"
"github.com/gghcode/go-gin-starterkit/api/todo"
"github.com/gghcode/go-gin-starterkit/api/user"
"github.com/gghcode/go-gin-starterkit/config"
"github.com/gghcode/go-gin-starterkit/db"
_ "github.com/gghcode/go-gin-starterkit/docs"
"github.com/gghcode/go-gin-starterkit/middleware"
"github.com/gghcode/go-gin-starterkit/service"
"github.com/gin-gonic/gin"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/swaggerFiles"
)
const (
envPrefix = "REST"
)
// @title Go Gin Starter API
// @version 1.0
// @description This is a sample gin starter server.
// @termsOfService http://swagger.io/terms/
// @securitydefinitions.apikey ApiKeyAuth
// @BasePath /api
// @in header
// @name Authorization
// @contact.name API Support
// @contact.email [email protected]
// @license.name MIT
// @license.url https://github.com/gghcode/go-gin-starterkit/blob/master/LICENSE
func main() {
container, err := inject.New(
inject.Provide(config.NewBuilder().
AddConfigFile("config.yaml", true).
BindEnvs(envPrefix).
Build),
inject.Provide(db.NewConn),
inject.Provide(db.NewRedisConn),
inject.Provide(service.NewPassport),
inject.Provide(common.NewController, inject.As(api.IController)),
inject.Provide(user.NewRepository),
inject.Provide(user.NewController, inject.As(api.IController)),
inject.Provide(todo.NewRepository),
inject.Provide(todo.NewController, inject.As(api.IController)),
inject.Provide(auth.NewService),
inject.Provide(auth.NewController, inject.As(api.IController)),
)
if err != nil {
panic(err)
}
var conf config.Configuration
if err := container.Extract(&conf); err != nil {
panic(err)
}
var controllers []api.Controller
if err := container.Extract(&controllers); err != nil {
panic(err)
}
router := gin.New()
router.Use(middleware.AddAuthHandler(conf.Jwt))
router.GET("/docs/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
apiRouter := router.Group("api/")
for _, controller := range controllers {
controller.RegisterRoutes(apiRouter)
}
if err := router.Run(conf.Addr); err != nil {
panic(err)
}
}