Run from anywhere

Get the runpath at runtime, in order to run MovieNight from whatever
position into the filesystem.
This commit is contained in:
Bertrand Moreau 2020-08-21 11:59:58 +02:00
parent c1572c0c81
commit aaad03f44d
4 changed files with 54 additions and 41 deletions

View File

@ -4,6 +4,8 @@ import (
"fmt" "fmt"
html "html/template" html "html/template"
"net/http" "net/http"
"os"
"path/filepath"
"strings" "strings"
text "text/template" text "text/template"
) )
@ -16,14 +18,6 @@ var chatTemplates map[string]*text.Template
var isServer bool = false var isServer bool = false
// keys and files to load for that template
var serverTemplateDefs map[string][]string = map[string][]string{
"pin": []string{"./static/base.html", "./static/thedoor.html"},
"main": []string{"./static/base.html", "./static/main.html"},
"help": []string{"./static/base.html", "./static/help.html"},
"emotes": []string{"./static/base.html", "./static/emotes.html"},
}
var chatTemplateDefs map[string]string = map[string]string{ var chatTemplateDefs map[string]string = map[string]string{
fmt.Sprint(DTInvalid, 0): "wot", fmt.Sprint(DTInvalid, 0): "wot",
@ -38,6 +32,20 @@ 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)
ex, er := os.Executable()
if er != nil {
panic(er)
}
runPath := filepath.Dir(ex)
fmt.Println(runPath)
// keys and files to load for that template
var serverTemplateDefs map[string][]string = map[string][]string{
"pin": []string{runPath + "/static/base.html", runPath + "/static/thedoor.html"},
"main": []string{runPath + "/static/base.html", runPath + "/static/main.html"},
"help": []string{runPath + "/static/base.html", runPath + "/static/help.html"},
"emotes": []string{runPath + "/static/base.html", runPath + "/static/emotes.html"},
}
// Parse server templates // Parse server templates
for key, files := range serverTemplateDefs { for key, files := range serverTemplateDefs {

View File

@ -15,7 +15,7 @@ import (
"github.com/zorchenhimer/MovieNight/common" "github.com/zorchenhimer/MovieNight/common"
) )
const emoteDir = "./static/emotes/" const emoteDir = "/static/emotes/"
type TwitchUser struct { type TwitchUser struct {
ID string ID string
@ -28,8 +28,12 @@ type EmoteInfo struct {
} }
func loadEmotes() error { func loadEmotes() error {
//fmt.Println(processEmoteDir(emoteDir)) ex, er := os.Executable()
newEmotes, err := processEmoteDir(emoteDir) if er != nil {
panic(er)
}
runPath := filepath.Dir(ex)
newEmotes, err := processEmoteDir(runPath + emoteDir)
if err != nil { if err != nil {
return err return err
} }

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"os"
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
@ -44,24 +45,32 @@ func (self writeFlusher) Flush() error {
return nil return nil
} }
func runPath() string {
ex, er := os.Executable()
if er != nil {
panic(er)
}
return filepath.Dir(ex)
}
// Serving static files // Serving static files
func wsStaticFiles(w http.ResponseWriter, r *http.Request) { func wsStaticFiles(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path { switch r.URL.Path {
case "/favicon.ico": case "/favicon.ico":
http.ServeFile(w, r, "./favicon.png") http.ServeFile(w, r, runPath()+"/favicon.png")
return return
case "/justchat": case "/justchat":
http.ServeFile(w, r, "./static/justchat.html") http.ServeFile(w, r, runPath()+"/static/justchat.html")
return return
case "/justvideo": case "/justvideo":
http.ServeFile(w, r, "./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 ./static/\n", goodPath) common.LogDebugf("[static] serving %q from folder %s\n", goodPath, runPath())
http.ServeFile(w, r, "./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 +78,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, "./static/main.wasm") http.ServeFile(w, r, 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, "./static/img/"+base) http.ServeFile(w, r, 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("./static/", r.URL.Path)) http.ServeFile(w, r, path.Join(runPath()+"/static/", r.URL.Path))
} }
// Handling the websocket // Handling the websocket

36
main.go
View File

@ -6,7 +6,7 @@ import (
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"path/filepath" // "path/filepath"
"github.com/gorilla/sessions" "github.com/gorilla/sessions"
"github.com/nareix/joy4/format" "github.com/nareix/joy4/format"
@ -22,7 +22,6 @@ var (
stats = newStreamStats() stats = newStreamStats()
sAdminPass string sAdminPass string
confFile string confFile string
runPath string
) )
func setupSettings() error { func setupSettings() error {
@ -51,13 +50,6 @@ func setupSettings() error {
} }
func main() { func main() {
ex, er := os.Executable()
if er != nil {
panic(er)
}
runPath := filepath.Dir(ex)
fmt.Println(runPath)
flag.StringVar(&addr, "l", "", "host:port of the HTTP server") flag.StringVar(&addr, "l", "", "host:port of the HTTP server")
flag.StringVar(&rtmpAddr, "r", "", "host:port of the RTMP server") flag.StringVar(&rtmpAddr, "r", "", "host:port of the RTMP server")
flag.StringVar(&sKey, "k", "", "Stream key, to protect your stream") flag.StringVar(&sKey, "k", "", "Stream key, to protect your stream")
@ -141,20 +133,20 @@ func startRmtpServer() {
func startServer() { func startServer() {
// Chat websocket // Chat websocket
http.HandleFunc(runPath+"/ws", wsHandler) http.HandleFunc("/ws", wsHandler)
http.HandleFunc(runPath+"/static/js/", wsStaticFiles) http.HandleFunc("/static/js/", wsStaticFiles)
http.HandleFunc(runPath+"/static/css/", wsStaticFiles) http.HandleFunc("/static/css/", wsStaticFiles)
http.HandleFunc(runPath+"/static/img/", wsImages) http.HandleFunc("/static/img/", wsImages)
http.HandleFunc(runPath+"/static/main.wasm", wsWasmFile) http.HandleFunc("/static/main.wasm", wsWasmFile)
http.HandleFunc(runPath+"/emotes/", wsEmotes) http.HandleFunc("/emotes/", wsEmotes)
http.HandleFunc(runPath+"/favicon.ico", wsStaticFiles) http.HandleFunc("/favicon.ico", wsStaticFiles)
http.HandleFunc(runPath+"/chat", handleIndexTemplate) http.HandleFunc("/chat", handleIndexTemplate)
http.HandleFunc(runPath+"/video", handleIndexTemplate) http.HandleFunc("/video", handleIndexTemplate)
http.HandleFunc(runPath+"/help", handleHelpTemplate) http.HandleFunc("/help", handleHelpTemplate)
http.HandleFunc(runPath+"/pin", handlePin) http.HandleFunc("/pin", handlePin)
http.HandleFunc(runPath+"/emotes", handleEmoteTemplate) http.HandleFunc("/emotes", handleEmoteTemplate)
http.HandleFunc(runPath+"/", handleDefault) http.HandleFunc("/", handleDefault)
err := http.ListenAndServe(addr, nil) err := http.ListenAndServe(addr, nil)
if err != nil { if err != nil {