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

print api endpoints

parent a9c83850
No related branches found
No related tags found
No related merge requests found
...@@ -25,15 +25,23 @@ ...@@ -25,15 +25,23 @@
package main package main
import ( import (
"fmt" "encoding/json"
"net/http" "net/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
type webAPIv1Endpoint struct {
Methods []string `json:"allowed-methods"`
}
var (
webAPIv1Endpoints = make(map[string]*webAPIv1Endpoint)
)
func webAPIv1_Index(w http.ResponseWriter, r *http.Request) { func webAPIv1_Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "welcome to /api/v1") w.Header().Set("Content-Type", "application/json")
// TODO: print endpioint listing json.NewEncoder(w).Encode(webAPIv1Endpoints)
} }
func webAPIv1_Groups_List(w http.ResponseWriter, r *http.Request) { func webAPIv1_Groups_List(w http.ResponseWriter, r *http.Request) {
...@@ -85,11 +93,11 @@ func webAPIv1__methodNotAllowed(w http.ResponseWriter, r *http.Request) { ...@@ -85,11 +93,11 @@ func webAPIv1__methodNotAllowed(w http.ResponseWriter, r *http.Request) {
} }
func webAPIv1InstallHandler(r *mux.Router) { func webAPIv1InstallHandler(r *mux.Router) {
r.HandleFunc("", webAPIv1_Index) // Groups
r.HandleFunc("/", webAPIv1_Index) r.HandleFunc("/groups", webAPIv1_Groups_List).Methods("GET")
r.HandleFunc("/groups", webAPIv1__methodNotAllowed)
r.HandleFunc("/groups", webAPIv1_Groups_List)
// Playlists
r.HandleFunc("/groups/{group-id}/playlists", webAPIv1_PlaylistsOfGroup_List).Methods("GET") 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_PlaylistsOfGroup_Create).Methods("POST")
r.HandleFunc("/groups/{group-id}/playlists", webAPIv1__methodNotAllowed) r.HandleFunc("/groups/{group-id}/playlists", webAPIv1__methodNotAllowed)
...@@ -98,11 +106,35 @@ func webAPIv1InstallHandler(r *mux.Router) { ...@@ -98,11 +106,35 @@ func webAPIv1InstallHandler(r *mux.Router) {
r.HandleFunc("/groups/{group-id}/playlists/{playlist-id}", webAPIv1_PlaylistsOfGroup_Delete).Methods("DELETE") 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}", webAPIv1__methodNotAllowed)
// Files
r.HandleFunc("/groups/{group-id}/files", webAPIv1_FilesOfGroup_List).Methods("GET") r.HandleFunc("/groups/{group-id}/files", webAPIv1_FilesOfGroup_List).Methods("GET")
r.HandleFunc("/groups/{group-id}/files", webAPIv1_FilesOfGroup_Create).Methods("POST") r.HandleFunc("/groups/{group-id}/files", webAPIv1_FilesOfGroup_Create).Methods("POST")
r.HandleFunc("/groups/{group-id}/files", webAPIv1__methodNotAllowed) r.HandleFunc("/groups/{group-id}/files", webAPIv1__methodNotAllowed)
r.HandleFunc("/groups/{group-id}/files/{file-id}", webAPIv1_FilesOfGroup_Read).Methods("GET") 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_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_FilesOfGroup_Delete).Methods("DELETE")
r.HandleFunc("/groups/{group-id}/files/{file-id}", webAPIv1__methodNotAllowed) r.HandleFunc("/groups/{group-id}/files/{file-id}", webAPIv1__methodNotAllowed)
// Index
r.HandleFunc("/", webAPIv1_Index).Methods("GET")
r.HandleFunc("/", webAPIv1__methodNotAllowed)
r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
p, err := route.GetPathTemplate()
if err != nil {
return nil
}
m, err := route.GetMethods()
if err != nil || len(m) == 0 {
return err
}
if e, exists := webAPIv1Endpoints[p]; exists {
e.Methods = append(e.Methods, m...)
} else {
webAPIv1Endpoints[p] = &webAPIv1Endpoint{m}
}
return nil
})
} }
...@@ -36,7 +36,7 @@ import ( ...@@ -36,7 +36,7 @@ import (
const ( const (
WebUIPathPrefix = "/ui/" WebUIPathPrefix = "/ui/"
WebAPIv1Prefix = "/api/v1" WebAPIv1Prefix = "/api/v1/"
) )
func runWeb(ln net.Listener) error { func runWeb(ln net.Listener) error {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment