forked from kingwrcy/moments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_prod.go
93 lines (74 loc) · 2.09 KB
/
main_prod.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
89
90
91
92
93
//go:build prod
package main
import (
"embed"
"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"
myMiddleware "github.com/kingwrcy/moments/middleware"
"github.com/kingwrcy/moments/vo"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/rs/zerolog"
"github.com/samber/do/v2"
_ "github.com/swaggo/echo-swagger"
"gorm.io/gorm"
"io/fs"
"net/http"
)
var gitCommitID string
//go:embed public/*
var staticFiles embed.FS
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(myMiddleware.Auth(injector))
setupRouter(injector)
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
HTML5: true,
Root: "public", // because files are located in `web` directory in `webAssets` fs
Filesystem: http.FS(staticFiles),
}))
e.FileFS("/*", "public/index.html", staticFiles)
migrateTo3(tx, myLogger)
e.HideBanner = true
myLogger.Info().Msgf("服务端启动成功,监听:%d端口...", cfg.Port)
err = e.Start(fmt.Sprintf(":%d", cfg.Port))
if err != nil {
myLogger.Fatal().Msgf("服务启动失败,错误原因:%s", err)
}
}
func isEmbedFSEmpty(e embed.FS, path string) (bool, error) {
entries, err := fs.ReadDir(e, path)
if err != nil {
return false, err
}
return len(entries) == 0, nil
}