From 56b97bd9bec99aaad3eb19881ee01fe426428231 Mon Sep 17 00:00:00 2001 From: Marin Ivanov Date: Fri, 16 Jan 2026 21:45:10 +0200 Subject: show results page after submit --- main.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index c4bf010..75406bc 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "encoding/base64" "encoding/json" "fmt" + "html/template" "io" "log" "net/http" @@ -33,9 +34,11 @@ type Metadata struct { ContentType string `json:"contentType"` } -//go:embed form.html +//go:embed form.html result.html var static embed.FS +var tmpl = template.Must(template.ParseFS(static, "*.html")) + func detectMimetype(input io.Reader) (mimeType string, fileext string, recycled io.Reader, err error) { header := bytes.NewBuffer(nil) mtype, err := mimetype.DetectReader(io.TeeReader(input, header)) @@ -154,8 +157,14 @@ func (s *Server) formPost(w http.ResponseWriter, r *http.Request) { return } log.Printf("Created posting: %s", name) - w.Header().Add("location", fmt.Sprintf("%s/%s\n", s.host, name)) - w.WriteHeader(http.StatusFound) + + link := fmt.Sprintf("%s/%s\n", s.host, name) + tmplData := map[string]string{ + "Link": link, + } + if err := tmpl.ExecuteTemplate(w, "result.html", tmplData); err != nil { + w.WriteHeader(http.StatusInternalServerError) + } } func (s *Server) servePosting(w http.ResponseWriter, r *http.Request, name string) { @@ -234,8 +243,7 @@ func (s *Server) indexGet(w http.ResponseWriter, r *http.Request) { path := r.URL.Path if path == "/" { w.Header().Add("content-type", "text/html; charset=UTF-8") - f, _ := static.Open("form.html") - if _, err := io.Copy(w, f); err != nil { + if err := tmpl.ExecuteTemplate(w, "form.html", nil); err != nil { w.WriteHeader(http.StatusInternalServerError) } } else { -- cgit v1.2.3