Commits

Ian Lewis  committed ece99a5

Updated to work with the new go runtime

  • Participants
  • Parent commits f54e90a

Comments (0)

Files changed (3)

 application: ian-test-hr
 version: golang-guestbook
 runtime: go
-api_version: 2
+api_version: 3
 skip_files: |
  ^(.*/)?(
  (app\.yaml)|
     "http"
     "template"
     "strings"
-    "io"
     "time"
-    "fmt"
 )
 
 type Greeting struct {
     return time.SecondsToUTC((int64(t)/1000000) + timeZone["JST"])
 }
 
-func userNameFormatter(wr io.Writer, formatter string, data ...interface{}) {
-    for _, item := range data {
-        s, _ := item.(string)
-        splits := strings.Split(s, "@", 2)
-        fmt.Fprint(wr, splits[0][0:len(splits[0])/2] + "...@" + splits[1])
+func userNameFormatter(data ...interface{}) string {
+    if (len(data) == 1) {
+        s, _ := data[0].(string)
+        splits := strings.SplitN(s, "@", 2)
+        return splits[0][0:len(splits[0])/2] + "...@" + splits[1]
     }
+    return ""
 }
 
-func dateFormatter(wr io.Writer, formatter string, data ...interface{}) {
-    for _, item := range data {
-        date, _ := item.(datastore.Time)
-        fmt.Fprintf(wr, TimeToTime(date).Format("2006-01-02 15:04:05"))
+func dateFormatter(data ...interface{}) string {
+    if (len(data) == 1) {
+        date, _ := data[0].(datastore.Time)
+        return TimeToTime(date).Format("2006-01-02 15:04:05")
     }
+    return ""
 }
 
-func newlineFormatter(wr io.Writer, formatter string, data ...interface{}) {
-    for _, item := range data {
-        b, _ := item.([]byte)
-        s := string(b)
-        fmt.Fprintf(wr, strings.Replace(s, "\n", "<br/>", -1))
+func newlineFormatter(data ...interface{}) string {
+    if (len(data) == 1) {
+        b, _ := data[0].(string)
+        return strings.Replace(b, "\n", "<br/>", -1)
     }
+    return ""
 }
 
 func init() {
     http.HandleFunc("/", func(w http.ResponseWriter, request *http.Request) {
         c := appengine.NewContext(request)
         // TODO: Cache templates
-        fm := template.FormatterMap{}
-        fm["date"] = dateFormatter
-        fm["userName"] = userNameFormatter
-        fm["newlinesbr"] = newlineFormatter
-        t, _ := template.ParseFile("templates/base.html", fm)
+        t := template.New("Guestbook template")
+        t = t.Funcs(template.FuncMap{
+            "date": dateFormatter,
+            "userName": userNameFormatter,
+            "newlinesbr": newlineFormatter,
+        })
+        t, _ = t.ParseFile("templates/base.html")
 
         current_user := user.Current(c)
         var login_url string
                 AccountEmail: current_user.Email,
                 Date: datastore.SecondsToTime(time.Seconds()),
             }
-            datastore.Put(c, datastore.NewIncompleteKey("Greeting"), g)
+            datastore.Put(c, datastore.NewIncompleteKey(c, "Greeting", nil), g)
         }
         http.Redirect(w, request, "/", http.StatusFound)
     })

File templates/base.html

 </head>
 <body>
   <div style="float:right">
-    {.section CurrentUser}
-    {CurrentUser} <a href="{LoginUrl}">Sign Out</a>
-    {.or}
-    <a href="{LoginUrl}">Sign In</a>
-    {.end}
+    {{if .CurrentUser}}
+    {{.CurrentUser}} <a href="{{.LoginUrl}}">Sign Out</a>
+    {{else}}
+    <a href="{{.LoginUrl}}">Sign In</a>
+    {{end}}
   </div>
 
   <h1>Appengine Go! Guestbook <img src="/static/img/appengine-go.png"></h1>
     <div><textarea name="body" rows="10" cols="80"></textarea></div>
     <div><input type="submit" value="Save"></div>
   </form>
-  {.repeated section Greetings}
+  {{range .Greetings}}
   <div>
-    User: {AccountEmail|userName}<br/>
-    {Date|date}
-    <p style="padding-left:10px">{Body|html|newlinesbr}</p>
+    User: {{userName .AccountEmail}}<br/>
+    {{date .Date}}
+    <p style="padding-left:10px">{{html .Body | newlinesbr}}</p>
   </div>
-  {.end}
+  {{end}}
 
   <div style="text-align:center">
   </div>