mux t
This commit is contained in:
@@ -75,7 +75,7 @@ func readTags(fullPath string) (tag.Metadata, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleFolderCompletion(fullPath string, info *godirwalk.Dirent) error {
|
func handleFolderCompletion(fullPath string, info *godirwalk.Dirent) error {
|
||||||
log.Printf("processed folder `%s`\n", fullPath)
|
log.Printf("++++++ processed folder `%s`\n", fullPath)
|
||||||
if cLastAlbum.isEmpty() {
|
if cLastAlbum.isEmpty() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -101,6 +101,7 @@ func handleFolderCompletion(fullPath string, info *godirwalk.Dirent) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleFile(fullPath string, info *godirwalk.Dirent) error {
|
func handleFile(fullPath string, info *godirwalk.Dirent) error {
|
||||||
|
fmt.Println("+++++", fullPath)
|
||||||
if info.IsDir() {
|
if info.IsDir() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,10 +15,6 @@ import (
|
|||||||
"github.com/sentriz/gonic/handler"
|
"github.com/sentriz/gonic/handler"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
dbCon = db.New()
|
|
||||||
)
|
|
||||||
|
|
||||||
type middleware func(next http.HandlerFunc) http.HandlerFunc
|
type middleware func(next http.HandlerFunc) http.HandlerFunc
|
||||||
|
|
||||||
func newChain(wares ...middleware) middleware {
|
func newChain(wares ...middleware) middleware {
|
||||||
@@ -50,10 +46,7 @@ func extendFromBox(tmpl *template.Template, box *packr.Box, key string) *templat
|
|||||||
return newT
|
return newT
|
||||||
}
|
}
|
||||||
|
|
||||||
func setSubsonicRoutes(mux *http.ServeMux) {
|
func setSubsonicRoutes(cont handler.Controller, mux *http.ServeMux) {
|
||||||
cont := handler.Controller{
|
|
||||||
DB: dbCon,
|
|
||||||
}
|
|
||||||
withWare := newChain(
|
withWare := newChain(
|
||||||
cont.WithLogging,
|
cont.WithLogging,
|
||||||
cont.WithCORS,
|
cont.WithCORS,
|
||||||
@@ -83,17 +76,14 @@ func setSubsonicRoutes(mux *http.ServeMux) {
|
|||||||
mux.HandleFunc("/rest/getLicense.view", withWare(cont.GetLicence))
|
mux.HandleFunc("/rest/getLicense.view", withWare(cont.GetLicence))
|
||||||
}
|
}
|
||||||
|
|
||||||
func setAdminRoutes(mux *http.ServeMux) {
|
func setAdminRoutes(cont handler.Controller, mux *http.ServeMux) {
|
||||||
cont := handler.Controller{
|
|
||||||
DB: dbCon,
|
|
||||||
}
|
|
||||||
sessionKey := []byte(cont.GetSetting("session_key"))
|
sessionKey := []byte(cont.GetSetting("session_key"))
|
||||||
if len(sessionKey) == 0 {
|
if len(sessionKey) == 0 {
|
||||||
sessionKey = securecookie.GenerateRandomKey(32)
|
sessionKey = securecookie.GenerateRandomKey(32)
|
||||||
cont.SetSetting("session_key", string(sessionKey))
|
cont.SetSetting("session_key", string(sessionKey))
|
||||||
}
|
}
|
||||||
// create gormstore (and cleanup) for backend sessions
|
// create gormstore (and cleanup) for backend sessions
|
||||||
cont.SStore = gormstore.New(dbCon, []byte(sessionKey))
|
cont.SStore = gormstore.New(cont.DB, []byte(sessionKey))
|
||||||
go cont.SStore.PeriodicCleanup(1*time.Hour, nil)
|
go cont.SStore.PeriodicCleanup(1*time.Hour, nil)
|
||||||
// using packr to bundle templates and static files
|
// using packr to bundle templates and static files
|
||||||
box := packr.New("templates", "../../templates")
|
box := packr.New("templates", "../../templates")
|
||||||
@@ -143,8 +133,11 @@ func setAdminRoutes(mux *http.ServeMux) {
|
|||||||
func main() {
|
func main() {
|
||||||
address := ":6969"
|
address := ":6969"
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
setSubsonicRoutes(mux)
|
// create a new controller and pass a copy to both routes.
|
||||||
setAdminRoutes(mux)
|
// they will add more fields to their copy if they need them
|
||||||
|
baseController := handler.Controller{DB: db.New()}
|
||||||
|
setSubsonicRoutes(baseController, mux)
|
||||||
|
setAdminRoutes(baseController, mux)
|
||||||
server := &http.Server{
|
server := &http.Server{
|
||||||
Addr: address,
|
Addr: address,
|
||||||
Handler: mux,
|
Handler: mux,
|
||||||
|
|||||||
18
db/db.go
18
db/db.go
@@ -2,10 +2,17 @@ package db
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// cFile is the path to this go file
|
||||||
|
_, cFile, _, _ = runtime.Caller(0)
|
||||||
|
)
|
||||||
|
|
||||||
// New creates a new GORM connection to the database
|
// New creates a new GORM connection to the database
|
||||||
func New() *gorm.DB {
|
func New() *gorm.DB {
|
||||||
db, err := gorm.Open("sqlite3", "gonic.db")
|
db, err := gorm.Open("sqlite3", "gonic.db")
|
||||||
@@ -14,3 +21,14 @@ func New() *gorm.DB {
|
|||||||
}
|
}
|
||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// New creates a new GORM connection to the mock database
|
||||||
|
func NewMock() *gorm.DB {
|
||||||
|
// projectRoot presumes this file is `<root>/db/db.go`
|
||||||
|
dbPath, _ := filepath.Abs(filepath.Join(cFile, "../../test_data/mock.db"))
|
||||||
|
db, err := gorm.Open("sqlite3", dbPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("when opening mock database: %v\n", err)
|
||||||
|
}
|
||||||
|
return db
|
||||||
|
}
|
||||||
|
|||||||
27
handler/handler_sub_test.go
Normal file
27
handler/handler_sub_test.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
||||||
|
|
||||||
|
"github.com/sentriz/gonic/db"
|
||||||
|
)
|
||||||
|
|
||||||
|
var mockController = Controller{
|
||||||
|
DB: db.NewMock(),
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetArtists(t *testing.T) {
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
r, _ := http.NewRequest("GET", "", nil)
|
||||||
|
handler := http.HandlerFunc(mockController.GetArtists)
|
||||||
|
handler.ServeHTTP(rr, r)
|
||||||
|
dat, _ := ioutil.ReadFile("../test_data/mock_getArtists_response")
|
||||||
|
fmt.Println(string(dat))
|
||||||
|
fmt.Println(rr.Body)
|
||||||
|
}
|
||||||
@@ -35,7 +35,7 @@ func respondRaw(w http.ResponseWriter, r *http.Request,
|
|||||||
w.Write([]byte(");"))
|
w.Write([]byte(");"))
|
||||||
default:
|
default:
|
||||||
w.Header().Set("Content-Type", "application/xml")
|
w.Header().Set("Content-Type", "application/xml")
|
||||||
data, err := xml.Marshal(res)
|
data, err := xml.MarshalIndent(res, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("could not marshall to xml: %v\n", err)
|
log.Printf("could not marshall to xml: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|||||||
14
test_data/mock_getArtists_response
Normal file
14
test_data/mock_getArtists_response
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.9.0">
|
||||||
|
<albumList2 />
|
||||||
|
<artists>
|
||||||
|
<index name="A">
|
||||||
|
<artist id="1" name="A Certain Ratio" coverArt="0" albumCount="0" />
|
||||||
|
<artist id="3" name="Anikas" coverArt="0" albumCount="0" />
|
||||||
|
</index>
|
||||||
|
<index name="#">
|
||||||
|
<artist id="2" name="13th Floor Elevators" coverArt="0" albumCount="0" />
|
||||||
|
</index>
|
||||||
|
</artists>
|
||||||
|
<musicFolders />
|
||||||
|
</subsonic-response>
|
||||||
Reference in New Issue
Block a user