2022-11-16 23:46:24 +01:00
|
|
|
//go:build webui
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2022-12-12 22:53:34 +01:00
|
|
|
|
|
|
|
"github.com/notherealmarco/WASAPhoto/webui"
|
2022-11-16 23:46:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func registerWebUI(hdl http.Handler) (http.Handler, error) {
|
2022-11-24 14:46:29 +01:00
|
|
|
fmt.Printf("Registering WebUI...")
|
2022-11-16 23:46:24 +01:00
|
|
|
distDirectory, err := fs.Sub(webui.Dist, "dist")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error embedding WebUI dist/ directory: %w", err)
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if strings.HasPrefix(r.RequestURI, "/dashboard/") {
|
|
|
|
http.StripPrefix("/dashboard/", http.FileServer(http.FS(distDirectory))).ServeHTTP(w, r)
|
|
|
|
return
|
2022-12-12 22:53:34 +01:00
|
|
|
} else if r.RequestURI == "/" {
|
|
|
|
// Redirect to dashboard
|
|
|
|
http.Redirect(w, r, "/dashboard/", http.StatusTemporaryRedirect)
|
|
|
|
return
|
2022-11-16 23:46:24 +01:00
|
|
|
}
|
|
|
|
hdl.ServeHTTP(w, r)
|
|
|
|
}), nil
|
|
|
|
}
|