Miki Tebeka avatar Miki Tebeka committed b41a72c

Comments

Comments (0)

Files changed (1)

shortie/shortie.go

+/* AppEngine demo - a URL shortener */
 package shortie
 
 import (
 )
 
 const (
-	alphabet       = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+	base62Alphabet = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 	counterKeyName = "counter-key-name"
 	counterKind    = "Counter"
 	urlKind        = "Url"
 	hitWorkerPath  = "/_worker/hit"
 )
 
+// Encode a number in base62
 func base62Encode(num uint64) string {
 	if num == 0 {
 		return "0"
 	}
 
 	arr := []uint8{}
-	base := uint64(len(alphabet))
+	base := uint64(len(base62Alphabet))
 
 	for num > 0 {
 		rem := num % base
 	Count int64
 }
 
+// Get current count of urls
 func getCount(ctx appengine.Context) (int64, os.Error) {
 	key := datastore.NewKey(ctx, counterKind, counterKeyName, 0, nil)
 	counter := new(Counter)
 	return counter.Count, nil
 }
 
+// Get next id (short url). We use the global counter and then encode the last count in base62
 func nextId(ctx appengine.Context) (string, os.Error) {
 	var count int64
 
 	return url, err
 }
 
+// Increment 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 {
 		url, err := getURL(ctx, short)
 	}, nil)
 }
 
+// List of user urls, ordered by time (max 1000)
 func userURLs(ctx appengine.Context, user string) ([]*URL, os.Error) {
 	query := datastore.NewQuery(urlKind).
 		Filter("User =", user).
 	var fmap = template.FuncMap{
 		"strftime": timeFormatter,
 	}
+	// homeHTML is in html.go
 	homeTemplate = template.Must(template.New("home").Funcs(fmap).Parse(homeHTML))
 	http.HandleFunc(hitWorkerPath, hitHandler)
 	http.HandleFunc("/", rootHandler)
 }
 
+// Parameters for homeTemplate
 type homeParams struct {
 	User       string
 	LoginTitle string
 	URLs       []*URL
 }
 
+// Add http://<host> suffix to short url. This works both locally and on AppEngine
 func fullUrl(r *http.Request, id string) string {
 	return fmt.Sprintf("http://%s/%s", r.Host, id)
 }
 }
 
 func rootHandler(w http.ResponseWriter, r *http.Request) {
+	// If it has something, we assume it's a short url
 	if r.URL.Path != "/" {
 		redirectHandler(w, r)
 		return
 		User: "stranger",
 	}
 
+	// Run at function end. We check "err" and update params if needed. Then serve homeTemplate.
 	defer func() {
 		if err != nil {
 			params.Error = err.String()
 		return
 	}
 
+	// Offload hit counter update to a task
 	task := taskqueue.NewPOSTTask(hitWorkerPath, map[string][]string{"path": {path}})
 	if _, err := taskqueue.Add(ctx, task, ""); err != nil {
 		return
 	http.Redirect(w, r, url.Long, http.StatusTemporaryRedirect)
 }
 
+// Format time, used by homeTemplate
 func timeFormatter(args ...interface{}) string {
 	nsec, ok := args[0].(int64)
 	if !ok {
 	return t.Format("2006-01-02 15:04")
 }
 
+// Task handler for updating hits
 func hitHandler(w http.ResponseWriter, r *http.Request) {
 	ctx := appengine.NewContext(r)
 	path := r.FormValue("path")
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.