108 lines
2.4 KiB
Go
108 lines
2.4 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"crypto/sha512"
|
||
|
"encoding/hex"
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
"path/filepath"
|
||
|
|
||
|
"git.milar.in/milarin/envvars/v2"
|
||
|
"github.com/gorilla/mux"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
HttpIntf = envvars.String("HTTP_INTERFACE", "")
|
||
|
HttpPort = envvars.Uint16("HTTP_PORT", 80)
|
||
|
LibraryPath = envvars.String("LIBRARY_PATH", "/music")
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
r := mux.NewRouter()
|
||
|
|
||
|
r.HandleFunc("/all/", GetAllHandler).Methods("GET")
|
||
|
r.HandleFunc("/all/hash/", GetAllHashHandler).Methods("GET")
|
||
|
r.HandleFunc("/playlist/", GetPlaylistsHandler).Methods("GET")
|
||
|
r.HandleFunc("/playlist/{playlist}/", GetPlaylistHandler).Methods("GET")
|
||
|
r.HandleFunc("/playlist/{playlist}/song/{song}/", GetSongHandler).Methods("GET")
|
||
|
|
||
|
fmt.Printf("Starting music server on port %d\n", HttpPort)
|
||
|
if err := http.ListenAndServe(fmt.Sprintf("%s:%d", HttpIntf, HttpPort), r); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func GetAllHashHandler(w http.ResponseWriter, r *http.Request) {
|
||
|
playlists, err := GetAll()
|
||
|
if err != nil {
|
||
|
InternalServerError(w, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
b := &bytes.Buffer{}
|
||
|
if err := json.NewEncoder(b).Encode(playlists); err != nil {
|
||
|
InternalServerError(w, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
hash := sha512.Sum512(b.Bytes())
|
||
|
if _, err := fmt.Fprint(w, hex.EncodeToString(hash[:])); err != nil {
|
||
|
InternalServerError(w, err)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func GetAllHandler(w http.ResponseWriter, r *http.Request) {
|
||
|
playlists, err := GetAll()
|
||
|
if err != nil {
|
||
|
InternalServerError(w, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
w.Header().Add("Content-Type", "application/json")
|
||
|
if err := json.NewEncoder(w).Encode(playlists); err != nil {
|
||
|
InternalServerError(w, err)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func GetSongHandler(w http.ResponseWriter, r *http.Request) {
|
||
|
vars := mux.Vars(r)
|
||
|
playlist := vars["playlist"]
|
||
|
song := vars["song"]
|
||
|
|
||
|
http.ServeFile(w, r, filepath.Join(LibraryPath, playlist, song))
|
||
|
}
|
||
|
|
||
|
func GetPlaylistHandler(w http.ResponseWriter, r *http.Request) {
|
||
|
playlist := mux.Vars(r)["playlist"]
|
||
|
|
||
|
songs, err := GetSongs(playlist)
|
||
|
if err != nil {
|
||
|
InternalServerError(w, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
w.Header().Add("Content-Type", "application/json")
|
||
|
if err := json.NewEncoder(w).Encode(songs); err != nil {
|
||
|
InternalServerError(w, err)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func GetPlaylistsHandler(w http.ResponseWriter, r *http.Request) {
|
||
|
playlists, err := GetPlaylists()
|
||
|
if err != nil {
|
||
|
InternalServerError(w, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
w.Header().Add("Content-Type", "application/json")
|
||
|
if err := json.NewEncoder(w).Encode(playlists); err != nil {
|
||
|
InternalServerError(w, err)
|
||
|
return
|
||
|
}
|
||
|
}
|