Ejemplo n.º 1
0
func authRedirect(res *wcg.Response, req *wcg.Request) {
	ref, _ := req.Session.Get(SESSION_KEY_LOGIN_REF)
	if ref != "" && strings.HasPrefix(ref, "/") {
		res.Redirect(wcg.AbsoluteUrl(req, ref), http.StatusFound)
	} else {
		res.Redirect("/", http.StatusFound)
	}
}
Ejemplo n.º 2
0
// HTTP 201 Created
func (api *ApiHelper) Created(res *wcg.Response, req *wcg.Request, id string) {
	loc := path.Join(req.URL().Path, fmt.Sprintf("%s.json", id))
	res.WriteJsonWithStatus(201, nil, map[string]interface{}{
		"ok":       true,
		"id":       id,
		"location": wcg.AbsoluteUrl(req, loc),
	})
}
Ejemplo n.º 3
0
func showBlogHandler(res *wcg.Response, req *wcg.Request) {
	blog, posts, err := fetchBlog(res, req, true)
	if err != nil {
		return
	}
	res.SetLocal("Og", &supports.Og{Title: blog.Title,
		Type: "blog", Url: wcg.AbsoluteUrl(req, req.URL().Path),
		Description: blog.Description, SiteName: AppConfig.SiteTitle,
	})
	res.SetLocal("blog", blog)
	res.SetLocal("is_owner", blog.OwnerId == req.User.Id())
	res.SetLocal("posts", posts)
	res.SetLocal("js", "/static/js/blog.js")
	res.Templates("blog.html", "header.html", "footer.html", "parts/post_part.html")
}
Ejemplo n.º 4
0
func setupPages(app *App) {
	app.Page.Navi("/", "記事一覧").
		AssetPath("/blogapp/index.js").
		Templates("./blogapp/list.html", "./blogapp/post_part.html").
		Handler(
			func(res *wcg.Response, req *wcg.Request, p *lib.Page) {
				result, err := queryPosts(res, req, 20)
				if err != nil {
					panic(err)
				}
				p.Ogp("type", "blog")
				res.SetLocal("Posts", result.Posts)
				res.SetLocal("Next", result.Next)
				res.SetLocal("Previous", result.Previous)
			},
		)

	app.Page.Navi("/admin/editor.html", "記事管理").
		RequireAuth(lib.Admin).
		Templates("./blogapp/admin/editor.html").
		AssetPath("/blogapp/admin/editor.js")

	app.Page.Page("/posts/:id.html", "").
		Templates("./blogapp/show.html", "./blogapp/post_part.html").
		AssetPath("/blogapp/posts/show.js").
		Handler(
			func(res *wcg.Response, req *wcg.Request, p *lib.Page) {
				var post blog.Post
				driver := NewPostDriver(lib.NewAppContextFromRequest(req))
				key := driver.NewKey(req.Param("id"), 0, nil)
				err := driver.Get(key, &post)
				if err != nil {
					lib.Error(res, req, err)
					return
				}
				p.Title = post.Title
				p.Canonical = wcg.AbsoluteUrl(req, app.Page.Path(fmt.Sprintf("/posts/%s.html", post.Id)))
				p.Ogp("title", fmt.Sprintf("%s: %s", p.App.Title, p.Title))
				p.Ogp("url", p.Canonical)
				p.Ogp("type", "article")
				p.Ogp("description", post.Summary())
				imageUrl := post.ExtractFirstImageUrl()
				if imageUrl != "" {
					p.Ogp("image", imageUrl)
				}
				res.SetLocal("Post", post)
			})
}
Ejemplo n.º 5
0
func (p *Page) _processRequest(res *wcg.Response, req *wcg.Request) {
	// instance of Page
	pi := &Page{}
	pi.App = p.App
	pi.Title = p.Title
	pi.Path = p.Path
	pi.assetPath = p.assetPath
	if p.templates != nil {
		pi.templates = make([]string, len(p.templates))
		for i := range p.templates {
			pi.templates[i] = p.templates[i]
		}
	}
	pi.Ogp("site_name", "SPEEDLAND Project")
	// pre process to update page attributes
	if p.handler != nil {
		p.handler(res, req, pi)
		if res.IsClosed() {
			return
		}
	}

	// automatically set ogp attributes if not set.
	if _, ok := pi.ogp["title"]; !ok {
		pi.Ogp("title", fmt.Sprintf("%s: %s", pi.App.Title, pi.Title))
	}
	if _, ok := pi.ogp["url"]; !ok {
		pi.Ogp("url", wcg.AbsoluteUrl(req, req.URL().RequestURI()))
	}

	// set default OGP from app settings.
	for k, v := range pi.App.DefaultOgp {
		pi.Ogp(k, v)
	}

	res.SetLocal("Page", pi)
	res.SetLocal("Ogp", pi.ogp)
	res.SetLocal("Request", req)
	res.SetLocal("TrackingCode", TrackingCode)
	res.SetLocal("__JsBundle__", pi.GetAssetPath())
	res.SetLocal("__APP_COMMIT", APP_COMMIT)
	res.SetLocal("__APP_TIMESTAMP", APP_TIMESTAMP)
	template_paths := []string{"./header.html", "./footer.html"}
	res.Templates(append(pi.GetTemplates(), template_paths...)...)
}
Ejemplo n.º 6
0
func init() {
	app.Cron.Get(
		"Monitor API tokens",
		"every 1 minutes",
		"/api_tokens/",
		lib.Admin.Required(
			func(res *wcg.Response, req *wcg.Request) {
				var tokens []*models.ApiToken
				d := lib.NewApiTokenDriver(lib.NewAppContextFromRequest(req))
				_, err := d.NewQuery().GetAll(&tokens)
				if err != nil {
					panic(err)
				}

				m := 0
				c := 0
				for _, t := range tokens {
					if t.AlertOn > 0 {
						m += 1
						if t.ShouldAlert() {
							c += 1
							req.Logger.Fatal(
								"API Token '%s' losts access. The last access time was %s (Threshould: %d minutes).",
								t.Token,
								t.LastAccess,
								t.AlertOn,
							)
						}
					}
				}
				if c > 0 {
					notifications.SendAlert(
						req,
						"API token(s) have not been accessed for a while.",
						fmt.Sprintf(TMPL_ALERT_BODY, c, wcg.AbsoluteUrl(req, "/admin/clients.html")),
					)
				}
				res.WriteJson(map[string]interface{}{
					"alerted_tokens":   c,
					"monitored_tokens": m,
					"total_tokens":     len(tokens),
				})
			},
		))
}
Ejemplo n.º 7
0
func showPostHandler(res *wcg.Response, req *wcg.Request) {
	blog, _, err := fetchBlog(res, req, false)
	if err != nil {
		return
	}
	post, err := fetchPost(res, req, blog)
	if err != nil {
		return
	}
	res.SetLocal("Og", &supports.Og{Title: post.Title, Type: "article",
		Url: wcg.AbsoluteUrl(req, req.URL().Path), SiteName: blog.Title,
		Description: wcg.Shorten(post.Content, 80),
	})
	res.SetLocal("blog", blog)
	res.SetLocal("post", post)
	res.SetLocal("is_owner", post.OwnerId == req.User.Id())
	res.SetLocal("js", "/static/js/post.js")
	res.Templates("post.html", "header.html", "footer.html", "parts/post_part.html")
}
Ejemplo n.º 8
0
func createBlogHandler(res *wcg.Response, req *wcg.Request) {
	driver := models.NewBlogDriver(gae.NewContext(req), req.Logger)
	blog, err := driver.CreateBlog(
		req.Form("path"),
		req.Form("title"),
		req.Form("description"),
		req.User.Id(),
	)
	if err != nil {
		if err == models.ErrBlogAlreadyExists {
			res.WriteJsonWithStatus(409, nil, already_taken)
		} else if supports.IsValidationError(err) {
			res.WriteJsonWithStatus(400, nil, err)
		} else {
			res.RenderInternalError(err.Error())
		}
		return
	}
	res.WriteJsonWithStatus(201, map[string]string{
		"location": wcg.AbsoluteUrl(req, "/"+blog.Id),
	}, ok)
}