2023-03-19 15:07:40 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
2023-03-21 16:22:45 +01:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2023-03-19 15:07:40 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
//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"
|
|
|
|
|
2023-03-21 16:22:45 +01:00
|
|
|
http.Handle("/cdn-cgi/static/", http.StripPrefix("/cdn-cgi/", http.FileServer(http.FS(staticFS))))
|
|
|
|
http.HandleFunc("/cdn-cgi/trace", headersHandler)
|
2023-03-19 15:07:40 +01:00
|
|
|
http.HandleFunc("/", errorHandler)
|
|
|
|
|
2023-03-21 16:22:45 +01:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"port": port,
|
|
|
|
}).Info("Started webserver listening")
|
|
|
|
|
2023-03-19 15:07:40 +01:00
|
|
|
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 == "" {
|
2023-03-21 16:22:45 +01:00
|
|
|
errorData.Title = "Oops 🤓"
|
|
|
|
errorData.Message = "It seems like something went wrong. Please try again later."
|
2023-03-19 15:07:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
t.Execute(w, errorData)
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateInternalServerError(w http.ResponseWriter, err error) {
|
|
|
|
log.Println(err)
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
w.Write([]byte(`{"error": "Internal Server Error"}`))
|
|
|
|
}
|
2023-03-21 16:22:45 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|