本文整理匯總了Golang中github.com/mjibson/goon.FromContext函數的典型用法代碼示例。如果您正苦於以下問題:Golang FromContext函數的具體用法?Golang FromContext怎麽用?Golang FromContext使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了FromContext函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: GetFeed
func GetFeed(c mpg.Context, w http.ResponseWriter, r *http.Request) {
gn := goon.FromContext(c)
f := Feed{Url: r.FormValue("f")}
var stars []string
wg := sync.WaitGroup{}
fk := gn.Key(&f)
q := datastore.NewQuery(gn.Kind(&Story{})).Ancestor(fk).KeysOnly()
q = q.Order("-" + IDX_COL)
if cur := r.FormValue("c"); cur != "" {
if dc, err := datastore.DecodeCursor(cur); err == nil {
q = q.Start(dc)
}
} else {
// grab the stars list on the first run
wg.Add(1)
go c.Step("stars", func(c mpg.Context) {
gn := goon.FromContext(c)
usk := starKey(c, f.Url, "")
q := datastore.NewQuery(gn.Kind(&UserStar{})).Ancestor(gn.Key(usk).Parent()).KeysOnly()
keys, _ := gn.GetAll(q, nil)
stars = make([]string, len(keys))
for i, key := range keys {
stars[i] = starID(key)
}
wg.Done()
})
}
iter := gn.Run(q)
var stories []*Story
for i := 0; i < 20; i++ {
if k, err := iter.Next(nil); err == nil {
stories = append(stories, &Story{
Id: k.StringID(),
Parent: k.Parent(),
})
} else if err == datastore.Done {
break
} else {
serveError(w, err)
return
}
}
cursor := ""
if ic, err := iter.Cursor(); err == nil {
cursor = ic.String()
}
gn.GetMulti(&stories)
wg.Wait()
b, _ := json.Marshal(struct {
Cursor string
Stories []*Story
Stars []string `json:",omitempty"`
}{
Cursor: cursor,
Stories: stories,
Stars: stars,
})
w.Write(b)
}
示例2: handleRegisterUser
// ユーザー登録
func handleRegisterUser(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "リクエストエラー", http.StatusBadRequest)
return
}
c := appengine.NewContext(r)
g := goon.FromContext(c)
var user User
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
c.Infof("body:%s", string(body))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := JsonToStruct(w, body, &user); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
c.Infof("id:%s,name:%s,age:%d,password:%v", user.Id, user.Name, user.Age, user.Password)
if !checkUserId(c, user.Id) {
http.Error(w, "既に同じユーザーIDが登録されています", http.StatusInternalServerError)
return
}
user.Password = PasswordToHash(user.Password)
g.Put(&user)
w.Header().Set("Content-Type", "application/json;charset=utf-8")
w.Write(StructToJson(w, user))
}
示例3: CFix
func CFix(c mpg.Context, w http.ResponseWriter, r *http.Request) {
gn := goon.FromContext(c)
url := r.FormValue("feed")
c.Infof("fix feed %s", url)
f := Feed{Url: url}
if err := gn.Get(&f); err != nil {
c.Criticalf("cfix err: %v", err)
serveError(w, err)
return
}
q := datastore.NewQuery("S").Ancestor(gn.Key(&f))
var ss []*Story
keys, err := q.GetAll(c, &ss)
if err != nil {
c.Errorf("getall err: %v", err)
serveError(w, err)
return
}
c.Infof("trying to fix %v stories", len(ss))
const putLimit = 500
for i := 0; i <= len(keys)/putLimit; i++ {
lo := i * putLimit
hi := (i + 1) * putLimit
if hi > len(keys) {
hi = len(keys)
}
c.Infof("%v - %v", lo, hi)
if _, err := datastore.PutMulti(c, keys[lo:hi], ss[lo:hi]); err != nil {
c.Errorf("err: %v, %v, %v", lo, hi, err)
}
}
}
示例4: SubscribeCallback
func SubscribeCallback(c mpg.Context, w http.ResponseWriter, r *http.Request) {
gn := goon.FromContext(c)
vars := mux.Vars(r)
b, _ := base64.URLEncoding.DecodeString(vars["feed"])
f := Feed{Url: string(b)}
if err := gn.Get(&f); err != nil {
http.Error(w, "", http.StatusNotFound)
return
}
if r.Method == "GET" {
if r.FormValue("hub.mode") != "subscribe" || r.FormValue("hub.topic") != f.Url {
http.Error(w, "", http.StatusNotFound)
return
}
w.Write([]byte(r.FormValue("hub.challenge")))
i, _ := strconv.Atoi(r.FormValue("hub.lease_seconds"))
f.Subscribed = time.Now().Add(time.Second * time.Duration(i))
gn.Put(&f)
c.Debugf("subscribed: %v - %v", f.Url, f.Subscribed)
return
} else {
c.Infof("push: %v", f.Url)
defer r.Body.Close()
b, _ := ioutil.ReadAll(r.Body)
nf, ss := ParseFeed(c, f.Url, b)
err := updateFeed(c, f.Url, nf, ss)
if err != nil {
c.Errorf("push error: %v", err)
}
}
}
示例5: GetContents
func GetContents(c mpg.Context, w http.ResponseWriter, r *http.Request) {
var reqs []struct {
Feed string
Story string
}
b, _ := ioutil.ReadAll(r.Body)
if err := json.Unmarshal(b, &reqs); err != nil {
serveError(w, err)
return
}
scs := make([]*StoryContent, len(reqs))
gn := goon.FromContext(c)
for i, r := range reqs {
f := &Feed{Url: r.Feed}
s := &Story{Id: r.Story, Parent: gn.Key(f)}
scs[i] = &StoryContent{Id: 1, Parent: gn.Key(s)}
}
gn.GetMulti(scs)
ret := make([]string, len(reqs))
for i, sc := range scs {
ret[i] = sc.Content
}
b, _ = json.Marshal(&ret)
w.Write(b)
}
示例6: Update
func Update(c mp.Context, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
id, err := strconv.ParseInt(vars["songId"], 10, 64)
if err != nil {
return err
}
n := goon.FromContext(c)
s := &data.Song{Id: id}
if err := n.Get(s); err != nil {
return err
}
cpm := r.FormValue("cpm")
songs, _, err := chordpro.Parse(strings.NewReader(cpm))
if err != nil {
return err
}
copySong(&songs[0], s)
s.ChordPro = cpm
if _, err = n.Put(s); err != nil {
return err
}
if err = putSongDoc(c, s.Id, &songs[0]); err != nil {
return err
}
http.Redirect(w, r, fmt.Sprintf("/songs/%d", s.Id), http.StatusFound)
return nil
}
示例7: doUncheckout
func doUncheckout(c mpg.Context) (*UserCharge, error) {
cu := user.Current(c)
gn := goon.FromContext(c)
u := User{Id: cu.ID}
uc := UserCharge{Id: 1, Parent: gn.Key(&u)}
if err := gn.Get(&u); err != nil {
return nil, err
}
if err := gn.Get(&uc); err != nil || len(uc.Customer) == 0 {
return nil, err
}
resp, err := stripe(c, "DELETE", "customers/"+uc.Customer, "")
if err != nil {
return nil, err
} else if resp.StatusCode != http.StatusOK {
c.Errorf("%s", resp.Body)
c.Errorf("stripe delete error, but proceeding")
}
if err := gn.RunInTransaction(func(gn *goon.Goon) error {
if err := gn.Get(&u); err != nil && err != datastore.ErrNoSuchEntity {
return err
}
u.Account = AFree
u.Until = uc.Next
if err := gn.Delete(gn.Key(&uc)); err != nil {
return err
}
_, err := gn.Put(&u)
return err
}, nil); err != nil {
return nil, err
}
return &uc, nil
}
示例8: MarkUnread
func MarkUnread(c mpg.Context, w http.ResponseWriter, r *http.Request) {
cu := user.Current(c)
gn := goon.FromContext(c)
read := make(Read)
f := r.FormValue("feed")
s := r.FormValue("story")
rs := readStory{Feed: f, Story: s}
u := &User{Id: cu.ID}
ud := &UserData{
Id: "data",
Parent: gn.Key(u),
}
gn.RunInTransaction(func(gn *goon.Goon) error {
if err := gn.Get(ud); err != nil {
return err
}
gob.NewDecoder(bytes.NewReader(ud.Read)).Decode(&read)
delete(read, rs)
b := bytes.Buffer{}
gob.NewEncoder(&b).Encode(&read)
ud.Read = b.Bytes()
_, err := gn.Put(ud)
return err
}, nil)
}
示例9: UpdateFeeds
func UpdateFeeds(c mpg.Context, w http.ResponseWriter, r *http.Request) {
gn := goon.FromContext(c)
q := datastore.NewQuery(gn.Key(&Feed{}).Kind()).KeysOnly()
q = q.Filter("n <=", time.Now()).Limit(3000)
it := gn.Run(q)
i := 0
for {
k, err := it.Next(nil)
if err == datastore.Done {
break
} else if err != nil {
c.Errorf("next error: %v", err.Error())
break
}
t := taskqueue.NewPOSTTask(routeUrl("update-feed"), url.Values{
"feed": {k.StringID()},
})
if _, err := taskqueue.Add(c, t, "update-feed"); err != nil {
c.Errorf("taskqueue error: %v", err.Error())
}
i++
}
c.Infof("updating %d feeds", i)
fmt.Fprintf(w, "updating %d feeds", i)
}
示例10: includes
func includes(c mpg.Context, r *http.Request) *Includes {
i := &Includes{
Angular: Angular,
BootstrapCss: BootstrapCss,
BootstrapJs: BootstrapJs,
Jquery: Jquery,
Underscore: Underscore,
MiniProfiler: c.Includes(r),
GoogleAnalyticsId: GOOGLE_ANALYTICS_ID,
GoogleAnalyticsHost: GOOGLE_ANALYTICS_HOST,
IsDev: isDevServer,
}
if cu := user.Current(c); cu != nil {
gn := goon.FromContext(c)
user := &User{Id: cu.ID}
if err := gn.Get(user); err == nil {
i.User = user
i.IsAdmin = cu.Admin
if len(user.Messages) > 0 {
i.Messages = user.Messages
user.Messages = nil
gn.Put(user)
}
}
}
return i
}
示例11: TestBuildPageHandler
func TestBuildPageHandler(t *testing.T) {
opt := &aetest.Options{AppID: "unittest", StronglyConsistentDatastore: true}
user := &user.User{Email: "[email protected]", Admin: true, ID: "1234567890"}
inst, err := aetest.NewInstance(opt)
if err != nil {
t.Fatalf(err.Error())
}
defer inst.Close()
request, err := inst.NewRequest("POST", "/build", strings.NewReader("Name=ABC&chip=true&fox=true&hour=3&minute=0"))
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
request.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
aetest.Login(user, request)
recoder := httptest.NewRecorder()
buildPageHandler(recoder, request)
if recoder.Code != 302 {
b, _ := ioutil.ReadAll(recoder.Body)
t.Fatalf("unexpected %d, expected 302, body=%s", recoder.Code, string(b))
}
context := appengine.NewContext(request)
g := goon.FromContext(context)
v := Village{No: 1}
if err := g.Get(&v); err != nil {
t.Fatalf("unexpected err, expected Get Village{No: 1}")
}
if v.Name != "ABC" || v.Chip == false || v.IncludeFox == false || v.UpdatetimeHour != 3 || v.UpdatetimeMinute != 0 {
t.Fatalf("Failed: Cant't get correct Data from datastore")
}
}
示例12: TestGame
func (t *testSuite) TestGame() {
c, err := aetest.NewContext(nil)
if c != nil {
defer c.Close()
}
if err != nil {
t.Error("Could not start aetest - %v", err)
return
}
g := goon.FromContext(c)
game := NewGame(4)
game.Dealer = 0
game.Players[1] = createAI()
game.Players[1].SetHand(g, c, game, Hand{KD, QD, JD, JD, ND, TC, KC, QC, KH, NH, QS, NS}, 0, 1)
game.Players[2] = createAI()
game.Players[2].SetHand(g, c, game, Hand{AD, AD, KD, ND, NC, NC, TH, JH, AS, JS, JS, NS}, 0, 2)
game.Players[3] = createAI()
game.Players[3].SetHand(g, c, game, Hand{AC, AC, KC, JC, JC, TH, QH, QH, JH, AS, TS, KS}, 0, 3)
game.Players[0] = createAI()
game.Players[0].SetHand(g, c, game, Hand{TD, TD, QD, TC, QC, AH, AH, KH, NH, TS, KS, QS}, 0, 0)
game.Meld = make([]uint8, len(game.Players)/2)
game.CountMeld = make([]bool, len(game.Players)/2)
game.Counters = make([]uint8, len(game.Players)/2)
game.HighBid = 20
game.HighPlayer = game.Dealer
game.State = StateBid
game.Next = game.Dealer
//oright = game.Players[0].(*AI).HT
//Log(oright.Owner, "Start of game hands")
//oright.Debug()
game.inc() // so dealer's not the first to bid
game.processAction(g, c, nil, game.Players[game.Next].Tell(nil, nil, game, CreateBid(0, game.Next)))
t.True(true) // just getting to the end successfully counts!
}
示例13: UpdateFeed
func UpdateFeed(c mpg.Context, w http.ResponseWriter, r *http.Request) {
gn := goon.FromContext(appengine.Timeout(c, time.Minute))
url := r.FormValue("feed")
if url == "" {
c.Errorf("empty update feed")
return
}
c.Debugf("update feed %s", url)
last := len(r.FormValue("last")) > 0
f := Feed{Url: url}
s := ""
defer func() {
gn.Put(&Log{
Parent: gn.Key(&f),
Id: time.Now().UnixNano(),
Text: "UpdateFeed - " + s,
})
}()
if err := gn.Get(&f); err == datastore.ErrNoSuchEntity {
c.Errorf("no such entity - " + url)
s += "NSE"
return
} else if err != nil {
s += "err - " + err.Error()
return
} else if last {
// noop
}
if time.Now().Before(f.NextUpdate) {
c.Errorf("feed %v already updated: %v", url, f.NextUpdate)
s += "already updated"
return
}
feedError := func(err error) {
s += "feed err - " + err.Error()
f.Errors++
v := f.Errors + 1
const max = 24 * 7
if v > max {
v = max
} else if f.Errors == 1 {
v = 0
}
f.NextUpdate = time.Now().Add(time.Hour * time.Duration(v))
gn.Put(&f)
c.Warningf("error with %v (%v), bump next update to %v, %v", url, f.Errors, f.NextUpdate, err)
}
if feed, stories, err := fetchFeed(c, f.Url, f.Url); err == nil {
if err := updateFeed(c, f.Url, feed, stories, false, false, last); err != nil {
feedError(err)
} else {
s += "success"
}
} else {
feedError(err)
}
f.Subscribe(c)
}
示例14: handleUploadImage
// 畫像のアップロード
func handleUploadImage(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "リクエストエラー", http.StatusBadRequest)
return
}
c := appengine.NewContext(r)
g := goon.FromContext(c)
key := r.FormValue("key")
blobs, _, err := blobstore.ParseUpload(r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
image := blobs["uploadFile"]
if len(image) == 0 {
http.Error(w, "畫像がありません", http.StatusInternalServerError)
return
}
ibi := ImageBlobInfo{
Id: key,
BlobKey: string(image[0].BlobKey),
}
// keys := datastore.NewIncompleteKey(c, "blobInfo", nil)
// _, err = datastore.Put(c, keys, &ibInfo)
g.Put(&ibi)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write([]byte(ibi.BlobKey))
}
示例15: updateHandler
func updateHandler(w http.ResponseWriter, r *http.Request) {
c := gae.NewContext(r)
g := goon.FromContext(c)
if r.Header.Get("X-AppEngine-Cron") != "true" {
http.NotFound(w, r)
return
}
var ups []UpdateSchedule
if cache, err := memcache.Get(c, "UpdateSchedule"); err == memcache.ErrCacheMiss {
q := datastore.NewQuery("UpdateSchedule")
if _, err := g.GetAll(q, &ups); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if val, err := serialize(&ups); err == nil {
item := memcache.Item{Key: "UpdateSchedule", Value: val, Expiration: time.Hour * 12}
memcache.Add(c, &item)
}
} else if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else {
deserialize(cache.Value, &ups)
}
now := time.Now()
for _, us := range ups {
if us.IsUpdateTime(now) {
if err := updateVillage(us, g); err != nil {
c.Debugf("%v", err)
}
memcache.Delete(c, "UpdateSchedule")
}
}
return
}