2017-05-23 18:56:10 +00:00
|
|
|
package handler
|
2016-12-18 05:21:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2017-05-23 18:56:10 +00:00
|
|
|
|
|
|
|
"github.com/portainer/portainer"
|
|
|
|
httperror "github.com/portainer/portainer/http/error"
|
2016-12-18 05:21:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Handler is a collection of all the service handlers.
|
|
|
|
type Handler struct {
|
2017-05-23 18:56:10 +00:00
|
|
|
AuthHandler *AuthHandler
|
|
|
|
UserHandler *UserHandler
|
|
|
|
TeamHandler *TeamHandler
|
|
|
|
TeamMembershipHandler *TeamMembershipHandler
|
|
|
|
EndpointHandler *EndpointHandler
|
2017-06-20 11:00:32 +00:00
|
|
|
RegistryHandler *RegistryHandler
|
|
|
|
DockerHubHandler *DockerHubHandler
|
2017-05-23 18:56:10 +00:00
|
|
|
ResourceHandler *ResourceHandler
|
2017-06-01 08:14:55 +00:00
|
|
|
StatusHandler *StatusHandler
|
2017-05-23 18:56:10 +00:00
|
|
|
SettingsHandler *SettingsHandler
|
|
|
|
TemplatesHandler *TemplatesHandler
|
|
|
|
DockerHandler *DockerHandler
|
|
|
|
WebSocketHandler *WebSocketHandler
|
|
|
|
UploadHandler *UploadHandler
|
|
|
|
FileHandler *FileHandler
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ErrInvalidJSON defines an error raised the app is unable to parse request data
|
|
|
|
ErrInvalidJSON = portainer.Error("Invalid JSON")
|
|
|
|
// ErrInvalidRequestFormat defines an error raised when the format of the data sent in a request is not valid
|
|
|
|
ErrInvalidRequestFormat = portainer.Error("Invalid request data format")
|
2017-03-12 16:24:15 +00:00
|
|
|
// ErrInvalidQueryFormat defines an error raised when the data sent in the query or the URL is invalid
|
|
|
|
ErrInvalidQueryFormat = portainer.Error("Invalid query format")
|
|
|
|
// ErrEmptyResponseBody defines an error raised when portainer excepts to parse the body of a HTTP response and there is nothing to parse
|
2017-05-23 18:56:10 +00:00
|
|
|
// ErrEmptyResponseBody = portainer.Error("Empty response body")
|
2016-12-18 05:21:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ServeHTTP delegates a request to the appropriate subhandler.
|
|
|
|
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if strings.HasPrefix(r.URL.Path, "/api/auth") {
|
|
|
|
http.StripPrefix("/api", h.AuthHandler).ServeHTTP(w, r)
|
|
|
|
} else if strings.HasPrefix(r.URL.Path, "/api/users") {
|
|
|
|
http.StripPrefix("/api", h.UserHandler).ServeHTTP(w, r)
|
2017-05-23 18:56:10 +00:00
|
|
|
} else if strings.HasPrefix(r.URL.Path, "/api/teams") {
|
|
|
|
http.StripPrefix("/api", h.TeamHandler).ServeHTTP(w, r)
|
|
|
|
} else if strings.HasPrefix(r.URL.Path, "/api/team_memberships") {
|
|
|
|
http.StripPrefix("/api", h.TeamMembershipHandler).ServeHTTP(w, r)
|
2016-12-25 20:34:02 +00:00
|
|
|
} else if strings.HasPrefix(r.URL.Path, "/api/endpoints") {
|
|
|
|
http.StripPrefix("/api", h.EndpointHandler).ServeHTTP(w, r)
|
2017-06-20 11:00:32 +00:00
|
|
|
} else if strings.HasPrefix(r.URL.Path, "/api/registries") {
|
|
|
|
http.StripPrefix("/api", h.RegistryHandler).ServeHTTP(w, r)
|
|
|
|
} else if strings.HasPrefix(r.URL.Path, "/api/dockerhub") {
|
|
|
|
http.StripPrefix("/api", h.DockerHubHandler).ServeHTTP(w, r)
|
2017-05-23 18:56:10 +00:00
|
|
|
} else if strings.HasPrefix(r.URL.Path, "/api/resource_controls") {
|
|
|
|
http.StripPrefix("/api", h.ResourceHandler).ServeHTTP(w, r)
|
2016-12-18 05:21:29 +00:00
|
|
|
} else if strings.HasPrefix(r.URL.Path, "/api/settings") {
|
|
|
|
http.StripPrefix("/api", h.SettingsHandler).ServeHTTP(w, r)
|
2017-06-01 08:14:55 +00:00
|
|
|
} else if strings.HasPrefix(r.URL.Path, "/api/status") {
|
|
|
|
http.StripPrefix("/api", h.StatusHandler).ServeHTTP(w, r)
|
2016-12-18 05:21:29 +00:00
|
|
|
} else if strings.HasPrefix(r.URL.Path, "/api/templates") {
|
|
|
|
http.StripPrefix("/api", h.TemplatesHandler).ServeHTTP(w, r)
|
2016-12-25 20:34:02 +00:00
|
|
|
} else if strings.HasPrefix(r.URL.Path, "/api/upload") {
|
|
|
|
http.StripPrefix("/api", h.UploadHandler).ServeHTTP(w, r)
|
2016-12-18 05:21:29 +00:00
|
|
|
} else if strings.HasPrefix(r.URL.Path, "/api/websocket") {
|
|
|
|
http.StripPrefix("/api", h.WebSocketHandler).ServeHTTP(w, r)
|
|
|
|
} else if strings.HasPrefix(r.URL.Path, "/api/docker") {
|
|
|
|
http.StripPrefix("/api/docker", h.DockerHandler).ServeHTTP(w, r)
|
|
|
|
} else if strings.HasPrefix(r.URL.Path, "/") {
|
|
|
|
h.FileHandler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// encodeJSON encodes v to w in JSON format. Error() is called if encoding fails.
|
|
|
|
func encodeJSON(w http.ResponseWriter, v interface{}, logger *log.Logger) {
|
|
|
|
if err := json.NewEncoder(w).Encode(v); err != nil {
|
2017-05-23 18:56:10 +00:00
|
|
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, logger)
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
|
|
|
}
|