Factorize runPath()

This commit is contained in:
Zorglube 2020-08-23 17:54:01 +02:00
parent d63b361d8e
commit 7c33ba91d1
5 changed files with 23 additions and 29 deletions

View File

@ -4,8 +4,6 @@ import (
"fmt" "fmt"
html "html/template" html "html/template"
"net/http" "net/http"
"os"
"path/filepath"
"strings" "strings"
text "text/template" text "text/template"
) )
@ -27,20 +25,12 @@ var chatTemplateDefs map[string]string = map[string]string{
`</span> <span class="cmdme">{{.Message}}</span></span>`, `</span> <span class="cmdme">{{.Message}}</span></span>`,
} }
func runPath() string {
ex, er := os.Executable()
if er != nil {
panic(er)
}
return filepath.Dir(ex)
}
// Called from the server // Called from the server
func InitTemplates() error { func InitTemplates() error {
isServer = true isServer = true
serverTemplates = make(map[string]*html.Template) serverTemplates = make(map[string]*html.Template)
chatTemplates = make(map[string]*text.Template) chatTemplates = make(map[string]*text.Template)
var runPath string = runPath() var runPath string = RunPath()
// keys and files to load for that template // keys and files to load for that template
var serverTemplateDefs map[string][]string = map[string][]string{ var serverTemplateDefs map[string][]string = map[string][]string{

View File

@ -3,6 +3,8 @@ package common
// Misc utils // Misc utils
import ( import (
"os"
"path/filepath"
"regexp" "regexp"
) )
@ -16,3 +18,12 @@ func IsValidName(name string) bool {
return 3 <= len(name) && len(name) <= 36 && return 3 <= len(name) && len(name) <= 36 &&
usernameRegex.MatchString(name) 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)
}

View File

@ -27,16 +27,8 @@ type EmoteInfo struct {
Code string Code string
} }
func runPath() string {
ex, er := os.Executable()
if er != nil {
panic(er)
}
return filepath.Dir(ex)
}
func loadEmotes() error { func loadEmotes() error {
newEmotes, err := processEmoteDir(runPath() + emoteDir) newEmotes, err := processEmoteDir(common.RunPath() + emoteDir)
if err != nil { if err != nil {
return err return err
} }

View File

@ -46,22 +46,23 @@ func (self writeFlusher) Flush() error {
// Serving static files // Serving static files
func wsStaticFiles(w http.ResponseWriter, r *http.Request) { func wsStaticFiles(w http.ResponseWriter, r *http.Request) {
var runPath string = common.RunPath()
switch r.URL.Path { switch r.URL.Path {
case "/favicon.ico": case "/favicon.ico":
http.ServeFile(w, r, runPath()+"/favicon.png") http.ServeFile(w, r, runPath+"/favicon.png")
return return
case "/justchat": case "/justchat":
http.ServeFile(w, r, runPath()+"/static/justchat.html") http.ServeFile(w, r, runPath+"/static/justchat.html")
return return
case "/justvideo": case "/justvideo":
http.ServeFile(w, r, runPath()+"/static/justvideo.html") http.ServeFile(w, r, runPath+"/static/justvideo.html")
return return
} }
goodPath := r.URL.Path[8:len(r.URL.Path)] 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) { 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") w.Header().Set("Cache-Control", "no-cache, must-revalidate")
} }
common.LogDebugln("[static] serving wasm file") 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) { func wsImages(w http.ResponseWriter, r *http.Request) {
base := filepath.Base(r.URL.Path) base := filepath.Base(r.URL.Path)
common.LogDebugln("[img] ", base) 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) { 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 // Handling the websocket

View File

@ -85,7 +85,7 @@ func LoadSettings(filename string) (*Settings, error) {
} }
s.filename = filename s.filename = filename
var logFileDir string = runPath() + "/" + s.LogFile var logFileDir string = common.RunPath() + "/" + s.LogFile
fmt.Printf("Log file: %s\n", logFileDir) fmt.Printf("Log file: %s\n", logFileDir)
if err = common.SetupLogging(s.LogLevel, logFileDir); err != nil { if err = common.SetupLogging(s.LogLevel, logFileDir); err != nil {
return nil, fmt.Errorf("Unable to setup logger: %s", err) return nil, fmt.Errorf("Unable to setup logger: %s", err)