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"
html "html/template"
"net/http"
"os"
"path/filepath"
"strings"
text "text/template"
)
@ -27,20 +25,12 @@ var chatTemplateDefs map[string]string = map[string]string{
`</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
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{

View File

@ -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)
}

View File

@ -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
}

View File

@ -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

View File

@ -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)