forked from ProtoML/ProtoML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiServer.go
79 lines (68 loc) · 2.1 KB
/
apiServer.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
package api
import (
"fmt"
"net/http"
//"github.com/ProtoML/ProtoML-persist/persist/elastic"
"github.com/ProtoML/ProtoML/logger"
"github.com/ProtoML/ProtoML-persist/persist"
"html"
"github.com/ant0ine/go-json-rest/rest"
)
const (
LOGTAG = "API-Server"
DEFAULT_API_PORT = 8080
SERVER_CLOSE_PANIC_ERROR = "Closing webserver, ignore panic"
)
type APIServerState struct {
Port int
poisonPill chan bool
errChan chan error
Store persist.PersistStorage
ResourceHandler rest.ResourceHandler
}
func New(port int, store persist.PersistStorage)(*APIServerState) {
return &APIServerState{Port:port,Store:store,ResourceHandler:rest.ResourceHandler{}}
}
// Used to halt server due to non-trivial gracefull server stopping
func (server *APIServerState) Close() {
server.poisonPill <- true // pop poison pill to panic then recover
}
// Start api server with ability to shutdown
func (server *APIServerState) Start() (errChan chan error) {
routes := []*rest.Route{&rest.Route{"GET", "/", server.index}}
routes = append(routes, server.APIHandleFuncs()...)
server.ResourceHandler.SetRoutes(routes...)
server.errChan = make(chan error)
// main server
HTTP := func() {
logger.LogInfo(LOGTAG,"Starting API HTTP Server On Port :%d",server.Port)
err := http.ListenAndServe(fmt.Sprintf(":%d",server.Port), &server.ResourceHandler)
server.errChan <- err
}
// closing halt bomb
server.poisonPill = make(chan bool)
Panic := func(PoisonPill chan bool) {
<-PoisonPill
panic(SERVER_CLOSE_PANIC_ERROR)
}
// stopping bomb and shutdown api server
Recover := func(){
recover()
close(server.errChan)
}
// run in goroutine to allow for return
go func() {
defer server.Store.Close()
defer Recover()
go HTTP()
Panic(server.poisonPill)
}()
return server.errChan
}
func (server* APIServerState) unrecognizedCall(w rest.ResponseWriter, req *rest.Request) {
//http.Redirect(w, req, "/error", http.StatusNotFound)
return
}
func (server* APIServerState) index(w rest.ResponseWriter, req *rest.Request) {
fmt.Fprintf(w, "Hello, %q", html.EscapeString(req.URL.Path))
}