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