Created
June 11, 2013 01:08
-
-
Save crosbymichael/5753816 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
m := map[string]map[string]func(*Server, float64, http.ResponseWriter, *http.Request, map[string]string) error{ | |
"GET": { | |
"/auth": getAuth, | |
"/version": getVersion, | |
"/info": getInfo, | |
"/images/json": getImagesJson, | |
"/images/viz": getImagesViz, | |
"/images/search": getImagesSearch, | |
"/images/{name:.*}/history": getImagesHistory, "/images/{name:.*}/json": getImagesByName, | |
"/containers/ps": getContainersJson, | |
"/containers/json": getContainersJson, | |
"/containers/{name:.*}/export": getContainersExport, | |
"/containers/{name:.*}/changes": getContainersChanges, | |
"/containers/{name:.*}/json": getContainersByName, | |
}, | |
"POST": { | |
"/auth": postAuth, | |
"/commit": postCommit, | |
"/build": postBuild, | |
"/images/create": postImagesCreate, | |
"/images/{name:.*}/insert": postImagesInsert, | |
"/images/{name:.*}/push": postImagesPush, | |
"/images/{name:.*}/tag": postImagesTag, | |
"/images/getCache": postImagesGetCache, | |
"/containers/create": postContainersCreate, | |
"/containers/{name:.*}/kill": postContainersKill, | |
"/containers/{name:.*}/restart": postContainersRestart, | |
"/containers/{name:.*}/start": postContainersStart, | |
"/containers/{name:.*}/stop": postContainersStop, | |
"/containers/{name:.*}/wait": postContainersWait, | |
"/containers/{name:.*}/resize": postContainersResize, | |
"/containers/{name:.*}/attach": postContainersAttach, | |
}, | |
"DELETE": { | |
"/containers/{name:.*}": deleteContainers, | |
"/images/{name:.*}": deleteImages, | |
}, | |
"OPTIONS": { | |
"": optionsHandler, | |
}, | |
} | |
for method, routes := range m { | |
for route, fct := range routes { | |
utils.Debugf("Registering %s, %s", method, route) | |
// NOTE: scope issue, make sure the variables are local and won't be changed | |
localRoute := route | |
localMethod := method | |
localFct := fct | |
f := func(w http.ResponseWriter, r *http.Request) { | |
utils.Debugf("Calling %s %s", localMethod, localRoute) | |
if logging { | |
log.Println(r.Method, r.RequestURI) | |
} | |
if strings.Contains(r.Header.Get("User-Agent"), "Docker-Client/") { | |
userAgent := strings.Split(r.Header.Get("User-Agent"), "/") | |
if len(userAgent) == 2 && userAgent[1] != VERSION { | |
utils.Debugf("Warning: client and server don't have the same version (client: %s, server: %s)", userAgent[1], VERSION) | |
} | |
} | |
version, err := strconv.ParseFloat(mux.Vars(r)["version"], 64) | |
if err != nil { | |
version = API_VERSION | |
} | |
if srv.enableCors { | |
writeCorsHeaders(w, r) | |
} | |
if version == 0 || version > API_VERSION { | |
w.WriteHeader(http.StatusNotFound) | |
return | |
} | |
if err := localFct(srv, version, w, r, mux.Vars(r)); err != nil { | |
httpError(w, err) | |
} | |
} | |
if localRoute == "" { | |
r.Methods(localMethod).HandlerFunc(f) | |
} else { | |
r.Path("/v{version:[0-9.]+}" + localRoute).Methods(localMethod).HandlerFunc(f) | |
r.Path(localRoute).Methods(localMethod).HandlerFunc(f) | |
} | |
} | |
} | |
return r, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment