From 8fdf0b61e2d4fb9243e8782774c7f984eb7205f6 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Mon, 7 Feb 2022 22:51:47 +0400 Subject: [PATCH] Added NotFound handler. Removed commented out content-type header. --- internal/handlers/handlers.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index cf4b41b..b732ed8 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -23,6 +23,10 @@ var defaultAmberOptions = amber.Options{PrettyPrint: false, LineNumbers: false} var compiledTemplates map[string]*template.Template +type NotFoundData struct { + MainSite string +} + type IndexData struct { MainSite string FileMaxSz string @@ -176,7 +180,6 @@ func (h *UploadHandlers) Download(w http.ResponseWriter, r *http.Request) { } w.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", name)) - // w.Header().Add("Content-Type", "application/octet-stream") fd, err := os.Open(path) if err != nil { @@ -190,3 +193,13 @@ func (h *UploadHandlers) Download(w http.ResponseWriter, r *http.Request) { http.ServeContent(w, r, path, stat.ModTime(), fd) } + +func (h *UploadHandlers) NotFound(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + if err := compiledTemplates["404"].Execute(w, NotFoundData{ + MainSite: utils.MainSite(r.Host), + }); err != nil { + w.WriteHeader(http.StatusInternalServerError) + h.logErr.Fatalln("failed to execute 404 template:", err) + } +}