Commits

Miki Tebeka committed f1639ab

redirect

Comments (0)

Files changed (1)

shortie/shortie.go

 
 func init() {
 	homeTemplate = template.Must(template.New("home").Parse(homeHTML))
-	http.HandleFunc("/", handler)
+	http.HandleFunc("/", rootHandler)
 }
 
 type homeParams struct {
 	ShortURL   string
 }
 
-func handler(w http.ResponseWriter, r *http.Request) {
+func rootHandler(w http.ResponseWriter, r *http.Request) {
+	if r.URL.Path != "/" {
+		redirectHandler(w, r)
+		return
+	}
+
 	var err os.Error
 	params := &homeParams{
 		User: "stranger",
 
 	params.Count, err = getCount(ctx)
 }
+
+func redirectHandler(w http.ResponseWriter, r *http.Request) {
+	ctx := appengine.NewContext(r)
+	path := r.URL.Path[1:]
+
+	url, err := getUrl(ctx, path)
+	if err != nil {
+		http.NotFound(w, r)
+		return
+	}
+
+	incHits(ctx, path)
+	http.Redirect(w, r, url.Long, http.StatusTemporaryRedirect)
+}