Adding error checks to REST calls

This commit is contained in:
Thorhallur Sverrisson 2015-09-23 05:48:32 +00:00
parent c263560401
commit cfa232741d

76
web.go
View file

@ -83,55 +83,79 @@ func GetHealthHandler(rw http.ResponseWriter, r *http.Request) {
func GetBackendsHandler(rw http.ResponseWriter, r *http.Request) { func GetBackendsHandler(rw http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
templatesRenderer.JSON(rw, http.StatusOK, currentConfigurations[vars["provider"]].Backends) providerId := vars["provider"]
if provider, ok := currentConfigurations[providerId]; ok {
templatesRenderer.JSON(rw, http.StatusOK, provider.Backends)
} else {
http.NotFound(rw, r)
}
} }
func GetBackendHandler(rw http.ResponseWriter, r *http.Request) { func GetBackendHandler(rw http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id := vars["backend"] providerId := vars["provider"]
if backend, ok := currentConfigurations[vars["provider"]].Backends[id]; ok { backendId := vars["backend"]
templatesRenderer.JSON(rw, http.StatusOK, backend) if provider, ok := currentConfigurations[providerId]; ok {
} else { if backend, ok := provider.Backends[backendId]; ok {
http.NotFound(rw, r) templatesRenderer.JSON(rw, http.StatusOK, backend)
return
}
} }
http.NotFound(rw, r)
} }
func GetFrontendsHandler(rw http.ResponseWriter, r *http.Request) { func GetFrontendsHandler(rw http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
templatesRenderer.JSON(rw, http.StatusOK, currentConfigurations[vars["provider"]].Frontends) providerId := vars["provider"]
if provider, ok := currentConfigurations[providerId]; ok {
templatesRenderer.JSON(rw, http.StatusOK, provider.Frontends)
} else {
http.NotFound(rw, r)
}
} }
func GetFrontendHandler(rw http.ResponseWriter, r *http.Request) { func GetFrontendHandler(rw http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id := vars["frontend"] providerId := vars["provider"]
if frontend, ok := currentConfigurations[vars["provider"]].Frontends[id]; ok { frontendId := vars["frontend"]
templatesRenderer.JSON(rw, http.StatusOK, frontend) if provider, ok := currentConfigurations[providerId]; ok {
} else { if frontend, ok := provider.Frontends[frontendId]; ok {
http.NotFound(rw, r) templatesRenderer.JSON(rw, http.StatusOK, frontend)
return
}
} }
http.NotFound(rw, r)
} }
func GetServersHandler(rw http.ResponseWriter, r *http.Request) { func GetServersHandler(rw http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
backend := vars["backend"] providerId := vars["provider"]
if backend, ok := currentConfigurations[vars["provider"]].Backends[backend]; ok { backendId := vars["backend"]
templatesRenderer.JSON(rw, http.StatusOK, backend.Servers) if provider, ok := currentConfigurations[providerId]; ok {
} else { if backend, ok := provider.Backends[backendId]; ok {
http.NotFound(rw, r) templatesRenderer.JSON(rw, http.StatusOK, backend.Servers)
return
}
} }
http.NotFound(rw, r)
} }
func GetServerHandler(rw http.ResponseWriter, r *http.Request) { func GetServerHandler(rw http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
backend := vars["backend"] providerId := vars["provider"]
server := vars["server"] backendId := vars["backend"]
if backend, ok := currentConfigurations[vars["provider"]].Backends[backend]; ok { serverId := vars["server"]
if server, ok := backend.Servers[server]; ok { fmt.Printf("%v %v %v\n", providerId, backendId, serverId)
templatesRenderer.JSON(rw, http.StatusOK, server) if provider, ok := currentConfigurations[providerId]; ok {
} else { fmt.Printf("provider OK\n")
http.NotFound(rw, r) if backend, ok := provider.Backends[backendId]; ok {
fmt.Printf("backend OK\n")
if server, ok := backend.Servers[serverId]; ok {
fmt.Printf("server OK\n")
templatesRenderer.JSON(rw, http.StatusOK, server)
return
}
} }
} else {
http.NotFound(rw, r)
} }
http.NotFound(rw, r)
} }