-
Notifications
You must be signed in to change notification settings - Fork 22
/
http.go
126 lines (107 loc) · 2.31 KB
/
http.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
package main
import (
"errors"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/urfave/negroni"
"net"
"net/http"
"sync"
)
type HttpServer struct {
ln net.Listener
s http.Server
wg *sync.WaitGroup
pending int32
stopped bool
started bool
initializers []func() error
uninitializers []func()
}
func NewHttpServer() *HttpServer {
return &HttpServer{
wg: new(sync.WaitGroup),
}
}
var (
gHttpServer = NewHttpServer()
)
func (o *HttpServer) Init() error {
var err error
for _, f := range o.initializers {
err = f()
if err != nil {
return err
}
}
return nil
}
func (o *HttpServer) Uninit() {
for _, f := range o.initializers {
f()
}
}
func (o *HttpServer) Run() error {
var err error
if o.started {
msg := fmt.Sprintf("http server start repeatly")
log.Errorln(msg)
return errors.New(msg)
}
err = o.Init()
if err != nil {
log.Errorf("http server init failed: %s", err)
return err
}
defer o.Uninit()
// Prepare the middleware and http handlers
n := negroni.New()
mux := ServeMux()
n.UseFunc(RecoveryMiddleware)
n.UseFunc(LoggerMiddleware)
n.UseFunc(CutServiceMiddleware)
n.UseHandler(mux)
o.s.Handler = n
o.ln, err = net.Listen("tcp", gApp.Cnf.Addr)
if err != nil {
log.Errorf("listen %s failed: %s", gApp.Cnf.Addr, err)
return err
}
log.Printf("http server serving addr: %s", gApp.Cnf.Addr)
o.started = true
o.wg.Add(1)
err = o.s.Serve(o.ln)
log.Errorf("http server quit: %s", err)
o.wg.Done()
o.started = false
if o.stopped {
// Stop in plan, so return with no error
o.stopped = false
return nil
}
return err
}
func (o *HttpServer) Stop() {
if !o.started {
return
}
o.stopped = true
o.ln.Close()
o.wg.Wait()
}
func (o *HttpServer) AddToInit(f func() error) {
o.initializers = append(o.initializers, f)
}
func (o *HttpServer) AddToUninit(f func()) {
o.uninitializers = append(o.uninitializers, f)
}
func ServeMux() *http.ServeMux {
apiUrlPrefix := "/api/v1"
mux := http.NewServeMux()
mux.HandleFunc(apiUrlPrefix+"/cmd/run", RunCmdHandler)
mux.HandleFunc(apiUrlPrefix+"/cmd/query", QueryCmdHandler)
mux.HandleFunc(apiUrlPrefix+"/cmd/list", ListCmdHandler)
mux.HandleFunc(apiUrlPrefix+"/cmd/cancel", CancelCmdHandler)
mux.HandleFunc(apiUrlPrefix+"/status/mem", StatusMemHandler)
return mux
}