forked from kingwrcy/moments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (55 loc) · 1.54 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
//go:build !prod
package main
import (
"fmt"
"github.com/ilyakaznacheev/cleanenv"
_ "github.com/joho/godotenv/autoload"
"github.com/kingwrcy/moments/db"
_ "github.com/kingwrcy/moments/docs"
"github.com/kingwrcy/moments/handler"
"github.com/kingwrcy/moments/log"
"github.com/kingwrcy/moments/middleware"
"github.com/kingwrcy/moments/vo"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog"
"github.com/samber/do/v2"
_ "github.com/swaggo/echo-swagger"
"gorm.io/gorm"
)
var gitCommitID string
func newEchoEngine(_ do.Injector) (*echo.Echo, error) {
e := echo.New()
return e, nil
}
// @title Moments API
// @version 0.2.1
func main() {
injector := do.New()
var cfg vo.AppConfig
err := cleanenv.ReadEnv(&cfg)
if err != nil {
fmt.Printf("读取配置文件异常:%s", err)
return
}
do.ProvideValue(injector, &cfg)
do.Provide(injector, log.NewLogger)
myLogger := do.MustInvoke[zerolog.Logger](injector)
if gitCommitID != "" {
myLogger.Info().Msgf("git commit id = %s", gitCommitID)
}
handleEmptyConfig(myLogger, &cfg)
do.Provide(injector, db.NewDB)
do.Provide(injector, newEchoEngine)
do.Provide(injector, handler.NewBaseHandler)
tx := do.MustInvoke[*gorm.DB](injector)
e := do.MustInvoke[*echo.Echo](injector)
e.Use(middleware.Auth(injector))
setupRouter(injector)
migrateTo3(tx, myLogger)
myLogger.Info().Msgf("服务端启动成功,监听:%d端口...", cfg.Port)
e.HideBanner = true
err = e.Start(fmt.Sprintf(":%d", cfg.Port))
if err != nil {
myLogger.Fatal().Msgf("服务启动失败,错误原因:%s", err)
}
}