summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarin Ivanov <[email protected]>2026-01-20 23:09:13 +0200
committerMarin Ivanov <[email protected]>2026-01-20 23:09:13 +0200
commitf4d93d480f99a3579df1091a1579ae9d158d2a5f (patch)
treec949c3e65e4d5f828fec7444ed8e0e65c69f433e
parent162cab0f62357b04f5667a4e2d50e55f4b17db37 (diff)
redirect after successful post
-rw-r--r--main.go37
-rw-r--r--template.html4
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 }}
<fieldset>
<legend>Text Link</legend>
- <a href="{{ .TextLink }}">{{ .TextLink }}</a>
+ <a href="{{ .Host }}{{ .TextLink }}">{{ .Host }}{{ .TextLink }}</a>
</fieldset>
{{end}}
{{if .FileLink }}
<fieldset>
<legend>File Link</legend>
- <a href="{{ .FileLink }}">{{ .FileLink }}</a>
+ <a href="{{ .Host }}{{ .FileLink }}">{{ .Host }}{{ .FileLink }}</a>
</fieldset>
{{end}}