diff options
| -rw-r--r-- | main.go | 37 | ||||
| -rw-r--r-- | template.html | 4 |
2 files changed, 19 insertions, 22 deletions
@@ -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}} |
