-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathserver.go
127 lines (101 loc) · 3.3 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
package vertex
import (
"errors"
"fmt"
"net"
"net/http"
"runtime/debug"
"sync"
"time"
"github.com/dvirsky/go-pylog/logging"
"github.com/hydrogen18/stoppableListener"
"github.com/julienschmidt/httprouter"
)
// Server represents a multi-API http server with a single router
type Server struct {
addr string
apis []*API
router *httprouter.Router
listener net.Listener
wg sync.WaitGroup
}
type builderFunc func() *API
var apiBuilders = map[string]builderFunc{}
// Register lest you automatically add an API to the server from your module's init() function.
//
// name is a unique name for your API (doesn't have to match the API name exactly).
//
// builder is a func that creates the API when we are ready to start the server.
//
// Optionally, you can pass a pointer to a config struct, or nil if you don't need to. This way, we can read the config struct's values
// from a unified config file BEFORE we call the builder, so the builder can use values in the config struct.
func Register(name string, builder func() *API, config interface{}) {
//logging.Info("Adding api builder %s", name)
apiBuilders[name] = builderFunc(builder)
if config != nil {
registerAPIConfig(name, config)
}
}
// NewServer creates a new blank server to add APIs to
func NewServer(addr string) *Server {
return &Server{
addr: addr,
apis: make([]*API, 0),
router: httprouter.New(),
}
}
// AddAPI adds an API to the server manually. It's preferred to use Register in an init() function
func (s *Server) AddAPI(a *API) {
a.configure(s.router)
s.router.PanicHandler = func(w http.ResponseWriter, r *http.Request, v interface{}) {
code, msg := httpError(NewErrorf("Unhandled panic: %s\n%s", v, string(debug.Stack())))
http.Error(w, msg, code)
}
s.apis = append(s.apis, a)
}
// Handler returns the underlying router, mainly for testing
func (s *Server) Handler() http.Handler {
return s.router
}
// InitAPIs initializes and adds all the APIs registered from API builders
func (s *Server) InitAPIs() {
for _, builder := range apiBuilders {
s.AddAPI(builder())
}
}
// Run runs the server if it has any APIs registered on it
func (s *Server) Run() (err error) {
if len(s.apis) == 0 {
return errors.New("No APIs defined for server")
}
// Server the console swagger UI
s.router.ServeFiles("/console/*filepath", http.Dir(Config.Server.ConsoleFilesPath))
// Start a stoppable listener
var l net.Listener
if l, err = net.Listen("tcp", s.addr); err != nil {
return fmt.Errorf("Could not listen in server: %s", err)
}
if s.listener, err = stoppableListener.New(l); err != nil {
return fmt.Errorf("Could not start stoppable listener in server: %s", err)
}
logging.Info("Starting server on %s", s.listener.Addr().String())
s.wg.Add(1)
defer func() {
s.wg.Done()
// don't return an error on server stopped
if err == stoppableListener.StoppedError {
err = nil
}
}()
srv := http.Server{
Handler: s.router,
ReadTimeout: time.Duration(Config.Server.ClientTimeout) * time.Second,
WriteTimeout: time.Duration(Config.Server.ClientTimeout) * time.Second, // maximum duration before timing out write of the response
}
return srv.Serve(s.listener)
}
// Stop waits up to a second and closes the server
func (s *Server) Stop() {
s.listener.(*stoppableListener.StoppableListener).Stop()
s.wg.Wait()
}