/
main.go
182 lines (165 loc) · 4.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"bytes"
"context"
"errors"
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/gevinzone/basic-go/week9/webook/ioc"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/pflag"
"github.com/spf13/viper"
_ "github.com/spf13/viper/remote"
"go.uber.org/zap"
"net/http"
"time"
)
func main() {
//db := initDB()
//rdb := initRedis()
//
//server := initWebServer()
//
//u := initUser(db, rdb)
//u.RegisterRoutes(server)
//initViperRemote()
initViperV1()
initLogger()
closeFunc := ioc.InitOTEL()
initPrometheus()
keys := viper.AllKeys()
println(keys)
//setting := viper.AllSettings()
//fmt.Println(setting)
app := InitWebServer()
// Consumer 在我设计下,类似于 Web,或者 GRPC 之类的,是一个顶级入口
for _, c := range app.consumers {
err := c.Start()
if err != nil {
panic(err)
}
}
app.cron.Start()
server := app.web
server.GET("/hello", func(ctx *gin.Context) {
ctx.String(http.StatusOK, "你好,你来了")
})
server.Run(":8080")
// 一分钟内你要关完,要退出
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
closeFunc(ctx)
ctx = app.cron.Stop()
// 想办法 close ??
// 这边可以考虑超时强制退出,防止有些任务,执行特别长的时间
tm := time.NewTimer(time.Minute * 10)
select {
case <-tm.C:
case <-ctx.Done():
}
// 作业
//server.Run(":8081")
}
func initPrometheus() {
go func() {
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":8081", nil)
}()
}
func initLogger() {
logger, err := zap.NewDevelopment()
if err != nil {
panic(err)
}
zap.L().Info("这是 replace 之前")
// 如果你不 replace,直接用 zap.L(),你啥都打不出来。
zap.ReplaceGlobals(logger)
zap.L().Info("hello,你搞好了")
type Demo struct {
Name string `json:"name"`
}
zap.L().Info("这是实验参数",
zap.Error(errors.New("这是一个 error")),
zap.Int64("id", 123),
zap.Any("一个结构体", Demo{Name: "hello"}))
}
func initViperReader() {
viper.SetConfigType("yaml")
cfg := `
db.mysql:
dsn: "root:root@tcp(localhost:13316)/webook"
redis:
addr: "localhost:6379"
`
err := viper.ReadConfig(bytes.NewReader([]byte(cfg)))
if err != nil {
panic(err)
}
}
func initViperRemote() {
err := viper.AddRemoteProvider("etcd3",
// 通过 webook 和其他使用 etcd 的区别出来
"http://127.0.0.1:12379", "/webook")
if err != nil {
panic(err)
}
viper.SetConfigType("yaml")
err = viper.WatchRemoteConfig()
if err != nil {
panic(err)
}
viper.OnConfigChange(func(in fsnotify.Event) {
fmt.Println(in.Name, in.Op)
})
err = viper.ReadRemoteConfig()
if err != nil {
panic(err)
}
}
func initViperV1() {
cfile := pflag.String("config",
"config/config.yaml", "指定配置文件路径")
pflag.Parse()
viper.SetConfigFile(*cfile)
// 实时监听配置变更
viper.WatchConfig()
// 只能告诉你文件变了,不能告诉你,文件的哪些内容变了
viper.OnConfigChange(func(in fsnotify.Event) {
// 比较好的设计,它会在 in 里面告诉你变更前的数据,和变更后的数据
// 更好的设计是,它会直接告诉你差异。
fmt.Println(in.Name, in.Op)
fmt.Println(viper.GetString("db.dsn"))
})
//viper.SetDefault("db.mysql.dsn",
// "root:root@tcp(localhost:3306)/mysql")
//viper.SetConfigFile("config/dev.yaml")
//viper.KeyDelimiter("-")
err := viper.ReadInConfig()
if err != nil {
panic(err)
}
}
func initViper() {
viper.SetDefault("db.mysql.dsn",
"root:root@tcp(localhost:3306)/mysql")
// 配置文件的名字,但是不包含文件扩展名
// 不包含 .go, .yaml 之类的后缀
viper.SetConfigName("dev")
// 告诉 viper 我的配置用的是 yaml 格式
// 现实中,有很多格式,JSON,XML,YAML,TOML,ini
viper.SetConfigType("yaml")
// 当前工作目录下的 config 子目录
viper.AddConfigPath("./config")
//viper.AddConfigPath("/tmp/config")
//viper.AddConfigPath("/etc/webook")
// 读取配置到 viper 里面,或者你可以理解为加载到内存里面
err := viper.ReadInConfig()
if err != nil {
panic(err)
}
//otherViper := viper.New()
//otherViper.SetConfigName("myjson")
//otherViper.AddConfigPath("./config")
//otherViper.SetConfigType("json")
}