From 7c33ba91d16a387032f009b3a0088295a35e5caa Mon Sep 17 00:00:00 2001 From: Zorglube Date: Sun, 23 Aug 2020 17:54:01 +0200 Subject: [PATCH] Factorize runPath() --- common/templates.go | 12 +----------- common/utils.go | 11 +++++++++++ emotes.go | 10 +--------- handlers.go | 17 +++++++++-------- settings.go | 2 +- 5 files changed, 23 insertions(+), 29 deletions(-) diff --git a/common/templates.go b/common/templates.go index 8659c79..448bc29 100644 --- a/common/templates.go +++ b/common/templates.go @@ -4,8 +4,6 @@ import ( "fmt" html "html/template" "net/http" - "os" - "path/filepath" "strings" text "text/template" ) @@ -27,20 +25,12 @@ var chatTemplateDefs map[string]string = map[string]string{ ` {{.Message}}`, } -func runPath() string { - ex, er := os.Executable() - if er != nil { - panic(er) - } - return filepath.Dir(ex) -} - // Called from the server func InitTemplates() error { isServer = true serverTemplates = make(map[string]*html.Template) chatTemplates = make(map[string]*text.Template) - var runPath string = runPath() + var runPath string = RunPath() // keys and files to load for that template var serverTemplateDefs map[string][]string = map[string][]string{ diff --git a/common/utils.go b/common/utils.go index 4f1dda2..18e68d7 100644 --- a/common/utils.go +++ b/common/utils.go @@ -3,6 +3,8 @@ package common // Misc utils import ( + "os" + "path/filepath" "regexp" ) @@ -16,3 +18,12 @@ func IsValidName(name string) bool { return 3 <= len(name) && len(name) <= 36 && usernameRegex.MatchString(name) } + +// Return the absolut directory containing the MovieNight binary +func RunPath() string { + ex, er := os.Executable() + if er != nil { + panic(er) + } + return filepath.Dir(ex) +} diff --git a/emotes.go b/emotes.go index 9e76bba..6feeee4 100644 --- a/emotes.go +++ b/emotes.go @@ -27,16 +27,8 @@ type EmoteInfo struct { Code string } -func runPath() string { - ex, er := os.Executable() - if er != nil { - panic(er) - } - return filepath.Dir(ex) -} - func loadEmotes() error { - newEmotes, err := processEmoteDir(runPath() + emoteDir) + newEmotes, err := processEmoteDir(common.RunPath() + emoteDir) if err != nil { return err } diff --git a/handlers.go b/handlers.go index c75130f..4815401 100644 --- a/handlers.go +++ b/handlers.go @@ -46,22 +46,23 @@ func (self writeFlusher) Flush() error { // Serving static files func wsStaticFiles(w http.ResponseWriter, r *http.Request) { + var runPath string = common.RunPath() switch r.URL.Path { case "/favicon.ico": - http.ServeFile(w, r, runPath()+"/favicon.png") + http.ServeFile(w, r, runPath+"/favicon.png") return case "/justchat": - http.ServeFile(w, r, runPath()+"/static/justchat.html") + http.ServeFile(w, r, runPath+"/static/justchat.html") return case "/justvideo": - http.ServeFile(w, r, runPath()+"/static/justvideo.html") + http.ServeFile(w, r, runPath+"/static/justvideo.html") return } goodPath := r.URL.Path[8:len(r.URL.Path)] - common.LogDebugf("[static] serving %q from folder %s\n", goodPath, runPath()) + common.LogDebugf("[static] serving %q from folder %s\n", goodPath, runPath) - http.ServeFile(w, r, runPath()+"/static/"+goodPath) + http.ServeFile(w, r, runPath+"/static/"+goodPath) } func wsWasmFile(w http.ResponseWriter, r *http.Request) { @@ -69,17 +70,17 @@ func wsWasmFile(w http.ResponseWriter, r *http.Request) { w.Header().Set("Cache-Control", "no-cache, must-revalidate") } common.LogDebugln("[static] serving wasm file") - http.ServeFile(w, r, runPath()+"/static/main.wasm") + http.ServeFile(w, r, common.RunPath()+"/static/main.wasm") } func wsImages(w http.ResponseWriter, r *http.Request) { base := filepath.Base(r.URL.Path) common.LogDebugln("[img] ", base) - http.ServeFile(w, r, runPath()+"/static/img/"+base) + http.ServeFile(w, r, common.RunPath()+"/static/img/"+base) } func wsEmotes(w http.ResponseWriter, r *http.Request) { - http.ServeFile(w, r, path.Join(runPath()+"/static/", r.URL.Path)) + http.ServeFile(w, r, path.Join(common.RunPath()+"/static/", r.URL.Path)) } // Handling the websocket diff --git a/settings.go b/settings.go index 4b2408d..d560a33 100644 --- a/settings.go +++ b/settings.go @@ -85,7 +85,7 @@ func LoadSettings(filename string) (*Settings, error) { } s.filename = filename - var logFileDir string = runPath() + "/" + s.LogFile + var logFileDir string = common.RunPath() + "/" + s.LogFile fmt.Printf("Log file: %s\n", logFileDir) if err = common.SetupLogging(s.LogLevel, logFileDir); err != nil { return nil, fmt.Errorf("Unable to setup logger: %s", err)