-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (79 loc) · 2.05 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
package main
import (
"encoding/json"
"fmt"
"github.com/gole-dev/gole-layout/internal/server"
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
goleApp "github.com/gole-dev/gole/pkg/app"
"github.com/gole-dev/gole/pkg/config"
logger "github.com/gole-dev/gole/pkg/log"
"github.com/gole-dev/gole/pkg/redis"
v "github.com/gole-dev/gole/pkg/version"
"github.com/spf13/pflag"
_ "go.uber.org/automaxprocs"
"github.com/gole-dev/gole-layout/internal/model"
)
var (
cfgDir = pflag.StringP("config dir", "c", "config", "config path.")
env = pflag.StringP("env name", "e", "local", "env var name.")
version = pflag.BoolP("version", "v", false, "show version info.")
)
// @title API Doc
// @version 1.0
// @description API Doc of Project
// @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 goleApp.Config
if err := c.Load("app", &cfg); err != nil {
fmt.Printf("load config error: %v \nyou should copy config/sample as config/local for local dev\n", err)
return
}
// set global
goleApp.Conf = &cfg
// -------------- init resource -------------
logger.Init()
// init db
model.Init()
// init redis
redis.Init()
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 := goleApp.New(
goleApp.WithName(cfg.Name),
goleApp.WithVersion(cfg.Version),
goleApp.WithLogger(logger.GetLogger()),
goleApp.WithServer(
// init http server
server.NewHTTPServer(&cfg.HTTP),
// init gRPC server
server.NewGRPCServer(&cfg.GRPC),
),
)
if err := app.Run(); err != nil {
panic(err)
}
}