Commits

Miki Tebeka  committed c9c9b89

go1

  • Participants
  • Parent commits ba24e67
  • Tags appengine

Comments (0)

Files changed (3)

 application: go-shortie
 version: 1
 runtime: go
-api_version: 3
+api_version: go1
 
 handlers:
 - url: /static

File compile.sh

-#!/bin/bash
-
-# Compile the code
-
-cd shortie
-GOROOT=/opt/google_appengine_go/goroot \
-    /opt/google_appengine_go/goroot/bin/6g *.go

File shortie/shortie.go

 	"appengine/delay"
 	"appengine/memcache"
 	"appengine/user"
+	"errors"
 	"fmt"
-	"http"
-	"os"
+	"net/http"
 	"regexp"
 	"strings"
-	"template"
+	"text/template"
 	"time"
 )
 
 }
 
 // urlCount return the current count of urls.
-func urlCount(ctx appengine.Context) (int64, os.Error) {
+func urlCount(ctx appengine.Context) (int64, error) {
 	key := datastore.NewKey(ctx, counterKind, counterKeyName, 0, nil)
 	counter := new(Counter)
 	if err := datastore.Get(ctx, key, counter); err != nil && err != datastore.ErrNoSuchEntity {
 /* nextId returns the next short url. 
 We use the global counter and then encode the last count in base62.
 */
-func nextId(ctx appengine.Context) (string, os.Error) {
+func nextId(ctx appengine.Context) (string, error) {
 	var count int64
 
-	err := datastore.RunInTransaction(ctx, func(ctx appengine.Context) os.Error {
+	err := datastore.RunInTransaction(ctx, func(ctx appengine.Context) error {
 		key := datastore.NewKey(ctx, counterKind, counterKeyName, 0, nil)
 		counter := new(Counter)
 		if err := datastore.Get(ctx, key, counter); err != nil && err != datastore.ErrNoSuchEntity {
 	Short   string
 	Long    string
 	User    string
-	Created int64
+	Created time.Time
 	Hits    int64
 }
 
 }
 
 // getURL fetches a URL from the datastore by short url.
-func getURL(ctx appengine.Context, short string) (*URL, os.Error) {
+func getURL(ctx appengine.Context, short string) (*URL, error) {
 	key := urlKey(ctx, short)
 	url := new(URL)
 	err := datastore.Get(ctx, key, url)
 }
 
 // incHits increments hit count on url (this is done when short url is resolved).
-func incHits(ctx appengine.Context, short string) os.Error {
-	return datastore.RunInTransaction(ctx, func(ctx appengine.Context) os.Error {
+func incHits(ctx appengine.Context, short string) error {
+	return datastore.RunInTransaction(ctx, func(ctx appengine.Context) error {
 		url, err := getURL(ctx, short)
 		if err != nil {
 			return err
 var delayedInc = delay.Func("hits", incHits)
 
 // userURLs return a list of user urls, ordered by time (max 1000).
-func userURLs(ctx appengine.Context, user string) ([]*URL, os.Error) {
+func userURLs(ctx appengine.Context, user string) ([]*URL, error) {
 	query := datastore.NewQuery(urlKind).
 		Filter("User =", user).
 		Order("-Created").
 func init() {
 	var fmap = template.FuncMap{
 		"strftime": timeFormatter,
-		"oddeven" : oddEven,
+		"oddeven":  oddEven,
 	}
 	// homeHTML is in html.go
 	homeTemplate = template.Must(template.New("home").Funcs(fmap).Parse(homeHTML))
 	}
 
 	ctx := appengine.NewContext(r)
-	var err os.Error
+	var err error
 	params := &homeParams{
 		User: "stranger",
 	}
 	// Run at end. We check "err" and update params if needed. Then serve homeTemplate.
 	defer func() {
 		if err != nil {
-			params.Error = err.String()
+			params.Error = err.Error()
 			ctx.Errorf("%v", err)
 		}
 		homeTemplate.Execute(w, params)
 	if r.Method == "POST" {
 		longURL := strings.TrimSpace(r.FormValue("url"))
 		if len(longURL) == 0 {
-			err = os.NewError("Empty URL")
+			err = errors.New("Empty URL")
 			return
 		}
 
 		}
 
 		if !strings.Contains(longURL, ".") {
-			err = os.NewError(fmt.Sprintf("Bad URL - %s", longURL))
+			err = errors.New(fmt.Sprintf("Bad URL - %s", longURL))
 			return
 		}
 
 			Short:   id,
 			Long:    longURL,
 			User:    params.User,
-			Created: time.Nanoseconds(),
+			Created: time.Now(),
 			Hits:    0,
 		}
 		key := datastore.NewKey(ctx, urlKind, id, 0, nil)
 		}
 	} else if err != nil {
 		ctx.Errorf("memcache error - %s", err)
-		http.Error(w, err.String(), http.StatusInternalServerError)
+		http.Error(w, err.Error(), http.StatusInternalServerError)
 		return
 	} else { // Found in memcache
 		longURL = string(item.Value)
 	if !ok {
 		return "ERR"
 	}
-	t := time.NanosecondsToLocalTime(nsec)
+	t := time.Unix(0, nsec)
 	return t.Format("2006-01-02 15:04")
 }
 
 	if !ok {
 		return "ERR"
 	}
-	if idx % 2 == 0 {
+	if idx%2 == 0 {
 		return "odd"
 	}
 	return "even"