func configureBlogs(routes *wcg.Router) { // check user is already signed in or not. routes.All("/admin/blogs/*", func(res *wcg.Response, req *wcg.Request) { if wcg.IsGuest(req.User) { res.Redirect("/", http.StatusFound) } }) // public interfaces. routes.Get("/my/", myblogRedirector(true)) routes.Get("/:blog_id/", showBlogHandler) // admin interfaces routes.Get("/admin/blogs/my/", myblogRedirector(false)) routes.Get("/admin/blogs/:blog_id/", manageBlogHandler) routes.Post("/admin/blogs/", createBlogHandler) routes.Put("/admin/blogs/:blog_id/", updateBlogHandler) routes.Delete("/admin/blogs/:blog_id/", deleteBlogHandler) }
func myblogRedirector(public bool) func(res *wcg.Response, req *wcg.Request) { return func(res *wcg.Response, req *wcg.Request) { if wcg.IsGuest(req.User) { res.Redirect("/", http.StatusSeeOther) } else { driver := models.NewBlogDriver(gae.NewContext(req), req.Logger) blogs, err := driver.FindBlogsByOwner(req.User.Id()) if err != nil { res.RenderInternalError(err.Error()) return } if len(blogs) > 0 { blog := blogs[0] if public { res.Redirect(fmt.Sprintf("/%s/", blog.Id), http.StatusSeeOther) } else { res.Redirect(fmt.Sprintf("/admin/blogs/%s/", blog.Id), http.StatusSeeOther) } } else { res.Redirect("/", http.StatusSeeOther) } } } }