epee-service/main.go
Maël Gramain ba9e9ab9ab
Some checks failed
ci/woodpecker/push/release Pipeline was successful
ci/woodpecker/push/lint Pipeline was successful
ci/woodpecker/tag/lint Pipeline failed
ci/woodpecker/tag/release Pipeline was successful
lint
2024-05-11 18:49:57 +02:00

94 lines
2.1 KiB
Go

package main
import (
"embed"
"encoding/json"
"fmt"
"html/template"
"net/http"
log "github.com/sirupsen/logrus"
)
//go:embed static
var staticFS embed.FS
//go:embed templates
var templatesFS embed.FS
// Error model
type Error struct {
Title string `json:"title"`
Message string `json:"message"`
}
func main() {
port := "5900"
http.Handle("/cdn-cgi/static/", http.StripPrefix("/cdn-cgi/", http.FileServer(http.FS(staticFS))))
http.HandleFunc("/cdn-cgi/trace", headersHandler)
http.HandleFunc("/", errorHandler)
log.WithFields(log.Fields{
"port": port,
}).Info("Started webserver listening")
err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil)
if err != nil {
log.Fatal(err)
}
}
func errorHandler(w http.ResponseWriter, r *http.Request) {
errorCode := r.URL.Query().Get("error")
// Load error messages from JSON file
jsonData, err := templatesFS.ReadFile("templates/errors.json")
if err != nil {
generateInternalServerError(w, err)
return
}
errors := make(map[string]Error)
err = json.Unmarshal(jsonData, &errors)
if err != nil {
generateInternalServerError(w, err)
return
}
errorData := errors[errorCode]
if errorData.Title == "" {
errorData.Title = "Oops 🤓"
errorData.Message = "It seems like something went wrong. Please try again later."
}
// Render error page
tmpl, err := templatesFS.ReadFile("templates/error.html")
if err != nil {
generateInternalServerError(w, err)
return
}
t, err := template.New("error").Parse(string(tmpl))
if err != nil {
generateInternalServerError(w, err)
return
}
if err := t.Execute(w, errorData); err != nil {
log.Error(err)
}
}
func generateInternalServerError(w http.ResponseWriter, err error) {
log.Println(err)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
if _, err := w.Write([]byte(`{"error": "Internal Server Error"}`)); err != nil {
log.Error(err)
}
}
func headersHandler(w http.ResponseWriter, r *http.Request) {
for name, headers := range r.Header {
for _, h := range headers {
fmt.Fprintf(w, "%v: %v\n", name, h)
}
}
}