add some endpoints
This commit is contained in:
@@ -1,78 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/sentriz/gonic/context"
|
||||
"github.com/sentriz/gonic/db"
|
||||
"github.com/sentriz/gonic/handler"
|
||||
"github.com/sentriz/gonic/router"
|
||||
"github.com/sentriz/gonic/subsonic"
|
||||
|
||||
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
||||
"github.com/labstack/echo"
|
||||
)
|
||||
|
||||
var (
|
||||
username = "senan"
|
||||
password = "howdy"
|
||||
requiredParameters = []string{
|
||||
"u", "t", "s", "v", "c",
|
||||
}
|
||||
)
|
||||
type middleware func(next http.HandlerFunc) http.HandlerFunc
|
||||
|
||||
func checkCredentials(token, salt string) bool {
|
||||
toHash := fmt.Sprintf("%s%s", password, salt)
|
||||
hash := md5.Sum([]byte(toHash))
|
||||
expToken := hex.EncodeToString(hash[:])
|
||||
return token == expToken
|
||||
}
|
||||
|
||||
func contextMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
return next(&context.Subsonic{c})
|
||||
}
|
||||
}
|
||||
|
||||
func validationMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
cc := c.(*context.Subsonic)
|
||||
for _, req := range requiredParameters {
|
||||
param := cc.QueryParams().Get(req)
|
||||
if param != "" {
|
||||
continue
|
||||
func newChain(wares ...middleware) middleware {
|
||||
return func(final http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
last := final
|
||||
for i := len(wares) - 1; i >= 0; i-- {
|
||||
last = wares[i](last)
|
||||
}
|
||||
return cc.Respond(http.StatusBadRequest, subsonic.NewError(
|
||||
10, fmt.Sprintf("please provide a `%s` parameter", req),
|
||||
))
|
||||
last(w, r)
|
||||
}
|
||||
credsOk := checkCredentials(
|
||||
cc.QueryParams().Get("t"), // token
|
||||
cc.QueryParams().Get("s"), // salt
|
||||
)
|
||||
if !credsOk {
|
||||
return cc.Respond(http.StatusBadRequest, subsonic.NewError(
|
||||
40, "invalid username or password",
|
||||
))
|
||||
}
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
d := db.New()
|
||||
r := router.New()
|
||||
r.Use(contextMiddleware)
|
||||
r.Use(validationMiddleware)
|
||||
h := &handler.Handler{
|
||||
DB: d,
|
||||
Router: r,
|
||||
address := ":5000"
|
||||
cont := handler.Controller{
|
||||
DB: db.New(),
|
||||
}
|
||||
withWare := newChain(
|
||||
cont.LogConnection,
|
||||
cont.EnableCORS,
|
||||
cont.CheckParameters,
|
||||
)
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/rest/ping.view", withWare(cont.Ping))
|
||||
mux.HandleFunc("/rest/getIndexes.view", withWare(cont.GetIndexes))
|
||||
mux.HandleFunc("/rest/getMusicDirectory.view", withWare(cont.GetMusicDirectory))
|
||||
mux.HandleFunc("/rest/getCoverArt.view", withWare(cont.GetCoverArt))
|
||||
mux.HandleFunc("/rest/getMusicFolders.view", withWare(cont.GetMusicFolders))
|
||||
mux.HandleFunc("/rest/getPlaylists.view", withWare(cont.GetPlaylists))
|
||||
mux.HandleFunc("/rest/getGenres.view", withWare(cont.GetGenres))
|
||||
mux.HandleFunc("/rest/getPodcasts.view", withWare(cont.GetPodcasts))
|
||||
server := &http.Server{
|
||||
Addr: address,
|
||||
Handler: mux,
|
||||
ReadTimeout: 5 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
IdleTimeout: 15 * time.Second,
|
||||
}
|
||||
log.Printf("starting server at `%s`\n", address)
|
||||
err := server.ListenAndServe()
|
||||
if err != nil {
|
||||
log.Printf("when starting server: %v\n", err)
|
||||
}
|
||||
rest := r.Group("/rest")
|
||||
rest.GET("", h.GetTest)
|
||||
log.Fatal(r.Start("127.0.0.1:5001"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user