package main import ( "encoding/json" "fmt" "html/template" "log" "net/http" "os" "regexp" "slices" ) type AllowedFiles struct { AllowedFiles []string } func (a *AllowedFiles) GetFiles(w http.ResponseWriter, r *http.Request) { log.Println("Request for:", r.URL.String()) url := fmt.Sprintf("static%s", r.URL.String()) htmlRegex, err := regexp.Compile("^static/.*\\.html$") if err != nil { ErrorPage(w, 500) return } if url == "static/" { url = "static/index.html" } if !slices.Contains(a.AllowedFiles, url) { ErrorPage(w, 404) return } if htmlRegex.MatchString(url) { t := template.Must(template.ParseFiles("templates/template.html")) html, err := os.ReadFile(url) if err != nil { ErrorPage(w, 500) return } t.Execute(w, template.HTML(html)) } else { http.ServeFile(w, r, url) } } func GetAllowed(path string) (*AllowedFiles, error) { var allowed AllowedFiles allowedJson, err := os.ReadFile(path) if err != nil { return nil, err } err = json.Unmarshal(allowedJson, &allowed) return &allowed, err }