mirror of
https://github.com/github-tijlxyz/khatru-pyramid.git
synced 2025-06-23 16:05:28 +00:00
embed ui files
This commit is contained in:
parent
e47ed79135
commit
feee34b683
34
handler.go
34
handler.go
@ -1,8 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func inviteDataApiHandler(w http.ResponseWriter, re *http.Request) {
|
||||
@ -18,3 +20,35 @@ func inviteDataApiHandler(w http.ResponseWriter, re *http.Request) {
|
||||
http.Error(w, "internal server error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
// embed ui files
|
||||
//go:embed ui/dist/*
|
||||
var uiContent embed.FS
|
||||
|
||||
func embeddedUIHandler(w http.ResponseWriter, r *http.Request) {
|
||||
path := "ui/dist" + r.URL.Path
|
||||
|
||||
if r.URL.Path == "/" {
|
||||
path = "ui/dist/index.html"
|
||||
}
|
||||
|
||||
data, err := uiContent.ReadFile(path)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
contentType := http.DetectContentType(data)
|
||||
if strings.HasSuffix(r.URL.Path, ".js") {
|
||||
contentType = "application/javascript"
|
||||
} else if strings.HasSuffix(r.URL.Path, ".css") {
|
||||
contentType = "text/css"
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
|
||||
if _, err := w.Write(data); err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
2
main.go
2
main.go
@ -55,7 +55,7 @@ func main() {
|
||||
// invitedata api
|
||||
relay.Router().HandleFunc("/invitedata", inviteDataApiHandler)
|
||||
// ui
|
||||
relay.Router().Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("./ui/dist"))))
|
||||
relay.Router().HandleFunc("/", embeddedUIHandler)
|
||||
|
||||
fmt.Println("running on :3334")
|
||||
http.ListenAndServe(":3334", relay)
|
||||
|
Loading…
x
Reference in New Issue
Block a user