get_root.go 717 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package ui
  2. import (
  3. "io"
  4. "log"
  5. "net/http"
  6. "strings"
  7. )
  8. const rootInputName = "form[url]"
  9. // handleGetRoot handles path /
  10. func handleGetRoot(w http.ResponseWriter, r *http.Request) {
  11. sess, err := store.Get(r, globals.SessionName)
  12. if err != nil {
  13. w.WriteHeader(http.StatusInternalServerError)
  14. return
  15. }
  16. data := struct {
  17. Flashes []interface{}
  18. InputName string
  19. SubmitName string
  20. }{
  21. sess.Flashes(),
  22. rootInputName,
  23. "submit",
  24. }
  25. sess.Save(r, w)
  26. sw := &strings.Builder{}
  27. err = tmpl.ExecuteTemplate(sw, "home", data)
  28. if err != nil {
  29. log.Println(err)
  30. w.WriteHeader(http.StatusInternalServerError)
  31. } else {
  32. w.WriteHeader(http.StatusOK)
  33. io.Copy(w, strings.NewReader(sw.String()))
  34. }
  35. }