-
Notifications
You must be signed in to change notification settings - Fork 13
/
app.go
102 lines (81 loc) · 2.21 KB
/
app.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
package pairec
import (
"fmt"
"net/http"
"os"
"sync"
"time"
"github.com/alibaba/pairec/v2/middleware/prometheus"
"github.com/alibaba/pairec/v2/log"
"github.com/alibaba/pairec/v2/recconf"
)
var (
PairecApp *App
)
func init() {
PairecApp = NewApp()
}
type App struct {
Handlers *ControllerRegister
Server *http.Server
}
func NewApp() *App {
cr := NewControllerRegister()
app := &App{Handlers: cr, Server: &http.Server{}}
return app
}
func (app *App) Run() {
mode := os.Getenv("RUN_MODE")
if mode == "COMMAND" {
return
}
addr := fmt.Sprintf("%s:%d", recconf.Config.ListenConf.HttpAddr, recconf.Config.ListenConf.HttpPort)
if recconf.Config.PrometheusConfig.Enable {
config := recconf.Config.PrometheusConfig
var options []prometheus.PrometheusOption
if config.Subsystem != "" {
options = append(options, prometheus.WithSubsystem(config.Subsystem))
}
if len(config.ReqDurBuckets) > 0 {
options = append(options, prometheus.WithReqDurBuckets(config.ReqDurBuckets))
}
if len(config.ReqSizeBuckets) > 0 {
options = append(options, prometheus.WithReqSzBuckets(config.ReqSizeBuckets))
}
if len(config.RespSizeBuckets) > 0 {
options = append(options, prometheus.WithResSzBuckets(config.RespSizeBuckets))
}
p := prometheus.NewPrometheus(options...)
if config.PushGatewayURL != "" && config.PushIntervalSecs > 0 {
if config.Job == "" {
env := recconf.Config.RunMode
if os.Getenv("PAIREC_ENVIRONMENT") != "" {
env = os.Getenv("PAIREC_ENVIRONMENT")
}
config.Job = env
}
p.Push(config.PushGatewayURL, config.PushIntervalSecs, config.Job)
}
app.Use(p.HandlerFunc)
}
app.Handlers.ApplyMiddlewares()
app.Server.Handler = app.Handlers
app.Server.Addr = addr
app.Server.ReadTimeout = 30 * time.Second
app.Server.WriteTimeout = 30 * time.Second
app.Server.MaxHeaderBytes = 1 << 20
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
if err := app.Server.ListenAndServe(); err != http.ErrServerClosed {
log.Error(fmt.Sprintf("server stop, err=%v", err))
}
}()
fmt.Println("server start")
wg.Wait()
log.Flush()
}
func (app *App) Use(middleware ...MiddlewareFunc) {
app.Handlers.Middlewares = append(app.Handlers.Middlewares, middleware...)
}