From 8dabd2cfd035653af7ed166ffdbc28b61952de27 Mon Sep 17 00:00:00 2001 From: Simon Eisenmann Date: Fri, 28 Feb 2014 15:38:38 +0100 Subject: [PATCH] Added support for url prefixes when behind a web server sub path (see server.conf.in basePath configuration). --- html/head.html | 2 +- server.conf.in | 1 + src/app/spreed-speakfreely-server/config.go | 5 +++-- src/app/spreed-speakfreely-server/main.go | 22 ++++++++++++++++----- static/js/services/mediastream.js | 2 +- 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/html/head.html b/html/head.html index d8e18268..870a8a33 100644 --- a/html/head.html +++ b/html/head.html @@ -3,7 +3,7 @@ - + diff --git a/server.conf.in b/server.conf.in index 93645779..a64fcc43 100644 --- a/server.conf.in +++ b/server.conf.in @@ -5,6 +5,7 @@ listen = 127.0.0.1:8080 #root = /usr/share/spreed-speakfreely-server/www #readtimeout = 10 #writetimeout = 10 +#basePath = /some/sub/path/ # Set this when running behind a web server under a sub path. [app] #title = Spreed Speak Freely diff --git a/src/app/spreed-speakfreely-server/config.go b/src/app/spreed-speakfreely-server/config.go index 470d1931..5d358d14 100644 --- a/src/app/spreed-speakfreely-server/config.go +++ b/src/app/spreed-speakfreely-server/config.go @@ -28,6 +28,7 @@ type Config struct { Title string // Title ver string // Version (not exported to Javascript) S string // Static URL prefix with version + B string // Base URL StunURIs []string // STUN server URIs TurnURIs []string // TURN server URIs Tokens bool // True when we got a tokens file @@ -36,7 +37,7 @@ type Config struct { Plugin string // Plugin to load } -func NewConfig(title, ver, runtimeVersion string, stunURIs, turnURIs []string, tokens bool, globalRoomid, plugin string) *Config { +func NewConfig(title, ver, runtimeVersion, basePath string, stunURIs, turnURIs []string, tokens bool, globalRoomid, plugin string) *Config { sv := fmt.Sprintf("static/ver=%s", ver) - return &Config{Title: title, ver: ver, S: sv, StunURIs: stunURIs, TurnURIs: turnURIs, Tokens: tokens, Version: runtimeVersion, globalRoomid: globalRoomid, Plugin: plugin} + return &Config{Title: title, ver: ver, S: sv, B: basePath, StunURIs: stunURIs, TurnURIs: turnURIs, Tokens: tokens, Version: runtimeVersion, globalRoomid: globalRoomid, Plugin: plugin} } diff --git a/src/app/spreed-speakfreely-server/main.go b/src/app/spreed-speakfreely-server/main.go index 470b44c5..15aa32f1 100644 --- a/src/app/spreed-speakfreely-server/main.go +++ b/src/app/spreed-speakfreely-server/main.go @@ -143,6 +143,17 @@ func runner(runtime phoenix.Runtime) error { return fmt.Errorf("Unable to find client. Path correct and compiled css?") } + // Read base path from config and make sure it ends with a slash. + basePath, err := runtime.GetString("http", "basePath") + if err != nil { + basePath = "/" + } else { + if !strings.HasSuffix(basePath, "/") { + basePath = fmt.Sprintf("%s/", basePath) + } + log.Printf("Using '%s' base base path.", basePath) + } + sessionSecret, err := runtime.GetString("app", "sessionSecret") if err != nil { return fmt.Errorf("No sessionSecret in config file.") @@ -218,7 +229,7 @@ func runner(runtime phoenix.Runtime) error { } // Create configuration data structure. - config = NewConfig(title, ver, runtimeVersion, stunURIs, turnURIs, tokenProvider != nil, globalRoomid, plugin) + config = NewConfig(title, ver, runtimeVersion, basePath, stunURIs, turnURIs, tokenProvider != nil, globalRoomid, plugin) // Load templates. tt := template.New("") @@ -262,11 +273,12 @@ func runner(runtime phoenix.Runtime) error { } // Create router. - r := mux.NewRouter() + router := mux.NewRouter() + r := router.PathPrefix(basePath).Subrouter().StrictSlash(true) r.HandleFunc("/", httputils.MakeGzipHandler(mainHandler)) - r.Handle("/static/{path:.*}", httputils.FileStaticServer(http.Dir(rootFolder))) - r.Handle("/robots.txt", http.FileServer(http.Dir(path.Join(rootFolder, "static")))) - r.Handle("/favicon.ico", http.FileServer(http.Dir(path.Join(rootFolder, "static", "img")))) + r.Handle("/static/{path:.*}", http.StripPrefix(basePath, httputils.FileStaticServer(http.Dir(rootFolder)))) + r.Handle("/robots.txt", http.StripPrefix(basePath, http.FileServer(http.Dir(path.Join(rootFolder, "static"))))) + r.Handle("/favicon.ico", http.StripPrefix(basePath, http.FileServer(http.Dir(path.Join(rootFolder, "static", "img"))))) r.Handle("/ws", makeWsHubHandler(hub)) r.HandleFunc("/{room}", httputils.MakeGzipHandler(roomHandler)) makeApiHandler(r, tokenProvider) diff --git a/static/js/services/mediastream.js b/static/js/services/mediastream.js index 5ca01e11..40a016c9 100644 --- a/static/js/services/mediastream.js +++ b/static/js/services/mediastream.js @@ -32,7 +32,7 @@ define([ var globalcontext = $("#globalcontext").text(); var context = JSON.parse(globalcontext); - var url = (context.Ssl ? "wss" : "ws") + "://" + context.Host + "/ws"; + var url = (context.Ssl ? "wss" : "ws") + "://" + context.Host + (context.Cfg.B || "/") + "ws"; var version = context.Cfg.Version || "unknown"; console.log("Service version: "+version); console.log("Ws URL: "+ url);