-
Notifications
You must be signed in to change notification settings - Fork 499
/
server.go
157 lines (135 loc) · 4 KB
/
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
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
/*
Copyright 2017 Authors All rights reserved
*/
package httpserver
import (
"fmt"
"net"
"net/http"
"strconv"
"build-booster/common/blog"
"build-booster/common/ssl"
"github.com/emicklei/go-restful"
"github.com/gorilla/mux"
)
// HTTPServer is data struct of http server
type HTTPServer struct {
addr string
port uint
insecureAddr string
insecurePort uint
sock string
isSSL bool
caFile string
certFile string
keyFile string
certPasswd string
webContainer *restful.Container
router *mux.Router
}
// NewHTTPServer get a new http server.
func NewHTTPServer(port uint, addr, sock string) *HTTPServer {
return &HTTPServer{
addr: addr,
port: port,
sock: sock,
webContainer: restful.NewContainer(),
router: mux.NewRouter(),
isSSL: false,
}
}
// SetInsecureServer set insecure(http) address and port into http server.
func (s *HTTPServer) SetInsecureServer(insecureAddr string, insecurePort uint) {
s.insecureAddr = insecureAddr
s.insecurePort = insecurePort
}
// GetWebContainer return the web container.
func (s *HTTPServer) GetWebContainer() *restful.Container {
return s.webContainer
}
// GetRouter return the router.
func (s *HTTPServer) GetRouter() *mux.Router {
return s.router
}
// SetSSL set certs files into http server.
func (s *HTTPServer) SetSSL(cafile, certfile, keyfile, certPasswd string) {
s.caFile = cafile
s.certFile = certfile
s.keyFile = keyfile
s.certPasswd = certPasswd
s.isSSL = true
}
// RegisterWebServer register a new web server beyond rootPath and handle these actions.
func (s *HTTPServer) RegisterWebServer(rootPath string, filter restful.FilterFunction, actions []*Action) error {
//new a web service
ws := s.NewWebService(rootPath, filter)
//register action
s.RegisterActions(ws, actions)
return nil
}
// NewWebService get a new web service.
func (s *HTTPServer) NewWebService(rootPath string, filter restful.FilterFunction) *restful.WebService {
ws := new(restful.WebService)
if "" != rootPath {
ws.Path(rootPath)
}
ws.Produces(restful.MIME_JSON, restful.MIME_XML, restful.MIME_OCTET)
if nil != filter {
ws.Filter(filter)
}
s.webContainer.Add(ws)
return ws
}
// RegisterActions register a list of actions into web service.
func (s *HTTPServer) RegisterActions(ws *restful.WebService, actions []*Action) {
for _, action := range actions {
switch action.Verb {
case "POST":
route := ws.POST(action.Path).To(action.Handler)
ws.Route(route)
blog.Infof("register post api, url(%s)", action.Path)
case "GET":
route := ws.GET(action.Path).To(action.Handler)
ws.Route(route)
blog.Infof("register get api, url(%s)", action.Path)
case "PUT":
route := ws.PUT(action.Path).To(action.Handler)
ws.Route(route)
blog.Infof("register put api, url(%s)", action.Path)
case "DELETE":
route := ws.DELETE(action.Path).To(action.Handler)
ws.Route(route)
blog.Infof("register delete api, url(%s)", action.Path)
case "PATCH":
route := ws.PATCH(action.Path).To(action.Handler)
ws.Route(route)
blog.Infof("register patch api, url(%s)", action.Path)
default:
blog.Error("unrecognized action verb: %s", action.Verb)
}
}
}
// ListenAndServe start a http server
func (s *HTTPServer) ListenAndServe() error {
var chError = make(chan error)
//list and serve by addrport
go func() {
addrport := net.JoinHostPort(s.addr, strconv.FormatUint(uint64(s.port), 10))
httpserver := &http.Server{Addr: addrport, Handler: s.webContainer}
if s.isSSL {
tlsConf, err := ssl.ServerTSLConf(s.caFile, s.certFile, s.keyFile, s.certPasswd)
if err != nil {
blog.Error("fail to load certfile, err:%s", err.Error())
chError <- fmt.Errorf("fail to load certfile")
return
}
httpserver.TLSConfig = tlsConf
blog.Info("Start https service on(%s:%d)", s.addr, s.port)
chError <- httpserver.ListenAndServeTLS("", "")
} else {
blog.Info("Start http service on(%s:%d)", s.addr, s.port)
chError <- httpserver.ListenAndServe()
}
}()
return <-chError
}