forked from libopenstorage/openstorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
100 lines (87 loc) · 2.54 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
package server
import (
"fmt"
"net"
"net/http"
"os"
"path"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
)
// Route is a specification and handler for a REST endpoint.
type Route struct {
verb string
path string
fn func(http.ResponseWriter, *http.Request)
}
type restServer interface {
Routes() []*Route
String() string
logReq(request string, id string) *log.Entry
sendError(request string, id string, w http.ResponseWriter, msg string, code int)
}
type restBase struct {
restServer
version string
name string
}
func (rest *restBase) logReq(request string, id string) *log.Entry {
return log.WithFields(log.Fields{
"Driver": rest.name,
"Request": request,
"ID": id,
})
}
func (rest *restBase) sendError(request string, id string, w http.ResponseWriter, msg string, code int) {
rest.logReq(request, id).Warn(code, " ", msg)
http.Error(w, msg, code)
}
func notFound(w http.ResponseWriter, r *http.Request) {
log.Warnf("Not found: %+v ", r.URL)
http.NotFound(w, r)
}
func startServer(name string, sockBase string, port int, routes []*Route) error {
var (
listener net.Listener
err error
)
router := mux.NewRouter()
router.NotFoundHandler = http.HandlerFunc(notFound)
for _, v := range routes {
router.Methods(v.verb).Path(v.path).HandlerFunc(v.fn)
}
socket := path.Join(sockBase, name+".sock")
os.Remove(socket)
os.MkdirAll(path.Dir(socket), 0755)
log.Printf("Starting REST service on %+v", socket)
listener, err = net.Listen("unix", socket)
if err != nil {
log.Warn("Cannot listen on UNIX socket: ", err)
return err
}
go http.Serve(listener, router)
if port != 0 {
go http.ListenAndServe(fmt.Sprintf(":%v", port), router)
}
return nil
}
// StartGraphAPI starts a REST server to receive GraphDriver commands
func StartGraphAPI(name string, port int, restBase string) error {
graphPlugin := newGraphPlugin(name)
routes := append(graphPlugin.Routes())
return startServer(name, restBase, port, routes)
}
// StartServerAPI starts a REST server to receive driver configuration commands
// from the CLI/UX.
func StartServerAPI(name string, port int, restBase string) error {
volApi := newVolumeAPI(name)
clusterApi := newClusterAPI(name)
routes := append(volApi.Routes(), clusterApi.Routes()...)
return startServer(name, restBase, port, routes)
}
// StartPluginAPI starts a REST server to receive volume commands from the
// Linux container engine.
func StartPluginAPI(name string, pluginBase string) error {
rest := newVolumePlugin(name)
return startServer(name, pluginBase, 0, rest.Routes())
}