forked from docker-archive/classicswarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimary.go
138 lines (126 loc) · 5.09 KB
/
primary.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
package api
import (
"crypto/tls"
"net/http"
log "github.com/Sirupsen/logrus"
"github.com/docker/swarm/cluster"
"github.com/gorilla/mux"
)
// Primary router context, used by handlers.
type context struct {
cluster cluster.Cluster
eventsHandler *eventsHandler
statusHandler StatusHandler
debug bool
tlsConfig *tls.Config
}
type handler func(c *context, w http.ResponseWriter, r *http.Request)
var routes = map[string]map[string]handler{
"HEAD": {
"/containers/{name:.*}/archive": proxyContainer,
},
"GET": {
"/_ping": ping,
"/events": getEvents,
"/info": getInfo,
"/version": getVersion,
"/images/json": getImagesJSON,
"/images/viz": notImplementedHandler,
"/images/search": proxyRandom,
"/images/get": getImages,
"/images/{name:.*}/get": proxyImageTagOptional,
"/images/{name:.*}/history": proxyImage,
"/images/{name:.*}/json": proxyImage,
"/containers/ps": getContainersJSON,
"/containers/json": getContainersJSON,
"/containers/{name:.*}/archive": proxyContainer,
"/containers/{name:.*}/export": proxyContainer,
"/containers/{name:.*}/changes": proxyContainer,
"/containers/{name:.*}/json": getContainerJSON,
"/containers/{name:.*}/top": proxyContainer,
"/containers/{name:.*}/logs": proxyContainer,
"/containers/{name:.*}/stats": proxyContainer,
"/containers/{name:.*}/attach/ws": proxyHijack,
"/exec/{execid:.*}/json": proxyContainer,
"/networks": getNetworks,
"/networks/{networkid:.*}": proxyNetwork,
"/volumes": getVolumes,
"/volumes/{volumename:.*}": proxyVolume,
},
"POST": {
"/auth": proxyRandom,
"/commit": postCommit,
"/build": postBuild,
"/images/create": postImagesCreate,
"/images/load": postImagesLoad,
"/images/{name:.*}/push": proxyImageTagOptional,
"/images/{name:.*}/tag": postTagImage,
"/containers/create": postContainersCreate,
"/containers/{name:.*}/kill": proxyContainerAndForceRefresh,
"/containers/{name:.*}/pause": proxyContainerAndForceRefresh,
"/containers/{name:.*}/unpause": proxyContainerAndForceRefresh,
"/containers/{name:.*}/rename": postRenameContainer,
"/containers/{name:.*}/restart": proxyContainerAndForceRefresh,
"/containers/{name:.*}/start": proxyContainerAndForceRefresh,
"/containers/{name:.*}/stop": proxyContainerAndForceRefresh,
"/containers/{name:.*}/wait": proxyContainerAndForceRefresh,
"/containers/{name:.*}/resize": proxyContainer,
"/containers/{name:.*}/attach": proxyHijack,
"/containers/{name:.*}/copy": proxyContainer,
"/containers/{name:.*}/exec": postContainersExec,
"/exec/{execid:.*}/start": postExecStart,
"/exec/{execid:.*}/resize": proxyContainer,
"/networks/create": postNetworksCreate,
"/networks/{networkid:.*}/connect": proxyNetwork,
"/networks/{networkid:.*}/disconnect": proxyNetwork,
"/volumes": postVolumes,
},
"PUT": {
"/containers/{name:.*}/archive": proxyContainer,
},
"DELETE": {
"/containers/{name:.*}": deleteContainers,
"/images/{name:.*}": deleteImages,
"/networks/{networkid:.*}": deleteNetworks,
"/volumes/{name:.*}": deleteVolumes,
},
"OPTIONS": {
"": optionsHandler,
},
}
func writeCorsHeaders(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
w.Header().Add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS")
}
// NewPrimary creates a new API router.
func NewPrimary(cluster cluster.Cluster, tlsConfig *tls.Config, status StatusHandler, enableCors bool) *mux.Router {
// Register the API events handler in the cluster.
eventsHandler := newEventsHandler()
cluster.RegisterEventHandler(eventsHandler)
context := &context{
cluster: cluster,
eventsHandler: eventsHandler,
statusHandler: status,
tlsConfig: tlsConfig,
}
r := mux.NewRouter()
for method, mappings := range routes {
for route, fct := range mappings {
log.WithFields(log.Fields{"method": method, "route": route}).Debug("Registering HTTP route")
localRoute := route
localFct := fct
wrap := func(w http.ResponseWriter, r *http.Request) {
log.WithFields(log.Fields{"method": r.Method, "uri": r.RequestURI}).Debug("HTTP request received")
if enableCors {
writeCorsHeaders(w, r)
}
localFct(context, w, r)
}
localMethod := method
r.Path("/v{version:[0-9.]+}" + localRoute).Methods(localMethod).HandlerFunc(wrap)
r.Path(localRoute).Methods(localMethod).HandlerFunc(wrap)
}
}
return r
}