/mydailyline/mydailyline.go

https://code.google.com/p/my-daily-line/ · Go · 130 lines · 103 code · 22 blank · 5 comment · 14 complexity · a24b2913a78a79ec8437aa285a57f755 MD5 · raw file

  1. package mydailyline
  2. import (
  3. "appengine"
  4. "appengine/datastore"
  5. "appengine/user"
  6. //"fmt"
  7. "http"
  8. "strings"
  9. "template"
  10. "time"
  11. )
  12. var (
  13. homeTemplate = template.MustParseFile("home.html", nil)
  14. )
  15. type Greeting struct {
  16. Author string
  17. Content string
  18. Date datastore.Time
  19. DateEntered string
  20. Today string
  21. }
  22. func init() {
  23. http.HandleFunc("/", root)
  24. http.HandleFunc("/Today", today)
  25. http.HandleFunc("/post", post)
  26. http.HandleFunc("/signout", signout)
  27. }
  28. func root(w http.ResponseWriter, r *http.Request) {
  29. //fmt.Fprint(w, "My Daily Line.\n")
  30. homeTemplate.Execute(w, nil)
  31. }
  32. func signout(w http.ResponseWriter, r *http.Request) {
  33. c := appengine.NewContext(r)
  34. url, err := user.LogoutURL(c, "/")
  35. if err != nil {
  36. http.Error(w, err.String(), http.StatusInternalServerError)
  37. return
  38. }
  39. w.Header().Set("Location", url)
  40. w.WriteHeader(http.StatusFound)
  41. return
  42. }
  43. func today(w http.ResponseWriter, r *http.Request) {
  44. c := appengine.NewContext(r)
  45. //fmt.Fprint(w, "My Daily Line.\n")
  46. u := user.Current(c)
  47. if u == nil {
  48. url, err := user.LoginURL(c, r.URL.String())
  49. if err != nil {
  50. http.Error(w, err.String(), http.StatusInternalServerError)
  51. return
  52. }
  53. w.Header().Set("Location", url)
  54. w.WriteHeader(http.StatusFound)
  55. return
  56. }
  57. //q := datastore.NewQuery("Greeting").Order("-Date").Limit(10) DateEntered
  58. q := datastore.NewQuery("Greeting").Filter("Author =",u.String()).Order("-DateEntered").Limit(10)
  59. greetings := make([]Greeting, 0, 10)
  60. if _, err := q.GetAll(c, &greetings); err != nil {
  61. http.Error(w, err.String(), http.StatusInternalServerError)
  62. return
  63. }
  64. if err := guestbookTemplate.Execute(w, greetings); err != nil {
  65. http.Error(w, err.String(), http.StatusInternalServerError)
  66. }
  67. }
  68. var guestbookTemplate = template.MustParse(guestbookTemplateHTML, nil)
  69. //const guestbookTemplateHTML = `
  70. var guestbookTemplateHTML = `
  71. <html>
  72. <body>
  73. Save a memory for Today:
  74. <form action="/post" method="post">
  75. <div><input type="date" value="` + strings.Split(time.LocalTime().Format(time.RFC3339), "T",-1)[0] +`" name="date"></div>
  76. <div><textarea name="content" rows="3" cols="60"></textarea></div>
  77. <div><input type="submit" value="Save"></div>
  78. </form>
  79. {.repeated section @}
  80. {.section Author}
  81. <p>On {DateEntered|html} <b>{@|html}</b> wrote:</p>
  82. {.or}
  83. <p>On {DateEntered|html} an anonymous person wrote:</p>
  84. {.end}
  85. <pre>{Content|html}</pre>
  86. {.end}
  87. <a href='/signout'>Sign Out</a>
  88. </body>
  89. </html>
  90. `
  91. func post(w http.ResponseWriter, r *http.Request) {
  92. c := appengine.NewContext(r)
  93. g := Greeting{
  94. Content: r.FormValue("content"),
  95. Date: datastore.SecondsToTime(time.Seconds()),
  96. DateEntered: r.FormValue("date"),
  97. }
  98. if u := user.Current(c); u != nil {
  99. g.Author = u.String()
  100. }
  101. _, err := datastore.Put(c, datastore.NewIncompleteKey("Greeting"), &g)
  102. if err != nil {
  103. http.Error(w, err.String(), http.StatusInternalServerError)
  104. return
  105. }
  106. http.Redirect(w, r, "/Today", http.StatusFound)
  107. }