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

moved api to seperate package

parent 4f162f1b
No related branches found
No related tags found
No related merge requests found
......@@ -22,7 +22,7 @@
// along with tank. If not, see <http://www.gnu.org/licenses/>.
//
package main
package v1
import (
"encoding/json"
......@@ -31,125 +31,126 @@ import (
"github.com/gorilla/mux"
)
type webAPIv1Endpoint struct {
type endpoint struct {
Methods []string `json:"allowed-methods"`
}
var (
webAPIv1Endpoints = make(map[string]*webAPIv1Endpoint)
endpoints = make(map[string]*endpoint)
)
// common
func webAPIv1_Index(w http.ResponseWriter, r *http.Request) {
func listEndpoints(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(webAPIv1Endpoints)
json.NewEncoder(w).Encode(endpoints)
}
func webAPIv1__methodNotAllowed(w http.ResponseWriter, r *http.Request) {
func methodNotAllowed(w http.ResponseWriter, r *http.Request) {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
// Groups
func webAPIv1_Groups_List(w http.ResponseWriter, r *http.Request) {
func listGroups(w http.ResponseWriter, r *http.Request) {
http.Error(w, "listing groups not yet implemented", http.StatusNotImplemented)
}
// Imports
func webAPIv1_ImportsOfGroup_List(w http.ResponseWriter, r *http.Request) {
func listImportsOfGroup(w http.ResponseWriter, r *http.Request) {
http.Error(w, "listing imports of group not yet implemented", http.StatusNotImplemented)
}
func webAPIv1_ImportsOfGroup_Create(w http.ResponseWriter, r *http.Request) {
func createImportForGroup(w http.ResponseWriter, r *http.Request) {
http.Error(w, "creating imports in group not yet implemented", http.StatusNotImplemented)
}
func webAPIv1_ImportsOfGroup_Read(w http.ResponseWriter, r *http.Request) {
func readImportOfGroup(w http.ResponseWriter, r *http.Request) {
http.Error(w, "reading imports of group not yet implemented", http.StatusNotImplemented)
}
func webAPIv1_ImportsOfGroup_Upload(w http.ResponseWriter, r *http.Request) {
func uploadFileToImportOfGroup(w http.ResponseWriter, r *http.Request) {
http.Error(w, "uploading files to imports of group not yet implemented", http.StatusNotImplemented)
}
func webAPIv1_ImportsOfGroup_Delete(w http.ResponseWriter, r *http.Request) {
func deleteImportOfGroup(w http.ResponseWriter, r *http.Request) {
http.Error(w, "deleting imports from group not yet implemented", http.StatusNotImplemented)
}
// Files
func webAPIv1_FilesOfGroup_List(w http.ResponseWriter, r *http.Request) {
func listFilesOfGroup(w http.ResponseWriter, r *http.Request) {
http.Error(w, "listing files of group not yet implemented", http.StatusNotImplemented)
}
func webAPIv1_FilesOfGroup_Read(w http.ResponseWriter, r *http.Request) {
func readFileOfGroup(w http.ResponseWriter, r *http.Request) {
http.Error(w, "reading file of group not yet implemented", http.StatusNotImplemented)
}
func webAPIv1_FilesOfGroup_Update(w http.ResponseWriter, r *http.Request) {
func updateFileOfGroup(w http.ResponseWriter, r *http.Request) {
http.Error(w, "updateing file of group not yet implemented", http.StatusNotImplemented)
}
func webAPIv1_FilesOfGroup_Delete(w http.ResponseWriter, r *http.Request) {
func deleteFileOfGroup(w http.ResponseWriter, r *http.Request) {
http.Error(w, "deleting file from group not yet implemented", http.StatusNotImplemented)
}
// Playlists
func webAPIv1_PlaylistsOfGroup_List(w http.ResponseWriter, r *http.Request) {
func listPlaylistsOfGroup(w http.ResponseWriter, r *http.Request) {
http.Error(w, "listing playlists of group not yet implemented", http.StatusNotImplemented)
}
func webAPIv1_PlaylistsOfGroup_Create(w http.ResponseWriter, r *http.Request) {
func createPlaylistForGroup(w http.ResponseWriter, r *http.Request) {
http.Error(w, "creating playlists in group not yet implemented", http.StatusNotImplemented)
}
func webAPIv1_PlaylistsOfGroup_Read(w http.ResponseWriter, r *http.Request) {
func readPlaylistOfGroup(w http.ResponseWriter, r *http.Request) {
http.Error(w, "reading playlist of group not yet implemented", http.StatusNotImplemented)
}
func webAPIv1_PlaylistsOfGroup_Update(w http.ResponseWriter, r *http.Request) {
func updatePlaylistOfGroup(w http.ResponseWriter, r *http.Request) {
http.Error(w, "updateing playlist of group not yet implemented", http.StatusNotImplemented)
}
func webAPIv1_PlaylistsOfGroup_Delete(w http.ResponseWriter, r *http.Request) {
func deletePlaylistOfGroup(w http.ResponseWriter, r *http.Request) {
http.Error(w, "deleting playlist from group not yet implemented", http.StatusNotImplemented)
}
func webAPIv1InstallHandler(r *mux.Router) {
func InstallHandler(r *mux.Router) {
// Groups
r.HandleFunc("/groups", webAPIv1_Groups_List).Methods("GET")
r.HandleFunc("/groups", webAPIv1__methodNotAllowed)
r.HandleFunc("/groups", listGroups).Methods("GET")
r.HandleFunc("/groups", methodNotAllowed)
// Imports
r.HandleFunc("/groups/{group-id}/imports", webAPIv1_ImportsOfGroup_List).Methods("GET")
r.HandleFunc("/groups/{group-id}/imports", webAPIv1_ImportsOfGroup_Create).Methods("POST")
r.HandleFunc("/groups/{group-id}/imports", webAPIv1__methodNotAllowed)
r.HandleFunc("/groups/{group-id}/imports", listImportsOfGroup).Methods("GET")
r.HandleFunc("/groups/{group-id}/imports", createImportForGroup).Methods("POST")
r.HandleFunc("/groups/{group-id}/imports", methodNotAllowed)
r.HandleFunc("/groups/{group-id}/imports/{import-id}", webAPIv1_ImportsOfGroup_Read).Methods("GET")
r.HandleFunc("/groups/{group-id}/imports/{import-id}", webAPIv1_ImportsOfGroup_Upload).Methods("PUT")
r.HandleFunc("/groups/{group-id}/imports/{import-id}", webAPIv1_ImportsOfGroup_Delete).Methods("DELETE")
r.HandleFunc("/groups/{group-id}/imports/{import-id}", webAPIv1__methodNotAllowed)
r.HandleFunc("/groups/{group-id}/imports/{import-id}", readImportOfGroup).Methods("GET")
r.HandleFunc("/groups/{group-id}/imports/{import-id}", uploadFileToImportOfGroup).Methods("PUT")
r.HandleFunc("/groups/{group-id}/imports/{import-id}", deleteImportOfGroup).Methods("DELETE")
r.HandleFunc("/groups/{group-id}/imports/{import-id}", methodNotAllowed)
// Files
r.HandleFunc("/groups/{group-id}/files", webAPIv1_FilesOfGroup_List).Methods("GET")
r.HandleFunc("/groups/{group-id}/files", webAPIv1__methodNotAllowed)
r.HandleFunc("/groups/{group-id}/files", listFilesOfGroup).Methods("GET")
r.HandleFunc("/groups/{group-id}/files", methodNotAllowed)
r.HandleFunc("/groups/{group-id}/files/{file-id}", webAPIv1_FilesOfGroup_Read).Methods("GET")
r.HandleFunc("/groups/{group-id}/files/{file-id}", webAPIv1_FilesOfGroup_Update).Methods("PUT")
r.HandleFunc("/groups/{group-id}/files/{file-id}", webAPIv1_FilesOfGroup_Delete).Methods("DELETE")
r.HandleFunc("/groups/{group-id}/files/{file-id}", webAPIv1__methodNotAllowed)
r.HandleFunc("/groups/{group-id}/files/{file-id}", readFileOfGroup).Methods("GET")
r.HandleFunc("/groups/{group-id}/files/{file-id}", updateFileOfGroup).Methods("PUT")
r.HandleFunc("/groups/{group-id}/files/{file-id}", deleteFileOfGroup).Methods("DELETE")
r.HandleFunc("/groups/{group-id}/files/{file-id}", methodNotAllowed)
// Playlists
r.HandleFunc("/groups/{group-id}/playlists", webAPIv1_PlaylistsOfGroup_List).Methods("GET")
r.HandleFunc("/groups/{group-id}/playlists", webAPIv1_PlaylistsOfGroup_Create).Methods("POST")
r.HandleFunc("/groups/{group-id}/playlists", webAPIv1__methodNotAllowed)
r.HandleFunc("/groups/{group-id}/playlists", listPlaylistsOfGroup).Methods("GET")
r.HandleFunc("/groups/{group-id}/playlists", createPlaylistForGroup).Methods("POST")
r.HandleFunc("/groups/{group-id}/playlists", methodNotAllowed)
r.HandleFunc("/groups/{group-id}/playlists/{playlist-id}", webAPIv1_PlaylistsOfGroup_Read).Methods("GET")
r.HandleFunc("/groups/{group-id}/playlists/{playlist-id}", webAPIv1_PlaylistsOfGroup_Update).Methods("PUT")
r.HandleFunc("/groups/{group-id}/playlists/{playlist-id}", webAPIv1_PlaylistsOfGroup_Delete).Methods("DELETE")
r.HandleFunc("/groups/{group-id}/playlists/{playlist-id}", webAPIv1__methodNotAllowed)
r.HandleFunc("/groups/{group-id}/playlists/{playlist-id}", readPlaylistOfGroup).Methods("GET")
r.HandleFunc("/groups/{group-id}/playlists/{playlist-id}", updatePlaylistOfGroup).Methods("PUT")
r.HandleFunc("/groups/{group-id}/playlists/{playlist-id}", deletePlaylistOfGroup).Methods("DELETE")
r.HandleFunc("/groups/{group-id}/playlists/{playlist-id}", methodNotAllowed)
// Index
r.HandleFunc("/", webAPIv1_Index).Methods("GET")
r.HandleFunc("/", webAPIv1__methodNotAllowed)
r.HandleFunc("/", listEndpoints).Methods("GET")
r.HandleFunc("/", methodNotAllowed)
r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
p, err := route.GetPathTemplate()
if err != nil {
......@@ -161,10 +162,10 @@ func webAPIv1InstallHandler(r *mux.Router) {
return err
}
if e, exists := webAPIv1Endpoints[p]; exists {
if e, exists := endpoints[p]; exists {
e.Methods = append(e.Methods, m...)
} else {
webAPIv1Endpoints[p] = &webAPIv1Endpoint{m}
endpoints[p] = &endpoint{m}
}
return nil
})
......
......@@ -32,6 +32,7 @@ import (
"time"
"github.com/gorilla/mux"
"gitlab.servus.at/autoradio/tank/api/v1"
)
const (
......@@ -45,7 +46,7 @@ func runWeb(ln net.Listener) error {
r.Handle("/", http.RedirectHandler(WebUIPathPrefix, http.StatusSeeOther))
r.PathPrefix(WebUIPathPrefix).Handler(http.StripPrefix(WebUIPathPrefix, http.FileServer(assetFS())))
webAPIv1InstallHandler(r.PathPrefix(WebAPIv1Prefix).Subrouter())
v1.InstallHandler(r.PathPrefix(WebAPIv1Prefix).Subrouter())
srv := &http.Server{
Handler: r,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment