add routes
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/sentriz/gonic/subsonic"
|
||||
@@ -15,38 +16,60 @@ type Controller struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func getStrParam(r *http.Request, key string) string {
|
||||
return r.URL.Query().Get(key)
|
||||
}
|
||||
|
||||
func getIntParam(r *http.Request, key string) (int, error) {
|
||||
strVal := r.URL.Query().Get(key)
|
||||
if strVal == "" {
|
||||
return 0, fmt.Errorf("no param with key `%s`", key)
|
||||
}
|
||||
val, err := strconv.Atoi(strVal)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("not an int `%s`", strVal)
|
||||
}
|
||||
return val, nil
|
||||
}
|
||||
|
||||
func getIntParamOr(r *http.Request, key string, or int) int {
|
||||
val, err := getIntParam(r, key)
|
||||
if err != nil {
|
||||
return or
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func respondRaw(w http.ResponseWriter, r *http.Request, code int, sub *subsonic.Response) {
|
||||
format := r.URL.Query().Get("f")
|
||||
switch format {
|
||||
res := subsonic.MetaResponse{
|
||||
Response: sub,
|
||||
}
|
||||
switch r.URL.Query().Get("f") {
|
||||
case "json":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
data, err := json.Marshal(sub)
|
||||
data, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
log.Printf("could not marshall to json: %v\n", err)
|
||||
}
|
||||
w.Write([]byte(`{"subsonic-response":`))
|
||||
w.Write(data)
|
||||
w.Write([]byte("}"))
|
||||
fmt.Println("THE JSON", string(data))
|
||||
case "jsonp":
|
||||
w.Header().Set("Content-Type", "application/javascript")
|
||||
data, err := json.Marshal(sub)
|
||||
data, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
log.Printf("could not marshall to json: %v\n", err)
|
||||
}
|
||||
callback := r.URL.Query().Get("callback")
|
||||
w.Write([]byte(fmt.Sprintf(`%s({"subsonic-response":`, callback)))
|
||||
w.Write([]byte(callback))
|
||||
w.Write([]byte("("))
|
||||
w.Write(data)
|
||||
w.Write([]byte("});"))
|
||||
fmt.Println("THE JSONP", string(data))
|
||||
w.Write([]byte(");"))
|
||||
default:
|
||||
w.Header().Set("Content-Type", "application/xml")
|
||||
data, err := xml.Marshal(sub)
|
||||
data, err := xml.Marshal(res)
|
||||
if err != nil {
|
||||
log.Printf("could not marshall to xml: %v\n", err)
|
||||
}
|
||||
w.Write(data)
|
||||
fmt.Println("THE XML", string(data))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user