-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
54 lines (47 loc) · 1000 Bytes
/
server.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
package revproxy
import (
"context"
"log"
"net"
"net/http"
"github.com/gorilla/mux"
)
type server struct {
opt *serverOpt
router *mux.Router
svr *http.Server
}
func NewServer(funcs ...serverOptFunc) *server {
opt := new(serverOpt)
for _, fn := range funcs {
fn(opt)
}
initOpt(opt)
router := mux.NewRouter()
svr := &http.Server{
Handler: router,
ReadTimeout: opt.rto,
WriteTimeout: opt.wto,
IdleTimeout: opt.ito,
ReadHeaderTimeout: opt.rhto,
}
return &server{
opt: opt,
router: router,
svr: svr,
}
}
func (s *server) Start(template string, kv KeyValue) error {
if err := applyTemplate(template, kv, s.router, s.opt.allowHeaders); err != nil {
return err
}
listener, err := net.Listen("tcp", s.opt.listenAddr)
if err != nil {
return err
}
log.Printf("info: server listen %s/tcp", s.opt.listenAddr)
return s.svr.Serve(listener)
}
func (s *server) Stop(ctx context.Context) error {
return s.svr.Shutdown(ctx)
}