Move App to server package

This commit is contained in:
Deluan 2020-01-13 17:06:47 -05:00
parent a5b2e3c31f
commit 04ef2edeca
4 changed files with 17 additions and 15 deletions

View file

@ -1,4 +1,4 @@
package main package server
import ( import (
"net/http" "net/http"
@ -14,32 +14,32 @@ import (
"github.com/go-chi/cors" "github.com/go-chi/cors"
) )
type App struct { type Server struct {
Importer *scanner.Importer Importer *scanner.Importer
router *chi.Mux router *chi.Mux
} }
func NewApp(importer *scanner.Importer) *App { func New(importer *scanner.Importer) *Server {
a := &App{Importer: importer} a := &Server{Importer: importer}
initMimeTypes() initMimeTypes()
a.initRoutes() a.initRoutes()
a.initImporter() a.initImporter()
return a return a
} }
func (a *App) MountRouter(path string, subRouter http.Handler) { func (a *Server) MountRouter(path string, subRouter http.Handler) {
a.router.Group(func(r chi.Router) { a.router.Group(func(r chi.Router) {
r.Use(middleware.Logger) r.Use(middleware.Logger)
r.Mount(path, subRouter) r.Mount(path, subRouter)
}) })
} }
func (a *App) Run(addr string) { func (a *Server) Run(addr string) {
log.Info("Started CloudSonic server", "address", addr) log.Info("Started CloudSonic server", "address", addr)
log.Error(http.ListenAndServe(addr, a.router)) log.Error(http.ListenAndServe(addr, a.router))
} }
func (a *App) initRoutes() { func (a *Server) initRoutes() {
r := chi.NewRouter() r := chi.NewRouter()
r.Use(cors.Default().Handler) r.Use(cors.Default().Handler)
@ -60,11 +60,11 @@ func (a *App) initRoutes() {
a.router = r a.router = r
} }
func (a *App) initImporter() { func (a *Server) initImporter() {
go a.startPeriodicScans() go a.startPeriodicScans()
} }
func (a *App) startPeriodicScans() { func (a *Server) startPeriodicScans() {
for { for {
select { select {
case <-time.After(5 * time.Second): case <-time.After(5 * time.Second):

View file

@ -1,4 +1,4 @@
package main package server
import "mime" import "mime"

View file

@ -13,12 +13,13 @@ import (
"github.com/cloudsonic/sonic-server/persistence" "github.com/cloudsonic/sonic-server/persistence"
"github.com/cloudsonic/sonic-server/persistence/db_sql" "github.com/cloudsonic/sonic-server/persistence/db_sql"
"github.com/cloudsonic/sonic-server/scanner" "github.com/cloudsonic/sonic-server/scanner"
"github.com/cloudsonic/sonic-server/server"
"github.com/google/wire" "github.com/google/wire"
) )
// Injectors from wire_injectors.go: // Injectors from wire_injectors.go:
func CreateApp(musicFolder string) *App { func CreateApp(musicFolder string) *server.Server {
provider := createPersistenceProvider() provider := createPersistenceProvider()
checkSumRepository := provider.CheckSumRepository checkSumRepository := provider.CheckSumRepository
itunesScanner := scanner.NewItunesScanner(checkSumRepository) itunesScanner := scanner.NewItunesScanner(checkSumRepository)
@ -29,8 +30,8 @@ func CreateApp(musicFolder string) *App {
playlistRepository := provider.PlaylistRepository playlistRepository := provider.PlaylistRepository
propertyRepository := provider.PropertyRepository propertyRepository := provider.PropertyRepository
importer := scanner.NewImporter(musicFolder, itunesScanner, mediaFileRepository, albumRepository, artistRepository, artistIndexRepository, playlistRepository, propertyRepository) importer := scanner.NewImporter(musicFolder, itunesScanner, mediaFileRepository, albumRepository, artistRepository, artistIndexRepository, playlistRepository, propertyRepository)
app := NewApp(importer) serverServer := server.New(importer)
return app return serverServer
} }
func CreateSubsonicAPIRouter() *api.Router { func CreateSubsonicAPIRouter() *api.Router {

View file

@ -9,6 +9,7 @@ import (
"github.com/cloudsonic/sonic-server/itunesbridge" "github.com/cloudsonic/sonic-server/itunesbridge"
"github.com/cloudsonic/sonic-server/persistence/db_sql" "github.com/cloudsonic/sonic-server/persistence/db_sql"
"github.com/cloudsonic/sonic-server/scanner" "github.com/cloudsonic/sonic-server/scanner"
"github.com/cloudsonic/sonic-server/server"
"github.com/google/wire" "github.com/google/wire"
) )
@ -35,9 +36,9 @@ var allProviders = wire.NewSet(
createPersistenceProvider, createPersistenceProvider,
) )
func CreateApp(musicFolder string) *App { func CreateApp(musicFolder string) *server.Server {
panic(wire.Build( panic(wire.Build(
NewApp, server.New,
allProviders, allProviders,
)) ))
} }