Skip to content

Commit

Permalink
Add BaseURL configuration (fixes #103)
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Apr 3, 2020
1 parent b8eb22d commit 75cd21d
Show file tree
Hide file tree
Showing 17 changed files with 61 additions and 25 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ services:
ND_PORT: 4533
ND_TRANSCODINGCACHESIZE: 100MB
ND_SESSIONTIMEOUT: 30m
ND_BASEURL: ""
volumes:
- "./data:/data"
- "/path/to/your/music/folder:/music:ro"
Expand Down
1 change: 1 addition & 0 deletions conf/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type nd struct {
DbPath string ``
LogLevel string `default:"info"`
SessionTimeout string `default:"30m"`
BaseURL string `default:""`

IgnoredArticles string `default:"The El La Los Las Le Les Os As O A"`
IndexGroups string `default:"A B C D E F G H I J K L M N O P Q R S T U V W X-Z(XYZ) [Unknown]([)"`
Expand Down
3 changes: 3 additions & 0 deletions consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const (

DevInitialUserName = "admin"
DevInitialName = "Dev Admin"

URLPathUI = "/app"
URLPathSubsonicAPI = "/rest"
)

var (
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ services:
ND_PORT: 4533
ND_TRANSCODINGCACHESIZE: 100MB
ND_SESSIONTIMEOUT: 30m
ND_BASEURL: ""
volumes:
- "./data:/data"
- "./music:/music"
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func main() {
panic(fmt.Sprintf("Could not create the Subsonic API router. Aborting! err=%v", err))
}
a := CreateServer(conf.Server.MusicFolder)
a.MountRouter("/rest", subsonic)
a.MountRouter("/app", CreateAppRouter("/app"))
a.MountRouter(consts.URLPathSubsonicAPI, subsonic)
a.MountRouter(consts.URLPathUI, CreateAppRouter())
a.Run(":" + conf.Server.Port)
}
19 changes: 10 additions & 9 deletions server/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@ import (
)

type Router struct {
ds model.DataStore
mux http.Handler
path string
ds model.DataStore
mux http.Handler
}

func New(ds model.DataStore, path string) *Router {
r := &Router{ds: ds, path: path}
r.mux = r.routes()
return r
func New(ds model.DataStore) *Router {
return &Router{ds: ds}
}

func (app *Router) Setup(path string) {
app.mux = app.routes(path)
}

func (app *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
app.mux.ServeHTTP(w, r)
}

func (app *Router) routes() http.Handler {
func (app *Router) routes(path string) http.Handler {
r := chi.NewRouter()

r.Post("/login", Login(app.ds))
Expand All @@ -52,7 +53,7 @@ func (app *Router) routes() http.Handler {

// Serve UI app assets
r.Handle("/", ServeIndex(app.ds))
r.Handle("/*", http.StripPrefix(app.path, http.FileServer(assets.AssetFile())))
r.Handle("/*", http.StripPrefix(path, http.FileServer(assets.AssetFile())))

return r
}
Expand Down
3 changes: 3 additions & 0 deletions server/app/serve_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"html/template"
"io/ioutil"
"net/http"
"strings"

"github.com/deluan/navidrome/assets"
"github.com/deluan/navidrome/conf"
"github.com/deluan/navidrome/consts"
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model"
Expand All @@ -31,6 +33,7 @@ func ServeIndex(ds model.DataStore) http.HandlerFunc {
t, _ = t.Parse(string(indexStr))
appConfig := map[string]interface{}{
"firstTime": firstTime,
"baseURL": strings.TrimSuffix(conf.Server.BaseURL, "/"),
}
j, _ := json.Marshal(appConfig)
data := map[string]interface{}{
Expand Down
18 changes: 14 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package server
import (
"net/http"
"os"
"path"
"path/filepath"
"time"

"github.com/deluan/navidrome/conf"
"github.com/deluan/navidrome/consts"
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/scanner"
Expand All @@ -15,6 +17,11 @@ import (
"github.com/go-chi/cors"
)

type Handler interface {
http.Handler
Setup(path string)
}

type Server struct {
Scanner *scanner.Scanner
router *chi.Mux
Expand All @@ -29,11 +36,13 @@ func New(scanner *scanner.Scanner, ds model.DataStore) *Server {
return a
}

func (a *Server) MountRouter(path string, subRouter http.Handler) {
log.Info("Mounting routes", "path", path)
func (a *Server) MountRouter(urlPath string, subRouter Handler) {
urlPath = path.Join(conf.Server.BaseURL, urlPath)
log.Info("Mounting routes", "path", urlPath)
subRouter.Setup(urlPath)
a.router.Group(func(r chi.Router) {
r.Use(RequestLogger)
r.Mount(path, subRouter)
r.Mount(urlPath, subRouter)
})
}

Expand All @@ -53,8 +62,9 @@ func (a *Server) initRoutes() {
r.Use(middleware.Heartbeat("/ping"))
r.Use(InjectLogger)

indexHtml := path.Join(conf.Server.BaseURL, consts.URLPathUI, "index.html")
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/app", 302)
http.Redirect(w, r, indexHtml, 302)
})

workDir, _ := os.Getwd()
Expand Down
2 changes: 2 additions & 0 deletions server/subsonic/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func New(browser engine.Browser, cover engine.Cover, listGenerator engine.ListGe
return r
}

func (api *Router) Setup(path string) {}

func (api *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
api.mux.ServeHTTP(w, r)
}
Expand Down
2 changes: 1 addition & 1 deletion ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"homepage": "https://localhost/app/",
"homepage": ".",
"proxy": "http://localhost:4633/",
"eslintConfig": {
"extends": "react-app"
Expand Down
1 change: 1 addition & 0 deletions ui/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const App = () => {
if (appConfig.firstTime) {
localStorage.setItem('initialAccountCreation', 'true')
}
localStorage.setItem('baseURL', appConfig.baseURL)
} catch (e) {}

return (
Expand Down
5 changes: 3 additions & 2 deletions ui/src/authProvider.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import jwtDecode from 'jwt-decode'
import md5 from 'md5-hex'
import baseUrl from './utils/baseUrl'

const authProvider = {
login: ({ username, password }) => {
let url = '/app/login'
let url = baseUrl('/app/login')
if (localStorage.getItem('initialAccountCreation')) {
url = '/app/createAdmin'
url = baseUrl('/app/createAdmin')
}
const request = new Request(url, {
method: 'POST',
Expand Down
8 changes: 5 additions & 3 deletions ui/src/dataProvider.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { fetchUtils } from 'react-admin'
import jsonServerProvider from 'ra-data-json-server'
import baseUrl from './utils/baseUrl'

const baseUrl = '/app/api'
const restUrl = '/app/api'

const httpClient = (url, options = {}) => {
url = url.replace(baseUrl + '/albumSong', baseUrl + '/song')
url = baseUrl(url)
url = url.replace(restUrl + '/albumSong', restUrl + '/song')
if (!options.headers) {
options.headers = new Headers({ Accept: 'application/json' })
}
Expand All @@ -22,6 +24,6 @@ const httpClient = (url, options = {}) => {
})
}

const dataProvider = jsonServerProvider(baseUrl, httpClient)
const dataProvider = jsonServerProvider(restUrl, httpClient)

export default dataProvider
4 changes: 3 additions & 1 deletion ui/src/subsonic/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { fetchUtils } from 'react-admin'
import baseUrl from "../utils/baseUrl"

const url = (command, id, options) => {
const params = new URLSearchParams()
Expand All @@ -18,7 +19,8 @@ const url = (command, id, options) => {
params.append(k, options[k])
})
}
return `rest/${command}?${params.toString()}`
const url = `/rest/${command}?${params.toString()}`
return baseUrl(url)
}

const scrobble = (id, submit) => {
Expand Down
8 changes: 8 additions & 0 deletions ui/src/utils/baseUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const baseUrl = (path) => {
const base = localStorage.getItem('baseURL') || ''
const parts = [base]
parts.push(path.replace(/^\//, ''))
return parts.join('/')
}

export default baseUrl
4 changes: 2 additions & 2 deletions wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wire_injectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func CreateServer(musicFolder string) *server.Server {
))
}

func CreateAppRouter(path string) *app.Router {
func CreateAppRouter() *app.Router {
panic(wire.Build(allProviders))
}

Expand Down

0 comments on commit 75cd21d

Please sign in to comment.