本文整理匯總了Golang中appengine.NewContext函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewContext函數的具體用法?Golang NewContext怎麽用?Golang NewContext使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewContext函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: deleteAccountHandler
func deleteAccountHandler(w http.ResponseWriter, r *http.Request) {
id := ""
session, err := sessions.Session(r, "", "datastore")
c1 := appengine.NewContext(r)
c1.Debugf("deleteAccount: id=%v, session=%v, err=%v\n", session["userID"], session, err)
if err == nil {
if session["userID"] != nil {
id = session["userID"].(string)
}
}
if id != "" {
user := loadUser(r, id)
if user.Id != "" {
c := appengine.NewContext(r)
key := datastore.NewKey(c, "User", user.Id, 0, nil)
datastore.Delete(c, key)
session["userID"] = ""
sessions.Save(r, w)
memUserDelete(c, user.Id)
memcache.Delete(c, "user"+user.Id)
http.SetCookie(w, &http.Cookie{Name: "userId", Value: "", Domain: appConfig.AppDomain, Path: "/", MaxAge: -1})
}
}
http.Redirect(w, r, "/", http.StatusFound)
}
示例2: signInTwitterHandler
func signInTwitterHandler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
id := r.FormValue("id")
if id == "" {
serveError(c, w, errors.New("Missing ID parameter"))
return
}
conf := &tweetlib.Config{
ConsumerKey: appConfig.TwitterConsumerKey,
ConsumerSecret: appConfig.TwitterConsumerSecret,
Callback: "http://" + appConfig.AppHost + "/twitter?action=temp&id=" + id}
tok := &tweetlib.Token{}
tr := &tweetlib.Transport{Config: conf,
Token: tok,
Transport: &urlfetch.Transport{Context: c}}
tt, err := tr.TempToken()
if err != nil {
c := appengine.NewContext(r)
serveError(c, w, err)
c.Errorf("%v", err)
return
}
item := &memcache.Item{
Key: tt.Token,
Value: []byte(tt.Secret),
}
// Add the item to the memcache, if the key does not already exist
memcache.Add(c, item)
http.Redirect(w, r, tt.AuthURL(), http.StatusFound)
}
示例3: secureWebAccess
func secureWebAccess(w http.ResponseWriter, r *http.Request) *User {
session, _ := sessionsStore.Get(r, "logindata")
value := session.Values["id"]
c := appengine.NewContext(r)
c.Infof("The Value is: %v", value)
var u User
if value == nil {
c.Infof("Redirecting to login")
http.Redirect(w, r, "/login.do", http.StatusTemporaryRedirect)
} else {
c := appengine.NewContext(r)
q := datastore.NewQuery("User").
Filter("Username = ", value.(string))
//k := datastore.NewKey(c,"User",,0,nil)
if count, _ := q.Count(c); count == 0 {
//what ever you have in the session is not in the datastore, these happends when you delete a user directly from the datastore, and the sesion is still active
http.Redirect(w, r, "/login.do", http.StatusTemporaryRedirect)
return nil
}
t := q.Run(c)
t.Next(&u)
}
return &u
}
示例4: TestEditVisitPageMissingPathInfo
func TestEditVisitPageMissingPathInfo(t *testing.T) {
inst, err := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true})
if err != nil {
t.Fatalf("Failed to create instance: %v", err)
}
defer inst.Close()
url := "/editvisit/"
req, err := inst.NewRequest("GET", url, nil)
if err != nil {
t.Fatalf("Failed to create req: %v", err)
}
aetest.Login(&user.User{Email: "[email protected]"}, req)
w := httptest.NewRecorder()
c := appengine.NewContext(req)
addTestUser(c, "[email protected]", true)
editvisitpage(c, w, req)
code := w.Code
if code != http.StatusBadRequest {
t.Errorf("got code %v, want %v", code, http.StatusBadRequest)
}
body := w.Body.Bytes()
expected := []byte("id is missing in path for update request /editvisit/")
if !bytes.Contains(body, expected) {
t.Errorf("got body %v, did not contain %v", string(body),
string(expected))
}
url += "12345"
req, err = inst.NewRequest("GET", url, nil)
if err != nil {
t.Fatalf("Failed to create req: %v", err)
}
aetest.Login(&user.User{Email: "[email protected]"}, req)
w = httptest.NewRecorder()
c = appengine.NewContext(req)
editvisitpage(c, w, req)
code = w.Code
if code != http.StatusBadRequest {
t.Errorf("got code %v, want %v", code, http.StatusBadRequest)
}
body = w.Body.Bytes()
expected = []byte("id is missing in path for update request /editvisit/")
if !bytes.Contains(body, expected) {
t.Errorf("got body %v, did not contain %v", string(body),
string(expected))
}
}
示例5: data
func data(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
confData, err := Configs(appengine.NewContext(r))
handlePanic(w, c, err)
jsonData, err := json.Marshal(confData)
handlePanic(w, c, err)
fmt.Fprintf(w, "%s", jsonData)
}
示例6: sendLinks
func sendLinks(w http.ResponseWriter, req *http.Request) {
m, _ := url.ParseQuery(req.URL.RawQuery)
col, _ := strconv.Atoi(m["col"][0])
count, _ := strconv.Atoi(m["count"][0])
str := controllers.GetImages(col, count, appengine.NewContext(req))
json, _ := json.Marshal(str)
c := appengine.NewContext(req)
c.Infof("%d", len(str))
io.WriteString(w, string(json))
}
示例7: get
func (p *Profile) get(w http.ResponseWriter, r *http.Request) {
// Create a key with a key for Profile with Key = Id
p.Id = user.Current(appengine.NewContext(r)).ID
c := appengine.NewContext(r)
err := datastore.Get(c, datastore.NewKey(c, "Profile", p.Id, 0, nil), p)
if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
p.put(w, r)
}
}
示例8: twitterVerify
func twitterVerify(w http.ResponseWriter, r *http.Request) {
token := r.FormValue("oauth_token")
id := r.FormValue("id")
c := appengine.NewContext(r)
if id == "" {
serveError(c, w, errors.New("Missing ID parameter"))
return
}
item, _ := memcache.Get(c, token)
secret := string(item.Value)
verifier := r.FormValue("oauth_verifier")
conf := &tweetlib.Config{
ConsumerKey: appConfig.TwitterConsumerKey,
ConsumerSecret: appConfig.TwitterConsumerSecret}
tok := &tweetlib.Token{}
tr := &tweetlib.Transport{Config: conf,
Token: tok,
Transport: &urlfetch.Transport{Context: c}}
tt := &tweetlib.TempToken{Token: token, Secret: secret}
tok, err := tr.AccessToken(tt, verifier)
if err != nil {
c := appengine.NewContext(r)
serveError(c, w, err)
c.Errorf("%v", err)
return
}
tr.Token = tok
tl, _ := tweetlib.New(tr.Client())
u, err := tl.Account.VerifyCredentials(nil)
fmt.Printf("err=%v\n", err)
user := loadUser(r, id)
user.TwitterOAuthToken = tok.OAuthToken
user.TwitterOAuthSecret = tok.OAuthSecret
user.TwitterId = u.IdStr
user.TwitterScreenName = u.ScreenName
if err := saveUser(r, &user); err != nil {
serveError(c, w, err)
return
}
http.Redirect(w, r, "/", http.StatusFound)
}
示例9: primeCache
func primeCache(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
err := model.ForceUpdateStatuses(c)
if err != nil {
http.Error(w, "An error occured while updating the cache", http.StatusInternalServerError)
}
}
示例10: view
func view(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.Atoi(r.FormValue("id"))
c := appengine.NewContext(r)
var view detail_view
// グループ情報を取得
if group, err := model.GetGroup(c, id); err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
return
} else {
view.Group = group
}
// メンバー情報を取得
if memberlist, err := model.MemberList(c, id); err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
return
} else {
view.Member = memberlist
}
// 詳細畫麵を表示
if err := detailTemplate.Execute(w, view); err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
}
}
示例11: handleInboxItem
func handleInboxItem(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
userid, _, err := getSession(c, r)
if err != nil {
sendError(w, r, "No session cookie")
return
}
s := newStore(c)
perma_blobref := r.FormValue("perma")
item, err := s.GetInboxItem(userid, perma_blobref)
if err != nil {
http.Error(w, "Could not get inbox item", http.StatusInternalServerError)
c.Errorf("handleInboxItem: %v", err)
return
}
entry := make(map[string]interface{})
entry["perma"] = perma_blobref
entry["seq"] = item.LastSeq
err = fillInboxItem(s, perma_blobref, item.LastSeq, entry)
if err != nil {
fmt.Fprintf(w, `{"ok":false, "error":%v}`, err.String())
return
}
info, _ := json.Marshal(entry)
fmt.Fprintf(w, `{"ok":true, "item":%v}`, string(info))
}
示例12: handleListInbox
func handleListInbox(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
userid, _, err := getSession(c, r)
if err != nil {
sendError(w, r, "No session cookie")
return
}
s := newStore(c)
inbox, err := s.ListInbox(userid, false)
if err != nil {
sendError(w, r, err.String())
return
}
// Read the updates for all items in the inbox
for _, entry := range inbox {
fillInboxItem(s, entry["perma"].(string), entry["seq"].(int64), entry)
}
j := map[string]interface{}{"ok": true, "items": inbox}
msg, err := json.Marshal(j)
if err != nil {
panic("Cannot serialize")
}
fmt.Fprint(w, string(msg))
}
示例13: ArticleDeleteHandler
func ArticleDeleteHandler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
user := core.AdminUser(c, w)
if user == nil {
return
}
vars := mux.Vars(r)
id, err := strconv.ParseInt(vars["id"], 10, 64)
if err != nil {
core.HandleError(c, w, err)
return
}
article, err := GetArticleById(c, id, false)
if err != nil {
core.HandleNotFound(c, w)
return
}
err = DeleteArticle(c, article)
if err != nil {
core.HandleError(c, w, err)
return
}
http.Redirect(w, r, "/", 302)
}
示例14: ArticlePermaLinkHandler
func ArticlePermaLinkHandler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
vars := mux.Vars(r)
id, err := strconv.ParseInt(vars["id"], 10, 64)
if err != nil {
core.HandleError(c, w, err)
return
}
article, err := GetArticleById(c, id, true)
if err != nil {
core.HandleNotFound(c, w)
return
}
if !article.IsPublic {
user := auth.CurrentUser(c)
if !user.IsAdmin {
core.HandleAuthRequired(c, w)
return
}
}
redirectTo, err := article.URL()
if err != nil {
core.HandleNotFound(c, w)
return
}
http.Redirect(w, r, redirectTo.Path, 302)
}
示例15: handleIndex
func handleIndex(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
// Get user info
userInfo := getUserInfo(c)
// Get post id and page id
pageId, _ := strconv.Atoi(getUrlQuery(r.URL, "pid"))
pageSize := config.PageSize
// Get offset and page numbers
offset, pageNums := getOffset("Article", pageId, pageSize, c)
// New PageSetting
pageSetting := new(PageSetting)
// Setting PageSetting
pageSetting.Title = config.Title
pageSetting.Description = config.Description
pageSetting.Layout = "column2"
pageSetting.ShowSidebar = true
// showNext and showPrev button
if pageId <= 0 || pageId > pageNums {
pageId = 1
}
if pageId < pageNums {
pageSetting.ShowPrev = true
}
if pageId != 1 {
pageSetting.ShowNext = true
}
pageSetting.PrevPageID = pageId + 1
pageSetting.NextPageID = pageId - 1
// Get article data
dbQuery := datastore.NewQuery("Article").Order("-Date").Offset(offset).Limit(pageSize)
articleData, err := getArticleData(dbQuery, true, c)
if err != nil {
serveError(c, w, err)
return
}
// Get widget data
dbQuery = datastore.NewQuery("Widget").Order("Sequence")
widgetData, err := getWidgetData(dbQuery, true, c)
if err != nil {
serveError(c, w, err)
return
}
// New PageData
pageData := &PageData{ User: userInfo, Article: articleData, Widget: widgetData }
// New Page
page := NewPage(pageSetting, pageData)
// Render page
page.Render("index", w)
}