diff --git a/cmd/scanner/main.go b/cmd/scanner/main.go index a2b9c27..bc06132 100644 --- a/cmd/scanner/main.go +++ b/cmd/scanner/main.go @@ -75,7 +75,7 @@ func readTags(fullPath string) (tag.Metadata, 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() { return nil } @@ -101,6 +101,7 @@ func handleFolderCompletion(fullPath string, info *godirwalk.Dirent) error { } func handleFile(fullPath string, info *godirwalk.Dirent) error { + fmt.Println("+++++", fullPath) if info.IsDir() { return nil } diff --git a/cmd/server/main.go b/cmd/server/main.go index 8034f94..cc74b90 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -15,10 +15,6 @@ import ( "github.com/sentriz/gonic/handler" ) -var ( - dbCon = db.New() -) - type middleware func(next http.HandlerFunc) http.HandlerFunc func newChain(wares ...middleware) middleware { @@ -50,10 +46,7 @@ func extendFromBox(tmpl *template.Template, box *packr.Box, key string) *templat return newT } -func setSubsonicRoutes(mux *http.ServeMux) { - cont := handler.Controller{ - DB: dbCon, - } +func setSubsonicRoutes(cont handler.Controller, mux *http.ServeMux) { withWare := newChain( cont.WithLogging, cont.WithCORS, @@ -83,17 +76,14 @@ func setSubsonicRoutes(mux *http.ServeMux) { mux.HandleFunc("/rest/getLicense.view", withWare(cont.GetLicence)) } -func setAdminRoutes(mux *http.ServeMux) { - cont := handler.Controller{ - DB: dbCon, - } +func setAdminRoutes(cont handler.Controller, mux *http.ServeMux) { sessionKey := []byte(cont.GetSetting("session_key")) if len(sessionKey) == 0 { sessionKey = securecookie.GenerateRandomKey(32) cont.SetSetting("session_key", string(sessionKey)) } // 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) // using packr to bundle templates and static files box := packr.New("templates", "../../templates") @@ -143,8 +133,11 @@ func setAdminRoutes(mux *http.ServeMux) { func main() { address := ":6969" mux := http.NewServeMux() - setSubsonicRoutes(mux) - setAdminRoutes(mux) + // create a new controller and pass a copy to both routes. + // 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{ Addr: address, Handler: mux, diff --git a/db/db.go b/db/db.go index a4a6a7f..2d1d09f 100644 --- a/db/db.go +++ b/db/db.go @@ -2,10 +2,17 @@ package db import ( "log" + "path/filepath" + "runtime" "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 func New() *gorm.DB { db, err := gorm.Open("sqlite3", "gonic.db") @@ -14,3 +21,14 @@ func New() *gorm.DB { } return db } + +// New creates a new GORM connection to the mock database +func NewMock() *gorm.DB { + // projectRoot presumes this file is `/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 +} diff --git a/handler/handler_sub_test.go b/handler/handler_sub_test.go new file mode 100644 index 0000000..512eb6a --- /dev/null +++ b/handler/handler_sub_test.go @@ -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) +} diff --git a/handler/respond_sub.go b/handler/respond_sub.go index b901a9d..25332fa 100644 --- a/handler/respond_sub.go +++ b/handler/respond_sub.go @@ -35,7 +35,7 @@ func respondRaw(w http.ResponseWriter, r *http.Request, w.Write([]byte(");")) default: w.Header().Set("Content-Type", "application/xml") - data, err := xml.Marshal(res) + data, err := xml.MarshalIndent(res, "", " ") if err != nil { log.Printf("could not marshall to xml: %v\n", err) } diff --git a/test_data/mock_getArtists_response b/test_data/mock_getArtists_response new file mode 100644 index 0000000..e6177dd --- /dev/null +++ b/test_data/mock_getArtists_response @@ -0,0 +1,14 @@ + + + + + + + + + + + + + +