A web blog and personal homepage engine written in Go.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 

91 Zeilen
2.2 KiB

  1. package controllers
  2. import (
  3. "fmt"
  4. "html/template"
  5. "net/http"
  6. "git.kirsle.net/apps/gophertype/pkg/glue"
  7. "git.kirsle.net/apps/gophertype/pkg/mail"
  8. "git.kirsle.net/apps/gophertype/pkg/markdown"
  9. "git.kirsle.net/apps/gophertype/pkg/responses"
  10. "git.kirsle.net/apps/gophertype/pkg/session"
  11. "git.kirsle.net/apps/gophertype/pkg/settings"
  12. "github.com/albrow/forms"
  13. )
  14. func init() {
  15. glue.Register(glue.Endpoint{
  16. Path: "/contact",
  17. Methods: []string{"GET", "POST"},
  18. Handler: ContactHandler,
  19. })
  20. }
  21. // ContactHandler receives admin emails from users.
  22. func ContactHandler(w http.ResponseWriter, r *http.Request) {
  23. var (
  24. v = responses.NewTemplateVars(w, r)
  25. ses = session.Get(r)
  26. )
  27. // Load their cached name from any previous comments they may have posted.
  28. name, _ := ses.Values["c.name"].(string)
  29. email, _ := ses.Values["c.email"].(string)
  30. for r.Method == http.MethodPost {
  31. // Validate form parameters.
  32. form, _ := forms.Parse(r)
  33. val := form.Validator()
  34. val.Require("message")
  35. if val.HasErrors() {
  36. v.ValidationError = val.ErrorMap()
  37. v.V["Error"] = "Missing required form fields."
  38. break
  39. }
  40. var (
  41. name = form.Get("name")
  42. email = form.Get("email")
  43. subject = form.Get("subject")
  44. message = form.Get("message")
  45. )
  46. // Cache their name in their session for future comments/asks.
  47. ses.Values["c.name"] = name
  48. ses.Values["c.email"] = email
  49. ses.Save(r, w)
  50. // Email the site admin.
  51. if name == "" {
  52. name = "Anonymous"
  53. }
  54. if subject == "" {
  55. subject = "No Subject"
  56. }
  57. go mail.EmailAdmins(mail.Email{
  58. Subject: fmt.Sprintf("Contact Me (%s) from %s: %s", settings.Current.Title, name, subject),
  59. Template: "_builtin/email/generic.gohtml",
  60. Data: map[string]interface{}{
  61. "Subject": subject,
  62. "ReplyTo": email,
  63. "Message": template.HTML(
  64. markdown.RenderMarkdown(fmt.Sprintf(
  65. "Subject: %s\nSender name: %s\nSender email: %s\n\n%s\n",
  66. subject, name, email, message,
  67. )),
  68. ),
  69. },
  70. })
  71. session.Flash(w, r, "Thank you! Your message has been e-mailed to the site owner.")
  72. responses.Redirect(w, r, "/contact")
  73. return
  74. }
  75. v.V["name"] = name
  76. v.V["email"] = email
  77. responses.RenderTemplate(w, r, "_builtin/contact.gohtml", v)
  78. }