Files
opal/internal/httpserver/webui.go

38 lines
830 B
Go

package httpserver
import (
"net/http"
"path/filepath"
)
func RegisterWebui() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
http.Redirect(w, r, "/web/", http.StatusPermanentRedirect)
return
}
http.NotFound(w, r)
})
fs := http.Dir("web")
http.Handle("/web/", http.StripPrefix("/web/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
f, err := fs.Open(path)
if err != nil {
//file doesn't exist, try adding .html, probably a bad idea
htmlPath := path + ".html"
f, err = fs.Open(htmlPath)
if err != nil {
http.NotFound(w, r)
return
}
defer f.Close()
http.ServeFile(w, r, filepath.Join("web", htmlPath))
return
}
defer f.Close()
http.ServeFile(w, r, filepath.Join("web", path))
})))
}