From 5fe3036e03b85620905152e00066f18bcca7a90c Mon Sep 17 00:00:00 2001 From: Bertrand Moreau Date: Fri, 21 Aug 2020 20:05:11 +0200 Subject: [PATCH] Very little factorize around "func runPath()" --- common/templates.go | 15 +++++++++------ emotes.go | 11 +++++++---- handlers.go | 9 --------- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/common/templates.go b/common/templates.go index 6790d45..8659c79 100644 --- a/common/templates.go +++ b/common/templates.go @@ -27,17 +27,20 @@ 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) - ex, er := os.Executable() - if er != nil { - panic(er) - } - runPath := filepath.Dir(ex) - fmt.Println(runPath) + var runPath string = runPath() // keys and files to load for that template var serverTemplateDefs map[string][]string = map[string][]string{ diff --git a/emotes.go b/emotes.go index 8954e1b..9e76bba 100644 --- a/emotes.go +++ b/emotes.go @@ -27,13 +27,16 @@ type EmoteInfo struct { Code string } -func loadEmotes() error { +func runPath() string { ex, er := os.Executable() if er != nil { panic(er) } - runPath := filepath.Dir(ex) - newEmotes, err := processEmoteDir(runPath + emoteDir) + return filepath.Dir(ex) +} + +func loadEmotes() error { + newEmotes, err := processEmoteDir(runPath() + emoteDir) if err != nil { return err } @@ -92,7 +95,7 @@ func processEmoteDir(path string) (common.EmotesMap, error) { func findEmotes(dir string, em common.EmotesMap) (common.EmotesMap, error) { //em := NewEmotesMap() - fmt.Printf("finding emotes in %q\n", dir) + //fmt.Printf("finding emotes in %q\n", dir) emotePNGs, err := filepath.Glob(filepath.Join(dir, "*.png")) if err != nil { return em, fmt.Errorf("unable to glob emote directory: %s\n", err) diff --git a/handlers.go b/handlers.go index 554a239..c75130f 100644 --- a/handlers.go +++ b/handlers.go @@ -5,7 +5,6 @@ import ( "fmt" "io" "net/http" - "os" "path" "path/filepath" "strings" @@ -45,14 +44,6 @@ func (self writeFlusher) Flush() error { return nil } -func runPath() string { - ex, er := os.Executable() - if er != nil { - panic(er) - } - return filepath.Dir(ex) -} - // Serving static files func wsStaticFiles(w http.ResponseWriter, r *http.Request) { switch r.URL.Path {