From f4d93d480f99a3579df1091a1579ae9d158d2a5f Mon Sep 17 00:00:00 2001 From: Marin Ivanov Date: Tue, 20 Jan 2026 23:09:13 +0200 Subject: redirect after successful post --- main.go | 37 +++++++++++++++++-------------------- template.html | 4 ++-- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/main.go b/main.go index 1ba9292..74d62df 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "io" "log" "net/http" + "net/url" "os" "path/filepath" "strings" @@ -208,24 +209,10 @@ func (s *Server) post(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError) return } - - var textLink string - var fileLink string - if textName != "" { - log.Printf("Created posting: %s", textName) - textLink = fmt.Sprintf("%s/%s", s.host, textName) - } - if fileName != "" { - log.Printf("Created posting: %s", fileName) - fileLink = fmt.Sprintf("%s/%s", s.host, fileName) - } - tmplData := map[string]string{ - "TextLink": textLink, - "FileLink": fileLink, - } - if err := tmpl.ExecuteTemplate(w, "template.html", tmplData); err != nil { - w.WriteHeader(http.StatusInternalServerError) - } + log.Printf("Created posting: text=%s, file=%s", textName, fileName) + location := fmt.Sprintf("/link?text=%s&file=%s", url.QueryEscape(textName), url.QueryEscape(fileName)) + w.Header().Add("location", location) + w.WriteHeader(http.StatusFound) } else { if mimeType == "application/x-www-form-urlencoded" { mimeType = "auto" @@ -244,12 +231,22 @@ func (s *Server) post(w http.ResponseWriter, r *http.Request) { func (s *Server) get(w http.ResponseWriter, r *http.Request) { path := r.URL.Path - if path == "/" { + switch path { + case "/": w.Header().Add("content-type", "text/html; charset=UTF-8") if err := tmpl.ExecuteTemplate(w, "template.html", map[string]any{"Form": true}); err != nil { w.WriteHeader(http.StatusInternalServerError) } - } else { + case "/link": + tmplData := map[string]string{ + "Host": s.host, + "TextLink": r.FormValue("text"), + "FileLink": r.FormValue("file"), + } + if err := tmpl.ExecuteTemplate(w, "template.html", tmplData); err != nil { + w.WriteHeader(http.StatusInternalServerError) + } + default: s.servePosting(w, r, filepath.Base(path)) } } diff --git a/template.html b/template.html index 37d057b..45227c1 100644 --- a/template.html +++ b/template.html @@ -198,14 +198,14 @@ {{if .TextLink }}
Text Link - {{ .TextLink }} + {{ .Host }}{{ .TextLink }}
{{end}} {{if .FileLink }}
File Link - {{ .FileLink }} + {{ .Host }}{{ .FileLink }}
{{end}} -- cgit v1.2.3