diff options
| author | Jacob McDonnell <jacob@jacobmcdonnell.com> | 2026-04-26 16:52:03 -0400 |
|---|---|---|
| committer | Jacob McDonnell <jacob@jacobmcdonnell.com> | 2026-04-26 16:52:03 -0400 |
| commit | ee64cdc2712a8e7f77c6af02385afd43a2a1e61b (patch) | |
| tree | 8baf3e9ffba5522c901fc04dd118ba25697a1e35 /allowed.go | |
| parent | 97d5c458cfa039d857301e1ca7d5af3beb37131d (diff) | |
refactor: Split Go code into multiple files
Diffstat (limited to 'allowed.go')
| -rw-r--r-- | allowed.go | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/allowed.go b/allowed.go new file mode 100644 index 00000000..e6789206 --- /dev/null +++ b/allowed.go @@ -0,0 +1,48 @@ +package main + +import ( + "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) + } +} |
