-
Notifications
You must be signed in to change notification settings - Fork 237
/
main.go
106 lines (92 loc) · 2.24 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
*
* ____ __
* / __/__ ____ _/ /__
* / _// _ `/ _ `/ / -_)
* /___/\_,_/\_, /_/\__/
* /___/
*
*
* generate by http://patorjk.com/software/taag/#p=display&f=Small%20Slant&t=Eagle
*/
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/spf13/pflag"
_ "go.uber.org/automaxprocs"
"github.com/go-eagle/eagle/internal/model"
"github.com/go-eagle/eagle/internal/repository"
"github.com/go-eagle/eagle/internal/server"
"github.com/go-eagle/eagle/internal/service"
eagle "github.com/go-eagle/eagle/pkg/app"
"github.com/go-eagle/eagle/pkg/config"
logger "github.com/go-eagle/eagle/pkg/log"
v "github.com/go-eagle/eagle/pkg/version"
)
var (
cfgDir = pflag.StringP("config dir", "c", "config", "config path.")
env = pflag.StringP("env name", "e", "", "env var name.")
version = pflag.BoolP("version", "v", false, "show version info.")
)
// @title eagle docs api
// @version 1.0
// @description eagle demo
// @host localhost:8080
// @BasePath /v1
func main() {
pflag.Parse()
if *version {
ver := v.Get()
marshaled, err := json.MarshalIndent(&ver, "", " ")
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
fmt.Println(string(marshaled))
return
}
// init config
c := config.New(*cfgDir, config.WithEnv(*env))
var cfg eagle.Config
if err := c.Load("app", &cfg); err != nil {
panic(err)
}
// set global
eagle.Conf = &cfg
// -------------- init resource -------------
logger.Init()
// init db
model.Init()
// init redis
// nolint: errcheck
// redis.Init()
// init service
db, _ := model.GetDB()
service.Svc = service.New(repository.New(db))
gin.SetMode(cfg.Mode)
// init pprof server
go func() {
fmt.Printf("Listening and serving PProf HTTP on %s\n", cfg.PprofPort)
if err := http.ListenAndServe(cfg.PprofPort, http.DefaultServeMux); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen ListenAndServe for PProf, err: %s", err.Error())
}
}()
// start app
app := eagle.New(
eagle.WithName(cfg.Name),
eagle.WithVersion(cfg.Version),
eagle.WithLogger(logger.GetLogger()),
eagle.WithServer(
// init http server
server.NewHTTPServer(&cfg.HTTP),
),
)
if err := app.Run(); err != nil {
panic(err)
}
}