khatru-pyramid/handler.go

55 lines
1.3 KiB
Go
Raw Normal View History

2023-09-06 20:49:18 +02:00
package main
import (
2023-09-07 13:06:58 +02:00
"embed"
2023-09-06 20:49:18 +02:00
"encoding/json"
"net/http"
2023-09-07 13:06:58 +02:00
"strings"
2023-09-06 20:49:18 +02:00
)
func inviteDataApiHandler(w http.ResponseWriter, re *http.Request) {
jsonBytes, err := json.Marshal(whitelist)
if err != nil {
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(http.StatusOK)
if _, err := w.Write(jsonBytes); err != nil {
http.Error(w, "internal server error", http.StatusInternalServerError)
}
}
2023-09-07 13:06:58 +02:00
// 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
}
}