-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.go
69 lines (60 loc) · 1.85 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
package server
import (
"context"
"fmt"
"net/http"
"github.com/arbor-dev/arbor/logger"
"github.com/arbor-dev/arbor/security"
"github.com/arbor-dev/arbor/services"
"github.com/gorilla/mux"
)
//ArborServer is a struct that manages the proxy server
type ArborServer struct {
addr string
router *mux.Router
server *http.Server
}
//NewServer creates a new Arbor Server
func NewArborServer(routes services.RouteCollection, addr string, port uint16) *ArborServer {
a := new(ArborServer)
a.addr = fmt.Sprintf("%s:%d", addr, port)
a.router = NewRouter(routes)
a.server = &http.Server{Addr: a.addr, Handler: a.router}
return a
}
//StartServer starts the http server in a goroutine to start listening
func (a *ArborServer) StartServer() {
logger.Log(logger.SPEC, "Roots being planted [Server is listening on "+a.addr+"]")
err := a.server.ListenAndServe()
if err != nil {
if err.Error() == "http: Server closed" {
return
}
logger.Log(logger.FATAL, err.Error())
}
}
//KillServer ends the http server
func (a *ArborServer) KillServer() {
logger.Log(logger.SPEC, "Pulling up the roots [Shutting down the server...]")
a.server.Shutdown(context.Background())
if security.IsEnabled() {
security.Shutdown()
}
}
// StartSecuredServer starts a secured arbor server (Token required for access)
//
// Provide a set of routes to serve and a port to serve on.
func StartSecuredServer(routes services.RouteCollection, addr string, port uint16) *ArborServer {
srv := NewArborServer(routes, addr, port)
security.Init()
srv.StartServer()
return srv
}
// StartUnsecuredServer starts an unsecured arbor server (Token required for access)
//
// Provide a set of routes to server and a port to serve on/
func StartUnsecuredServer(routes services.RouteCollection, addr string, port uint16) *ArborServer {
srv := NewArborServer(routes, addr, port)
srv.StartServer()
return srv
}