本文整理匯總了Golang中github.com/MiniProfiler/go/miniprofiler_gae.Context類的典型用法代碼示例。如果您正苦於以下問題:Golang Context類的具體用法?Golang Context怎麽用?Golang Context使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Context類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: AddSubscription
func AddSubscription(c mpg.Context, w http.ResponseWriter, r *http.Request) {
backupOPML(c)
cu := user.Current(c)
url := r.FormValue("url")
o := &OpmlOutline{
Outline: []*OpmlOutline{
&OpmlOutline{XmlUrl: url},
},
}
if err := addFeed(c, cu.ID, o); err != nil {
c.Errorf("add sub error (%s): %s", url, err.Error())
serveError(w, err)
return
}
gn := goon.FromContext(c)
ud := UserData{Id: "data", Parent: gn.Key(&User{Id: cu.ID})}
gn.Get(&ud)
if err := mergeUserOpml(c, &ud, o); err != nil {
c.Errorf("add sub error opml (%v): %v", url, err)
serveError(w, err)
return
}
gn.PutMany(&ud, &Log{
Parent: ud.Parent,
Id: time.Now().UnixNano(),
Text: fmt.Sprintf("add sub: %v", url),
})
if r.Method == "GET" {
http.Redirect(w, r, routeUrl("main"), http.StatusFound)
}
backupOPML(c)
}
示例2: Charge
func Charge(c mpg.Context, w http.ResponseWriter, r *http.Request) {
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 {
serveError(w, err)
return
} else if u.Account != AFree {
serveError(w, fmt.Errorf("You're already subscribed."))
return
}
if err := gn.Get(&uc); err == nil && len(uc.Customer) > 0 {
serveError(w, fmt.Errorf("You're already subscribed."))
return
} else if err != datastore.ErrNoSuchEntity {
serveError(w, err)
return
}
resp, err := stripe(c, "POST", "customers", url.Values{
"email": {u.Email},
"description": {u.Id},
"card": {r.FormValue("stripeToken")},
"plan": {STRIPE_PLAN},
}.Encode())
if err != nil {
serveError(w, err)
return
} else if resp.StatusCode != http.StatusOK {
c.Errorf("%s", resp.Body)
serveError(w, fmt.Errorf("Error"))
return
}
defer resp.Body.Close()
b, _ := ioutil.ReadAll(resp.Body)
var sc StripeCustomer
if err := json.Unmarshal(b, &sc); err != nil {
serveError(w, err)
return
}
if err := gn.RunInTransaction(func(gn *goon.Goon) error {
if err := gn.Get(&u); err != nil && err != datastore.ErrNoSuchEntity {
return err
}
if err := gn.Get(&uc); err != nil && err != datastore.ErrNoSuchEntity {
return err
}
u.Account = APaid
uc.Customer = sc.Id
uc.Last4 = sc.Card.Last4
uc.Created = time.Unix(sc.Created, 0)
_, err := gn.PutMany(&u, &uc)
return err
}, nil); err != nil {
serveError(w, err)
return
}
b, _ = json.Marshal(&uc)
w.Write(b)
}
示例3: Donate
func Donate(c mpg.Context, w http.ResponseWriter, r *http.Request) {
cu := user.Current(c)
gn := goon.FromContext(c)
u := User{Id: cu.ID}
if err := gn.Get(&u); err != nil {
serveError(w, err)
return
}
amount, err := strconv.Atoi(r.FormValue("amount"))
if err != nil || amount < 200 {
serveError(w, fmt.Errorf("bad amount: %v", r.FormValue("amount")))
return
}
resp, err := stripe(c, "POST", "charges", url.Values{
"amount": {r.FormValue("amount")},
"description": {fmt.Sprintf("%v - %v", u.Id, u.Email)},
"card": {r.FormValue("stripeToken")},
"currency": {"usd"},
}.Encode())
if err != nil {
serveError(w, err)
return
} else if resp.StatusCode != http.StatusOK {
c.Errorf("%s", resp.Body)
serveError(w, fmt.Errorf("Error"))
return
}
}
示例4: 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
}
示例5: UploadOpml
func UploadOpml(c mpg.Context, w http.ResponseWriter, r *http.Request) {
opml := Opml{}
if err := json.Unmarshal([]byte(r.FormValue("opml")), &opml.Outline); err != nil {
serveError(w, err)
return
}
backupOPML(c)
cu := user.Current(c)
gn := goon.FromContext(c)
u := User{Id: cu.ID}
ud := UserData{Id: "data", Parent: gn.Key(&u)}
if err := gn.Get(&ud); err != nil {
serveError(w, err)
c.Errorf("get err: %v", err)
return
}
if b, err := json.Marshal(&opml); err != nil {
serveError(w, err)
c.Errorf("json err: %v", err)
return
} else {
l := Log{
Parent: ud.Parent,
Id: time.Now().UnixNano(),
Text: fmt.Sprintf("upload opml: %v -> %v", len(ud.Opml), len(b)),
}
ud.Opml = b
if _, err := gn.PutMany(&ud, &l); err != nil {
serveError(w, err)
return
}
backupOPML(c)
}
}
示例6: 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
}
示例7: UploadOpml
func UploadOpml(c mpg.Context, w http.ResponseWriter, r *http.Request) {
opml := Opml{}
if err := json.Unmarshal([]byte(r.FormValue("opml")), &opml.Outline); err != nil {
serveError(w, err)
return
}
backupOPML(c)
cu := user.Current(c)
gn := goon.FromContext(c)
u := User{Id: cu.ID}
ud := UserData{Id: "data", Parent: gn.Key(&u)}
if err := gn.Get(&ud); err != nil {
serveError(w, err)
c.Errorf("get err: %v", err)
return
}
if b, err := json.Marshal(&opml); err != nil {
saveError(c, fmt.Sprintf("%v", opml), err)
serveError(w, err)
c.Errorf("json err: %v", err)
return
} else {
ud.Opml = b
}
gn.Put(&ud)
backupOPML(c)
}
示例8: 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)
}
示例9: UpdateFeeds
func UpdateFeeds(c mpg.Context, w http.ResponseWriter, r *http.Request) {
q := datastore.NewQuery("F").KeysOnly().Filter("n <=", time.Now())
q = q.Limit(10 * 60 * 2) // 10/s queue, 2 min cron
it := q.Run(appengine.Timeout(c, time.Minute))
tc := make(chan *taskqueue.Task)
done := make(chan bool)
i := 0
u := routeUrl("update-feed")
go taskSender(c, "update-feed", tc, done)
for {
k, err := it.Next(nil)
if err == datastore.Done {
break
} else if err != nil {
c.Errorf("next error: %v", err.Error())
break
}
tc <- taskqueue.NewPOSTTask(u, url.Values{
"feed": {k.StringID()},
})
i++
}
close(tc)
<-done
c.Infof("updating %d feeds", i)
}
示例10: DeleteOldFeed
func DeleteOldFeed(c mpg.Context, w http.ResponseWriter, r *http.Request) {
g := goon.FromContext(c)
oldDate := time.Now().Add(-time.Hour * 24 * 90)
feed := Feed{Url: r.FormValue("f")}
if err := g.Get(&feed); err != nil {
c.Criticalf("err: %v", err)
return
}
if feed.LastViewed.After(oldDate) {
return
}
q := datastore.NewQuery(g.Kind(&Story{})).Ancestor(g.Key(&feed)).KeysOnly()
keys, err := q.GetAll(c, nil)
if err != nil {
c.Criticalf("err: %v", err)
return
}
q = datastore.NewQuery(g.Kind(&StoryContent{})).Ancestor(g.Key(&feed)).KeysOnly()
sckeys, err := q.GetAll(c, nil)
if err != nil {
c.Criticalf("err: %v", err)
return
}
keys = append(keys, sckeys...)
c.Infof("delete: %v - %v", feed.Url, len(keys))
err = g.DeleteMulti(keys)
if err != nil {
c.Criticalf("err: %v", err)
}
}
示例11: SubscribeFeed
func SubscribeFeed(c mpg.Context, w http.ResponseWriter, r *http.Request) {
gn := goon.FromContext(c)
f := Feed{Url: r.FormValue("feed")}
if err := gn.Get(&f); err != nil {
c.Errorf("%v: %v", err, f.Url)
serveError(w, err)
return
} else if f.IsSubscribed() {
return
}
u := url.Values{}
u.Add("hub.callback", f.PubSubURL())
u.Add("hub.mode", "subscribe")
u.Add("hub.verify", "sync")
fu, _ := url.Parse(f.Url)
fu.Fragment = ""
u.Add("hub.topic", fu.String())
req, err := http.NewRequest("POST", PUBSUBHUBBUB_HUB, strings.NewReader(u.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
cl := urlfetch.Client(c)
resp, err := cl.Do(req)
if err != nil {
c.Errorf("req error: %v", err)
} else if resp.StatusCode != 204 {
c.Errorf("resp: %v - %v", f.Url, resp.Status)
c.Errorf("%s", resp.Body)
} else {
c.Infof("subscribed: %v", f.Url)
}
}
示例12: FeedHistory
func FeedHistory(c mpg.Context, w http.ResponseWriter, r *http.Request) {
cu := user.Current(c)
gn := goon.FromContext(c)
u := User{Id: cu.ID}
uk := gn.Key(&u)
if v := r.FormValue("v"); len(v) == 0 {
q := datastore.NewQuery(gn.Key(&UserOpml{}).Kind()).Ancestor(uk).KeysOnly()
keys, err := gn.GetAll(q, nil)
if err != nil {
serveError(w, err)
return
}
times := make([]string, len(keys))
for i, k := range keys {
times[i] = strconv.FormatInt(k.IntID(), 10)
}
b, _ := json.Marshal(×)
w.Write(b)
} else {
a, _ := strconv.ParseInt(v, 10, 64)
uo := UserOpml{Id: a, Parent: uk}
c.Errorf("k: %v", uo)
if err := gn.Get(&uo); err != nil {
serveError(w, err)
return
}
downloadOpml(w, uo.opml(), cu.Email)
}
}
示例13: Main
func Main(c mpg.Context, w http.ResponseWriter, r *http.Request) {
if err := templates.ExecuteTemplate(w, "base.html", includes(c, w, r)); err != nil {
c.Errorf("%v", err)
serveError(w, err)
return
}
}
示例14: 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)
}
示例15: ImportOpml
func ImportOpml(c mpg.Context, w http.ResponseWriter, r *http.Request) {
cu := user.Current(c)
gn := goon.FromContext(c)
u := User{Id: cu.ID}
if err := gn.Get(&u); err != nil {
serveError(w, err)
return
}
backupOPML(c)
if file, _, err := r.FormFile("file"); err == nil {
if fdata, err := ioutil.ReadAll(file); err == nil {
buf := bytes.NewReader(fdata)
// attempt to extract from google reader takeout zip
if zb, zerr := zip.NewReader(buf, int64(len(fdata))); zerr == nil {
for _, f := range zb.File {
if strings.HasSuffix(f.FileHeader.Name, "Reader/subscriptions.xml") {
if rc, rerr := f.Open(); rerr == nil {
if fb, ferr := ioutil.ReadAll(rc); ferr == nil {
fdata = fb
break
}
}
}
}
}
// Preflight the OPML, so we can report any errors.
d := xml.NewDecoder(bytes.NewReader(fdata))
d.CharsetReader = charset.NewReader
d.Strict = false
opml := Opml{}
if err := d.Decode(&opml); err != nil {
serveError(w, err)
c.Errorf("opml error: %v", err.Error())
return
}
var b bytes.Buffer
enc := gob.NewEncoder(&b)
err := enc.Encode(&opml)
if err != nil {
serveError(w, err)
return
}
bk, err := saveFile(c, b.Bytes())
if err != nil {
serveError(w, err)
return
}
task := taskqueue.NewPOSTTask(routeUrl("import-opml-task"), url.Values{
"key": {string(bk)},
"user": {cu.ID},
})
taskqueue.Add(c, task, "import-reader")
}
}
}