Beispiel #1
1
func WelcomeHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	c.Infof("cs253: Requested URL: %v", r.URL)
	c.Infof("cs253: Http METHOD: %v", r.Method)
	if r.Method == "GET" {
		username := r.FormValue("username")
		if err := welcomeTemplate.Execute(w, username); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	} else {
		tools.Error404(w)
		return
	}
}
Beispiel #2
0
func DateHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	c.Infof("cs253: Requested URL: %v", r.URL)
	c.Infof("cs253: Http METHOD: %v", r.Method)
	if r.Method == "GET" {
		date := Date{
			Month: "",
			Day:   "",
			Year:  "",
		}
		if err := dateTemplate.Execute(w, date); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	} else if r.Method == "POST" {
		d := Date{
			Month: validMonth(r.FormValue("month")),
			Day:   validDay(r.FormValue("day")),
			Year:  validYear(r.FormValue("year")),
		}
		if d.Day == "" || d.Month == "" || d.Year == "" {
			d.Error = "That's an error!"
			if err := dateTemplate.Execute(w, d); err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
		}
		http.Redirect(w, r, "/unit1/thanks", http.StatusFound)
	} else {
		tools.Error404(w)
		return
	}
}
Beispiel #3
0
func ThanksHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	c.Infof("cs253: Requested URL: %v", r.URL)
	c.Infof("cs253: Http METHOD: %v", r.Method)
	if r.Method == "GET" {
		fmt.Fprint(w, "Thanks! That's a totally valid date!")
	} else {
		tools.Error404(w)
		return
	}
}
Beispiel #4
0
// Rot13Handler is the HTTP handler for encoding and decoding (rot13(rot13(x)) = x ) a string
func Rot13Handler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	c.Infof("cs253: Requested URL: %v", r.URL)
	c.Infof("cs253: HTTP METHOD: %v", r.Method)
	if r.Method == "GET" {
		writeFormRot13(w, "")
	} else if r.Method == "POST" {
		var r13 Rot13 = Rot13(r.FormValue("text"))
		c.Infof("cs253: Rot13 %v", r13.Encode())
		writeFormRot13(w, r13.Encode())
	} else {
		tools.Error404(w)
		return
	}
}
Beispiel #5
0
// BlogFrontHandler is the HTTP handler for displaying the most recent posts.
func BlogFrontHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	c.Infof("cs253: Requested URL: %v", r.URL)
	c.Infof("cs253: Method: %v", r.Method)
	if r.Method == "GET" {
		posts := models.RecentPosts(c)
		c.Infof("cs253: Posts len: %v", len(posts))
		for i, _ := range posts {
			c.Infof("cs253: Post id: %v", posts[i].Id)
		}
		writeBlog(c, w, posts)
	} else {
		tools.Error404(w)
		return
	}
}
Beispiel #6
0
// SignupHandler is the HTTP handler to signup.
// It verifies the validity of the inputs writen by the user in the signup form.
func SignupHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	c.Infof("cs253: Requested URL: %v", r.URL)
	c.Infof("cs253: Http METHOD: %v", r.Method)
	if r.Method == "GET" {
		s := Signup{}
		writeFormSignup(w, s)
	} else if r.Method == "POST" {
		s := Signup{
			Username:           r.FormValue("username"),
			Password:           r.FormValue("password"),
			Email:              r.FormValue("email"),
			Verify:             r.FormValue("verify"),
			ErrorUser:          "",
			ErrorPassword:      "",
			ErrorPasswordMatch: "",
			ErrorEmail:         "",
		}
		// verify signup info.
		if !(tools.IsUsernameValid(s.Username) &&
			tools.IsPasswordValid(s.Password) &&
			s.Password == s.Verify) ||
			(len(s.Email) > 0 && !tools.IsEmailValid(s.Email)) {

			if !tools.IsUsernameValid(s.Username) {
				s.ErrorUser = "******"
			}
			if !tools.IsPasswordValid(s.Password) {
				s.ErrorPassword = "******"
			}
			if s.Password != s.Verify {
				s.ErrorPasswordMatch = "Your passwords didn't match."
			}
			if len(s.Email) > 0 && !tools.IsEmailValid(s.Email) {
				s.ErrorEmail = "That's not a valid email."
			}
			s.Password = ""
			s.Verify = ""
			writeFormSignup(w, s)
		} else {
			http.Redirect(w, r, "/unit2/welcome?username="+s.Username, http.StatusFound)
		}
	} else {
		tools.Error404(w)
		return
	}
}
Beispiel #7
0
// NewPostHandler is the HTTP handler to create a new Post
func NewPostHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	c.Infof("cs253: Requested URL: %v", r.URL)
	c.Infof("cs253: Method: %v", r.Method)

	if r.Method == "GET" {
		writeNewPostForm(c, w, &NewPostForm{})
	} else if r.Method == "POST" {
		postForm := NewPostForm{
			r.FormValue("subject"),
			r.FormValue("content"),
			"",
		}
		if !(tools.IsStringValid(postForm.Subject) &&
			tools.IsStringValid(postForm.Content)) {

			postForm.Error = "We need to set both a subject and some content"
			writeNewPostForm(c, w, &postForm)
		} else {
			c.Infof("cs253: Blog new post:")

			postID, _, _ := datastore.AllocateIDs(c, "Post", nil, 1)
			key := datastore.NewKey(c, "Post", "", postID, nil)
			p := models.Post{
				postID,
				postForm.Subject,
				postForm.Content,
				time.Now(),
			}
			key, err := datastore.Put(c, key, &p)
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
			c.Infof("cs253: Blog Key: %v", key.IntID())

			// build url and redirect
			permalinkURL := "/blog/" + strconv.FormatInt(p.Id, 10)
			http.Redirect(w, r, permalinkURL, http.StatusFound)
		}
	} else {
		tools.Error404(w)
		return
	}
}
Beispiel #8
0
// PermalinkHandler is the HTTP handler for displaying a single post.
// post information is retreive via the URL: /blog/postId
func PermalinkHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	c.Infof("cs253: Requested URL: %v", r.URL)
	if r.Method == "GET" {

		path := strings.Split(r.URL.String(), "/")
		intID, _ := strconv.ParseInt(path[2], 0, 64)
		c.Infof("cs253: PATH : %v", intID)
		// build post key
		c.Infof("cs253: postAndTimeByID call : %v", intID)
		postAndTime := models.PostAndTimeByID(c, intID)
		c.Infof("cs253: postAndTimeByID done ! : %v", intID)

		c.Infof("cs253: Post id: %v", postAndTime.Post.Id)
		c.Infof("cs253: Cache hit time: %v", postAndTime.Cache_hit_time)

		writePermalink(c, w, postAndTime)
	} else {
		tools.Error404(w)
		return
	}
}