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

View file

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

View file

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

View file

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