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

web api now has logging too

parent 60e99dbe
No related branches found
No related tags found
No related merge requests found
......@@ -27,6 +27,8 @@ package v1
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/gorilla/mux"
......@@ -34,11 +36,6 @@ import (
"gitlab.servus.at/autoradio/tank/store"
)
var (
apiListing = APIListing{make(map[string]*APIEndpoint)}
)
// common
func sendWebResponse(w http.ResponseWriter, status int, respdata interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
......@@ -53,125 +50,83 @@ func methodNotAllowed(w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusMethodNotAllowed, ErrorResponse{fmt.Sprintf("method %s is not allowed here", r.Method)})
}
func listEndpoints(w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusOK, apiListing)
}
// Groups
func listGroups(st *store.Store, im *importer.SessionStore, w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"listing groups not yet implemented"})
}
// Imports
func listImportsOfGroup(st *store.Store, im *importer.SessionStore, w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"listing imports of group not yet implemented"})
}
func createImportForGroup(st *store.Store, im *importer.SessionStore, w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"creating imports in group not yet implemented"})
}
func readImportOfGroup(st *store.Store, im *importer.SessionStore, w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"reading imports of group not yet implemented"})
}
func uploadFileToImportOfGroup(st *store.Store, im *importer.SessionStore, w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"uploading files to imports of group not yet implemented"})
}
func deleteImportOfGroup(st *store.Store, im *importer.SessionStore, w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"deleting imports from group not yet implemented"})
}
// Files
func listFilesOfGroup(st *store.Store, im *importer.SessionStore, w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"listing files of group not yet implemented"})
}
func readFileOfGroup(st *store.Store, im *importer.SessionStore, w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"reading file of group not yet implemented"})
}
func updateFileOfGroup(st *store.Store, im *importer.SessionStore, w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"updateing file of group not yet implemented"})
}
func deleteFileOfGroup(st *store.Store, im *importer.SessionStore, w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"deleting file from group not yet implemented"})
}
// Playlists
func listPlaylistsOfGroup(st *store.Store, im *importer.SessionStore, w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"listing playlists of group not yet implemented"})
}
func createPlaylistForGroup(st *store.Store, im *importer.SessionStore, w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"creating playlists in group not yet implemented"})
}
func readPlaylistOfGroup(st *store.Store, im *importer.SessionStore, w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"reading playlist of group not yet implemented"})
}
func updatePlaylistOfGroup(st *store.Store, im *importer.SessionStore, w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"updateing playlist of group not yet implemented"})
}
func deletePlaylistOfGroup(st *store.Store, im *importer.SessionStore, w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"deleting playlist from group not yet implemented"})
}
type apiHandlerFunc func(*store.Store, *importer.SessionStore, http.ResponseWriter, *http.Request)
type apiHandler struct {
type API struct {
store *store.Store
importer *importer.SessionStore
H apiHandlerFunc
}
func (self apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
self.H(self.store, self.importer, w, r)
infoLog *log.Logger
errLog *log.Logger
dbgLog *log.Logger
listing APIListing
}
func NewAPI(st *store.Store, im *importer.SessionStore, infoLog, errLog, dbgLog *log.Logger) (api *API) {
if infoLog == nil {
infoLog = log.New(ioutil.Discard, "", 0)
}
if errLog == nil {
errLog = log.New(ioutil.Discard, "", 0)
}
if dbgLog == nil {
dbgLog = log.New(ioutil.Discard, "", 0)
}
api = &API{}
api.store = st
api.importer = im
api.infoLog = infoLog
api.errLog = errLog
api.dbgLog = dbgLog
api.listing.Endpoints = make(map[string]*APIEndpoint)
return
}
func (api *API) ListEndpoints() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusOK, api.listing)
})
}
func InstallHandler(r *mux.Router, st *store.Store, im *importer.SessionStore) {
func InstallHandler(r *mux.Router, st *store.Store, im *importer.SessionStore, infoLog, errLog, dbgLog *log.Logger) {
r.NotFoundHandler = http.HandlerFunc(notFound)
r.MethodNotAllowedHandler = http.HandlerFunc(methodNotAllowed)
api := NewAPI(st, im, infoLog, errLog, dbgLog) // TODO logging
// Groups
r.Handle("/groups", apiHandler{st, im, listGroups}).Methods("GET")
r.Handle("/groups", api.ListGroups()).Methods("GET")
r.HandleFunc("/groups", methodNotAllowed)
// Imports
r.Handle("/groups/{group-id}/imports", apiHandler{st, im, listImportsOfGroup}).Methods("GET")
r.Handle("/groups/{group-id}/imports", apiHandler{st, im, createImportForGroup}).Methods("POST")
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)
r.Handle("/groups/{group-id}/imports/{import-id}", apiHandler{st, im, readImportOfGroup}).Methods("GET")
r.Handle("/groups/{group-id}/imports/{import-id}", apiHandler{st, im, uploadFileToImportOfGroup}).Methods("PUT")
r.Handle("/groups/{group-id}/imports/{import-id}", apiHandler{st, im, deleteImportOfGroup}).Methods("DELETE")
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)
// Files
r.Handle("/groups/{group-id}/files", apiHandler{st, im, listFilesOfGroup}).Methods("GET")
r.Handle("/groups/{group-id}/files", api.ListFilesOfGroup()).Methods("GET")
r.HandleFunc("/groups/{group-id}/files", methodNotAllowed)
r.Handle("/groups/{group-id}/files/{file-id}", apiHandler{st, im, readFileOfGroup}).Methods("GET")
r.Handle("/groups/{group-id}/files/{file-id}", apiHandler{st, im, updateFileOfGroup}).Methods("PUT")
r.Handle("/groups/{group-id}/files/{file-id}", apiHandler{st, im, deleteFileOfGroup}).Methods("DELETE")
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)
// Playlists
r.Handle("/groups/{group-id}/playlists", apiHandler{st, im, listPlaylistsOfGroup}).Methods("GET")
r.Handle("/groups/{group-id}/playlists", apiHandler{st, im, createPlaylistForGroup}).Methods("POST")
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)
r.Handle("/groups/{group-id}/playlists/{playlist-id}", apiHandler{st, im, readPlaylistOfGroup}).Methods("GET")
r.Handle("/groups/{group-id}/playlists/{playlist-id}", apiHandler{st, im, updatePlaylistOfGroup}).Methods("PUT")
r.Handle("/groups/{group-id}/playlists/{playlist-id}", apiHandler{st, im, deletePlaylistOfGroup}).Methods("DELETE")
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)
// Index
r.HandleFunc("/", listEndpoints).Methods("GET")
r.Handle("/", api.ListEndpoints()).Methods("GET")
r.HandleFunc("/", methodNotAllowed)
r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
......@@ -185,10 +140,10 @@ func InstallHandler(r *mux.Router, st *store.Store, im *importer.SessionStore) {
return err
}
if e, exists := apiListing.Endpoints[p]; exists {
if e, exists := api.listing.Endpoints[p]; exists {
e.Methods = append(e.Methods, m...)
} else {
apiListing.Endpoints[p] = &APIEndpoint{m}
api.listing.Endpoints[p] = &APIEndpoint{m}
}
return nil
})
......
//
// tank
//
// Import and Playlist Daemon for autoradio project
//
//
// Copyright (C) 2017 Christian Pointner <equinox@helsinki.at>
//
// This file is part of tank.
//
// tank is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// tank is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with tank. If not, see <http://www.gnu.org/licenses/>.
//
package v1
import (
"net/http"
)
func (api *API) ListFilesOfGroup() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"listing files of group not yet implemented"})
})
}
func (api *API) ReadFileOfGroup() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"reading file of group not yet implemented"})
})
}
func (api *API) UpdateFileOfGroup() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"updateing file of group not yet implemented"})
})
}
func (api *API) DeleteFileOfGroup() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"deleting file from group not yet implemented"})
})
}
//
// tank
//
// Import and Playlist Daemon for autoradio project
//
//
// Copyright (C) 2017 Christian Pointner <equinox@helsinki.at>
//
// This file is part of tank.
//
// tank is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// tank is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with tank. If not, see <http://www.gnu.org/licenses/>.
//
package v1
import (
"net/http"
)
func (api *API) ListGroups() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"listing groups not yet implemented"})
})
}
//
// tank
//
// Import and Playlist Daemon for autoradio project
//
//
// Copyright (C) 2017 Christian Pointner <equinox@helsinki.at>
//
// This file is part of tank.
//
// tank is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// tank is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with tank. If not, see <http://www.gnu.org/licenses/>.
//
package v1
import (
"net/http"
)
func (api *API) ListImportsOfGroup() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"listing imports of group not yet implemented"})
})
}
func (api *API) CreateImportForGroup() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"creating imports in group not yet implemented"})
})
}
func (api *API) ReadImportOfGroup() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"reading imports of group not yet implemented"})
})
}
func (api *API) UploadFileToImportOfGroup() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"uploading files to imports of group not yet implemented"})
})
}
func (api *API) DeleteImportOfGroup() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"deleting imports from group not yet implemented"})
})
}
//
// tank
//
// Import and Playlist Daemon for autoradio project
//
//
// Copyright (C) 2017 Christian Pointner <equinox@helsinki.at>
//
// This file is part of tank.
//
// tank is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// tank is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with tank. If not, see <http://www.gnu.org/licenses/>.
//
package v1
import (
"net/http"
)
func (api *API) ListPlaylistsOfGroup() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"listing playlists of group not yet implemented"})
})
}
func (api *API) CreatePlaylistForGroup() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"creating playlists in group not yet implemented"})
})
}
func (api *API) ReadPlaylistOfGroup() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"reading playlist of group not yet implemented"})
})
}
func (api *API) UpdatePlaylistOfGroup() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"updateing playlist of group not yet implemented"})
})
}
func (api *API) DeletePlaylistOfGroup() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sendWebResponse(w, http.StatusNotImplemented, ErrorResponse{"deleting playlist from group not yet implemented"})
})
}
......@@ -48,7 +48,7 @@ func runWeb(ln net.Listener, st *store.Store, im *importer.SessionStore) error {
r.Handle("/", http.RedirectHandler(WebUIPathPrefix, http.StatusSeeOther))
r.PathPrefix(WebUIPathPrefix).Handler(http.StripPrefix(WebUIPathPrefix, http.FileServer(assetFS())))
v1.InstallHandler(r.PathPrefix(WebAPIv1Prefix).Subrouter(), st, im)
v1.InstallHandler(r.PathPrefix(WebAPIv1Prefix).Subrouter(), st, im, infoLog, errLog, dbgLog)
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