Skip to content
Snippets Groups Projects
Commit a6eeae4b authored by Christian Pointner's avatar Christian Pointner
Browse files

make use of gorilla MethodHandler

parent 82fb07ab
No related branches found
No related tags found
No related merge requests found
......@@ -26,11 +26,11 @@ package v1
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"gitlab.servus.at/autoradio/tank/importer"
"gitlab.servus.at/autoradio/tank/store"
......@@ -42,14 +42,6 @@ func sendWebResponse(w http.ResponseWriter, status int, respdata interface{}) {
json.NewEncoder(w).Encode(respdata) // TODO: Error Handling?
}
func notFound(w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotFound, ErrorResponse{"not found"})
}
func methodNotAllowed(w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusMethodNotAllowed, ErrorResponse{fmt.Sprintf("method %s is not allowed here", r.Method)})
}
type API struct {
store *store.Store
importer *importer.Importer
......@@ -87,47 +79,52 @@ func (api *API) ListEndpoints() http.Handler {
}
func InstallHandler(r *mux.Router, st *store.Store, im *importer.Importer, infoLog, errLog, dbgLog *log.Logger) {
r.NotFoundHandler = http.HandlerFunc(notFound)
r.MethodNotAllowedHandler = http.HandlerFunc(methodNotAllowed)
api := NewAPI(st, im, infoLog, errLog, dbgLog)
// Groups
r.Handle("/groups", api.ListGroups()).Methods("GET")
r.HandleFunc("/groups", methodNotAllowed)
groupsHandler := make(handlers.MethodHandler)
groupsHandler["GET"] = api.ListGroups()
r.Handle("/groups", groupsHandler)
// Imports
r.Handle("/groups/{group-id}/imports", api.ListImportsOfGroup()).Methods("GET")
r.Handle("/groups/{group-id}/imports", api.CreateImportForGroup()).Methods("POST")
r.HandleFunc("/groups/{group-id}/imports", methodNotAllowed)
importsHandler := make(handlers.MethodHandler)
importsHandler["GET"] = api.ListImportsOfGroup()
importsHandler["POST"] = api.CreateImportForGroup()
r.Handle("/groups/{group-id}/imports", importsHandler)
r.Handle("/groups/{group-id}/imports/{import-id}", api.ReadImportOfGroup()).Methods("GET")
r.Handle("/groups/{group-id}/imports/{import-id}", api.UploadFileToImportOfGroup()).Methods("PUT")
r.Handle("/groups/{group-id}/imports/{import-id}", api.DeleteImportOfGroup()).Methods("DELETE")
r.HandleFunc("/groups/{group-id}/imports/{import-id}", methodNotAllowed)
importHandler := make(handlers.MethodHandler)
importHandler["GET"] = api.ReadImportOfGroup()
importHandler["PUT"] = api.UploadFileToImportOfGroup()
importHandler["DELETE"] = api.DeleteImportOfGroup()
r.Handle("/groups/{group-id}/imports/{import-id}", importHandler)
// Files
r.Handle("/groups/{group-id}/files", api.ListFilesOfGroup()).Methods("GET")
r.HandleFunc("/groups/{group-id}/files", methodNotAllowed)
filesHandler := make(handlers.MethodHandler)
filesHandler["GET"] = api.ListFilesOfGroup()
r.Handle("/groups/{group-id}/files", filesHandler)
r.Handle("/groups/{group-id}/files/{file-id}", api.ReadFileOfGroup()).Methods("GET")
r.Handle("/groups/{group-id}/files/{file-id}", api.UpdateFileOfGroup()).Methods("PUT")
r.Handle("/groups/{group-id}/files/{file-id}", api.DeleteFileOfGroup()).Methods("DELETE")
r.HandleFunc("/groups/{group-id}/files/{file-id}", methodNotAllowed)
fileHandler := make(handlers.MethodHandler)
fileHandler["GET"] = api.ReadFileOfGroup()
fileHandler["PUT"] = api.UpdateFileOfGroup()
fileHandler["DELETE"] = api.DeleteFileOfGroup()
r.Handle("/groups/{group-id}/files/{file-id}", fileHandler)
// Playlists
r.Handle("/groups/{group-id}/playlists", api.ListPlaylistsOfGroup()).Methods("GET")
r.Handle("/groups/{group-id}/playlists", api.CreatePlaylistForGroup()).Methods("POST")
r.HandleFunc("/groups/{group-id}/playlists", methodNotAllowed)
playlistsHandler := make(handlers.MethodHandler)
playlistsHandler["GET"] = api.ListPlaylistsOfGroup()
playlistsHandler["POST"] = api.CreatePlaylistForGroup()
r.Handle("/groups/{group-id}/playlists", playlistsHandler)
r.Handle("/groups/{group-id}/playlists/{playlist-id}", api.ReadPlaylistOfGroup()).Methods("GET")
r.Handle("/groups/{group-id}/playlists/{playlist-id}", api.UpdatePlaylistOfGroup()).Methods("PUT")
r.Handle("/groups/{group-id}/playlists/{playlist-id}", api.DeletePlaylistOfGroup()).Methods("DELETE")
r.HandleFunc("/groups/{group-id}/playlists/{playlist-id}", methodNotAllowed)
playlistHandler := make(handlers.MethodHandler)
playlistHandler["GET"] = api.ReadPlaylistOfGroup()
playlistHandler["PUT"] = api.UpdatePlaylistOfGroup()
playlistHandler["DELETE"] = api.DeletePlaylistOfGroup()
r.Handle("/groups/{group-id}/playlists/{playlist-id}", playlistHandler)
// Index
r.Handle("/", api.ListEndpoints()).Methods("GET")
r.HandleFunc("/", methodNotAllowed)
indexHandler := make(handlers.MethodHandler)
indexHandler["GET"] = api.ListEndpoints() // TODO: fix this
r.Handle("/", indexHandler)
r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
p, err := route.GetPathTemplate()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment