add some endpoints
This commit is contained in:
@@ -11,7 +11,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/sentriz/gonic/db"
|
||||
"github.com/sentriz/gonic/model"
|
||||
|
||||
"github.com/dhowden/tag"
|
||||
"github.com/jinzhu/gorm"
|
||||
@@ -84,7 +83,7 @@ func handleFolderCompletion(fullPath string, info *godirwalk.Dirent) error {
|
||||
if cLastAlbum.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
cover := model.Cover{
|
||||
cover := db.Cover{
|
||||
Path: cLastAlbum.coverPath,
|
||||
}
|
||||
err := tx.Where(cover).First(&cover).Error
|
||||
@@ -122,7 +121,7 @@ func handleFile(fullPath string, info *godirwalk.Dirent) error {
|
||||
return nil
|
||||
}
|
||||
// set track basics
|
||||
track := model.Track{
|
||||
track := db.Track{
|
||||
Path: fullPath,
|
||||
}
|
||||
err = tx.Where(track).First(&track).Error
|
||||
@@ -131,6 +130,8 @@ func handleFile(fullPath string, info *godirwalk.Dirent) error {
|
||||
return nil
|
||||
}
|
||||
tags, err := readTags(fullPath)
|
||||
fmt.Println(tags.Raw())
|
||||
os.Exit(0)
|
||||
if err != nil {
|
||||
return fmt.Errorf("when reading tags: %v", err)
|
||||
}
|
||||
@@ -138,13 +139,13 @@ func handleFile(fullPath string, info *godirwalk.Dirent) error {
|
||||
discNumber, TotalDiscs := tags.Disc()
|
||||
track.Path = fullPath
|
||||
track.Title = tags.Title()
|
||||
track.DiscNumber = discNumber
|
||||
track.TotalDiscs = TotalDiscs
|
||||
track.TotalTracks = totalTracks
|
||||
track.TrackNumber = trackNumber
|
||||
track.Year = tags.Year()
|
||||
// set artist
|
||||
artist := model.Artist{
|
||||
track.DiscNumber = uint(discNumber)
|
||||
track.TotalDiscs = uint(TotalDiscs)
|
||||
track.TotalTracks = uint(totalTracks)
|
||||
track.TrackNumber = uint(trackNumber)
|
||||
track.Year = uint(tags.Year())
|
||||
// set artist {
|
||||
artist := db.Artist{
|
||||
Name: tags.AlbumArtist(),
|
||||
}
|
||||
err = tx.Where(artist).First(&artist).Error
|
||||
@@ -154,7 +155,7 @@ func handleFile(fullPath string, info *godirwalk.Dirent) error {
|
||||
}
|
||||
track.ArtistID = artist.ID
|
||||
// set album
|
||||
album := model.Album{
|
||||
album := db.Album{
|
||||
ArtistID: artist.ID,
|
||||
Title: tags.Album(),
|
||||
}
|
||||
@@ -180,10 +181,11 @@ func main() {
|
||||
orm = db.New()
|
||||
orm.SetLogger(log.New(os.Stdout, "gorm ", 0))
|
||||
orm.AutoMigrate(
|
||||
&model.Album{},
|
||||
&model.Artist{},
|
||||
&model.Track{},
|
||||
&model.Cover{},
|
||||
&db.Album{},
|
||||
&db.Artist{},
|
||||
&db.Track{},
|
||||
&db.Cover{},
|
||||
&db.User{},
|
||||
)
|
||||
// 🤫🤫🤫
|
||||
orm.Exec(`
|
||||
@@ -191,6 +193,11 @@ func main() {
|
||||
SELECT 'albums', 500000
|
||||
WHERE NOT EXISTS (SELECT * FROM sqlite_sequence)
|
||||
`)
|
||||
orm.Exec(`
|
||||
INSERT INTO users(username, password)
|
||||
SELECT 'admin', 'admin'
|
||||
WHERE NOT EXISTS (SELECT * FROM users)
|
||||
`)
|
||||
startTime := time.Now()
|
||||
tx = orm.Begin()
|
||||
err := godirwalk.Walk(os.Args[1], &godirwalk.Options{
|
||||
|
||||
@@ -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