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"
html "html/template"
"net/http"
"os"
"path/filepath"
"strings"
text "text/template"
)
@ -16,14 +18,6 @@ var chatTemplates map[string]*text.Template
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{
fmt.Sprint(DTInvalid, 0): "wot",
@ -38,6 +32,20 @@ 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)
// 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
for key, files := range serverTemplateDefs {

View File

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

View File

@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net/http"
"os"
"path"
"path/filepath"
"strings"
@ -44,24 +45,32 @@ 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 {
case "/favicon.ico":
http.ServeFile(w, r, "./favicon.png")
http.ServeFile(w, r, runPath()+"/favicon.png")
return
case "/justchat":
http.ServeFile(w, r, "./static/justchat.html")
http.ServeFile(w, r, runPath()+"/static/justchat.html")
return
case "/justvideo":
http.ServeFile(w, r, "./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 ./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) {
@ -69,17 +78,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, "./static/main.wasm")
http.ServeFile(w, r, 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, "./static/img/"+base)
http.ServeFile(w, r, runPath()+"/static/img/"+base)
}
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

36
main.go
View File

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