dashboard: handle correctly baseurl option

On the first public.html GET, load the index.html file, as Go
Template.
This commit is contained in:
nemunaire 2019-09-07 00:25:56 +02:00
parent ca891cd9b2
commit ded583008a
3 changed files with 46 additions and 22 deletions

View file

@ -1,8 +1,13 @@
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strings"
"time"
@ -14,16 +19,37 @@ import (
"github.com/julienschmidt/httprouter"
)
var BaseURL = "/"
var forwarder *string = nil
var fwdPublicJson = false
var indexTmpl []byte
func getIndexHtml(w io.Writer) {
if len(indexTmpl) == 0 {
if file, err := os.Open(path.Join(StaticDir, "index.html")); err != nil {
log.Println("Unable to open index.html: ", err)
} else {
defer file.Close()
if indexTmpl, err = ioutil.ReadAll(file); err != nil {
log.Println("Cannot read whole index.html: ", err)
} else {
indexTmpl = bytes.Replace(indexTmpl, []byte("{{.urlbase}}"), []byte(path.Clean(path.Join(BaseURL+"/", "nuke"))[:len(path.Clean(path.Join(BaseURL+"/", "nuke")))-4]), -1)
}
}
}
w.Write(indexTmpl)
}
func init() {
api.Router().GET("/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
http.Redirect(w, r, "public0.html", http.StatusFound)
})
for i := 0; i <= 9; i++ {
api.Router().GET(fmt.Sprintf("/public%d.html", i), func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
getIndexHtml(w)
})
}