Skip to content
Snippets Groups Projects
api.go 5.91 KiB
Newer Older
  • Learn to ignore specific revisions
  • //
    //  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 (
    	"encoding/json"
    	"net/http"
    
    	"github.com/gorilla/mux"
    )
    
    type endpoint struct {
    	Methods []string `json:"allowed-methods"`
    }
    
    var (
    	endpoints = make(map[string]*endpoint)
    )
    
    // common
    func listEndpoints(w http.ResponseWriter, r *http.Request) {
    	w.Header().Set("Content-Type", "application/json")
    	json.NewEncoder(w).Encode(endpoints)
    }
    
    func methodNotAllowed(w http.ResponseWriter, r *http.Request) {
    	http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
    }
    
    // Groups
    func listGroups(w http.ResponseWriter, r *http.Request) {
    	http.Error(w, "listing groups not yet implemented", http.StatusNotImplemented)
    }
    
    // Imports
    func listImportsOfGroup(w http.ResponseWriter, r *http.Request) {
    	http.Error(w, "listing imports of group not yet implemented", http.StatusNotImplemented)
    }
    
    func createImportForGroup(w http.ResponseWriter, r *http.Request) {
    	http.Error(w, "creating imports in group not yet implemented", http.StatusNotImplemented)
    }
    
    func readImportOfGroup(w http.ResponseWriter, r *http.Request) {
    	http.Error(w, "reading imports of group not yet implemented", http.StatusNotImplemented)
    }
    
    func uploadFileToImportOfGroup(w http.ResponseWriter, r *http.Request) {
    	http.Error(w, "uploading files to imports of group not yet implemented", http.StatusNotImplemented)
    }
    
    func deleteImportOfGroup(w http.ResponseWriter, r *http.Request) {
    	http.Error(w, "deleting imports from group not yet implemented", http.StatusNotImplemented)
    }
    
    // Files
    func listFilesOfGroup(w http.ResponseWriter, r *http.Request) {
    	http.Error(w, "listing files of group not yet implemented", http.StatusNotImplemented)
    }
    
    func readFileOfGroup(w http.ResponseWriter, r *http.Request) {
    	http.Error(w, "reading file of group not yet implemented", http.StatusNotImplemented)
    }
    
    func updateFileOfGroup(w http.ResponseWriter, r *http.Request) {
    	http.Error(w, "updateing file of group not yet implemented", http.StatusNotImplemented)
    }
    
    func deleteFileOfGroup(w http.ResponseWriter, r *http.Request) {
    	http.Error(w, "deleting file from group not yet implemented", http.StatusNotImplemented)
    }
    
    // Playlists
    func listPlaylistsOfGroup(w http.ResponseWriter, r *http.Request) {
    	http.Error(w, "listing playlists of group not yet implemented", http.StatusNotImplemented)
    }
    
    func createPlaylistForGroup(w http.ResponseWriter, r *http.Request) {
    	http.Error(w, "creating playlists in group not yet implemented", http.StatusNotImplemented)
    }
    
    func readPlaylistOfGroup(w http.ResponseWriter, r *http.Request) {
    	http.Error(w, "reading playlist of group not yet implemented", http.StatusNotImplemented)
    }
    
    func updatePlaylistOfGroup(w http.ResponseWriter, r *http.Request) {
    	http.Error(w, "updateing playlist of group not yet implemented", http.StatusNotImplemented)
    }
    
    func deletePlaylistOfGroup(w http.ResponseWriter, r *http.Request) {
    	http.Error(w, "deleting playlist from group not yet implemented", http.StatusNotImplemented)
    }
    
    func InstallHandler(r *mux.Router) {
    	// Groups
    	r.HandleFunc("/groups", listGroups).Methods("GET")
    	r.HandleFunc("/groups", methodNotAllowed)
    
    	// Imports
    	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}", 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", listFilesOfGroup).Methods("GET")
    	r.HandleFunc("/groups/{group-id}/files", 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", 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}", 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("/", 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 {
    			return nil
    		}
    
    		m, err := route.GetMethods()
    		if err != nil || len(m) == 0 {
    			return err
    		}
    
    		if e, exists := endpoints[p]; exists {
    			e.Methods = append(e.Methods, m...)
    		} else {
    			endpoints[p] = &endpoint{m}
    		}
    		return nil
    	})
    }