本文整理汇总了Golang中github.com/speedland/wcg.Request类的典型用法代码示例。如果您正苦于以下问题:Golang Request类的具体用法?Golang Request怎么用?Golang Request使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Request类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getResponseHelper
func getResponseHelper(req *wcg.Request) ResponseHelper {
app := req.Local(LOCAL_APP_KEY).(*App)
if strings.HasPrefix(req.URL().Path, "/api") {
return app.Api
}
return app.Page
}
示例2: fetchPost
func fetchPost(res *wcg.Response, req *wcg.Request, blog *models.Blog) (*models.Post, error) {
driver := models.NewPostDriver(gae.NewContext(req), req.Logger)
id := req.Param("post_id")
blogId := req.Param("blog_id")
post, err := driver.FindPostById(id, blogId)
if err != nil {
if err == models.ErrPostNotFound {
res.WriteHeader(404)
res.WriteString("Not found")
res.End()
return nil, err
}
res.RenderInternalError(err.Error())
return nil, err
}
if post.PostState() != models.PostStatePublished {
if post.OwnerId != req.User.Id() {
res.WriteHeader(404)
res.WriteString("Not found")
res.End()
return nil, err
}
}
return post, err
}
示例3: NewContext
// Returns a Context Object.
func NewContext(req *wcg.Request) appengine.Context {
ctx := req.Local("__gaetest__context")
if ctx != nil {
return ctx.(appengine.Context)
}
return appengine.NewContext(req.HttpRequest())
}
示例4: queryShows
func queryShows(res *wcg.Response, req *wcg.Request, q *datastore.Query) (*showQueryResult, error) {
var appCtx = lib.NewAppContextFromRequest(req)
var showList []event.Show
var basePath = req.HttpRequest().URL.Path
per_page := 12 // Restict to 12 due to view rendering.
page := wcg.ParseInt(req.Query("page"), 0, 0, wcg.ParseIntMax)
if _, err := q.Offset(page * per_page).Limit(per_page).GetAll(&showList); err != nil {
return nil, err
}
if showList == nil {
return &showQueryResult{
Shows: make([]EventShow, 0),
}, nil
}
if list, err := NewEventShowList(appCtx, showList); err != nil {
return nil, err
} else {
p := &showQueryResult{
Shows: list,
Current: fmt.Sprintf("%s?page=%d&n=%d", basePath, page, per_page),
}
// prev url
if page > 0 {
p.Previous = fmt.Sprintf("%s?page=%d&n=%d", basePath, page-1, per_page)
}
// next url
if len(p.Shows) == per_page {
p.Next = fmt.Sprintf("%s?page=%d&n=%d", basePath, page+1, per_page)
}
return p, nil
}
}
示例5: fetchPostForUpdate
func fetchPostForUpdate(req *wcg.Request, res *wcg.Response, blog *models.Blog) (*models.Post, error) {
driver := models.NewPostDriver(gae.NewContext(req), req.Logger)
id := req.Param("post_id")
blogId := req.Param("blog_id")
post, err := driver.FindPostById(id, blogId)
if err != nil {
if err == models.ErrPostNotFound {
res.WriteHeader(404)
res.WriteString("Not found")
res.End()
return nil, err
}
res.RenderInternalError(err.Error())
return nil, err
}
if blog.Id != post.BlogId {
res.WriteHeader(403)
res.WriteString("You could not manage that blog.")
res.End()
}
if post.OwnerId != req.User.Id() {
res.WriteHeader(403)
res.WriteString("You could not manage this post.")
res.End()
return nil, err
}
return post, err
}
示例6: GetCurrentApp
func GetCurrentApp(req *wcg.Request) *App {
if app := req.Local(LOCAL_APP_KEY); app != nil {
return app.(*App)
} else {
return nil
}
}
示例7: Forbidden
func (ph *PageHelper) Forbidden(res *wcg.Response, req *wcg.Request) {
res.SetLocal("Ref", req.URL().RequestURI())
res.TemplatesWithStatus(
403,
nil,
"403.html", "header.html", "footer.html",
)
}
示例8: crawlSpecifiedMembers
func crawlSpecifiedMembers(res *wcg.Response, req *wcg.Request, app *App) {
member, ok := app.Members[req.Param("member")]
if !ok {
lib.NotFound(res, req)
return
}
crawl(res, req, member, app)
}
示例9: Created
// 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),
})
}
示例10: indexSpecifiedMember
func indexSpecifiedMember(res *wcg.Response, req *wcg.Request, app *App) {
var appCtx = lib.NewAppContextFromRequest(req)
member, ok := app.Members[req.Param("member")]
if !ok {
lib.NotFound(res, req)
return
}
num := wcg.ParseInt(req.Param("n"), 0, 0, wcg.ParseIntMax)
if num == 0 {
num = wcg.ParseIntMax
}
// Crawling
crawler := ameblo.NewCrawler(appCtx.NewHttpClient())
prefix := strings.TrimSuffix(member.BlogUrl, ".html") // xxxx.html => xxxx-{num}.html
entries := make([]*ameblo.AmebloEntry, 0)
for i := 1; i < num; i += 1 {
url := fmt.Sprintf("%s-%d.html", prefix, i)
req.Logger.Info("Indexing from %s ... ", url)
newentries, err := crawler.CrawlEntryList(url)
if err != nil {
lib.InternalError(res, req, err)
return
}
if len(newentries) > 20 {
panic(fmt.Errorf("Unexpected number of entries (%d) are returned during indexing.", len(newentries)))
}
if len(newentries) == 0 {
break
}
if len(newentries) < 20 {
entries = append(entries, newentries...)
break
}
if len(entries) > 0 && entries[len(entries)-1].Url == newentries[len(newentries)-1].Url {
break
}
entries = append(entries, newentries...)
}
// Save and return resutls
results := make([]string, 0)
for _, ent := range entries {
ent.Owner = member.Name
results = append(results, ent.Url)
}
if err := updateIndexes(appCtx, entries); err != nil {
req.Logger.Error("Failed to update the entry: %v", err)
lib.InternalError(res, req, err) // stopped.
} else {
time.Sleep(10 * time.Second) // TODO: wait for all indexes are updated on datastore.
mc := appCtx.NewMemcacheDriver()
mckey := fmt.Sprintf(MC_KEY_HISTORY, app.Key, member.Name)
mc.Delete(mckey)
res.WriteJson(results)
}
}
示例11: loadEvent
func loadEvent(app *App, req *wcg.Request, key string) (*event.Event, error) {
var e event.Event
d := NewEventDriver(lib.NewAppContextFromRequest(req))
if err := d.Get(d.NewKey(req.Param(key), 0, nil), &e); err != nil {
return nil, err
} else {
return &e, nil
}
}
示例12: updateAttributesFromForm
func updateAttributesFromForm(res *wcg.Response, req *wcg.Request, e *event.Event) error {
var err error
if err = eventFormValidator.Eval(req.HttpRequest().PostForm); err != nil {
return err
}
e.Title = req.Form("title")
e.Link = req.Form("link")
e.ImageLink = req.Form("image_link")
e.YAKeyword = req.Form("ya_keyword")
e.EventType = event.ParseEventType(req.Form("event_type"))
return nil
}
示例13: sendToBigQuery
func sendToBigQuery(req *wcg.Request) error {
var errors = make([]error, 0)
var svc *bigquery.Service
var records []*wcg.LogRecord
data := make(map[string][]*bigquery.TableDataInsertAllRequestRows)
if tmp := req.Local(requestLocalLogCacheKey); tmp == nil {
return nil
} else {
records = tmp.([]*wcg.LogRecord)
}
svc, err := bq.NewService(req)
if err != nil {
return err
}
for _, r := range records {
tableId, row, err := bqTransform(r)
if err != nil {
errors = append(errors, err)
continue
}
if rows, ok := data[tableId]; ok {
rows = append(rows, row)
} else {
data[tableId] = []*bigquery.TableDataInsertAllRequestRows{row}
}
}
for tableId, rows := range data {
ret, err := svc.Tabledata.InsertAll(
LogSinkConfig.BigqueryProject,
LogSinkConfig.BigqueryDataset,
tableId,
&bigquery.TableDataInsertAllRequest{
Kind: "bigquery#tableDataInsertAllRequest",
Rows: rows,
}).Do()
if err != nil {
errors = append(errors, err)
continue
}
if len(ret.InsertErrors) > 0 {
errors = append(errors, fmt.Errorf("InsertErrors: %s", ret.InsertErrors[0]))
continue
}
}
if len(errors) > 0 {
return logTransmissionError(errors)
}
return nil
}
示例14: ahAuthHandler
func ahAuthHandler(res *wcg.Response, req *wcg.Request) bool {
if strings.HasPrefix(req.URL().Path, "/_ah/") {
req.User = &ApiUser{
token: &models.ApiToken{
Token: "x-appengine-ah",
Description: "Dummy API Token (_ah user)",
CreatedAt: time.Now(),
},
lastLogin: time.Now(),
}
return true
}
return false
}
示例15: Redirect
func Redirect(res *wcg.Response, req *wcg.Request) {
urlStr := req.Query("u")
if urlobj, err := url.Parse(urlStr); err == nil && validateUrl(urlobj) {
// TODO: Check blacklist
res.Redirect(urlStr, 302)
return
}
res.TemplatesWithStatus(
404,
nil,
"404.html", "header.html", "footer.html",
)
return
}