本文整理汇总了Golang中google/golang.org/appengine.NewContext函数的典型用法代码示例。如果您正苦于以下问题:Golang NewContext函数的具体用法?Golang NewContext怎么用?Golang NewContext使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewContext函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: handlePut
func handlePut(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
hc := &http.Client{
Transport: &oauth2.Transport{
Source: google.AppEngineTokenSource(ctx, storage.ScopeFullControl),
Base: &urlfetch.Transport{Context: ctx},
},
}
cctx := cloud.NewContext(appengine.AppID(ctx), hc)
rdr, hdr, err := req.FormFile("form-upload-file")
if err != nil {
http.Error(res, "ERROR RECEIVING FILE: "+err.Error(), 500)
return
}
writer := storage.NewWriter(cctx, bucketName, hdr.Filename)
io.Copy(writer, rdr)
err = writer.Close()
if err != nil {
http.Error(res, "ERROR WRITING TO BUCKET: "+err.Error(), 500)
return
}
}
示例2: index
func index(res http.ResponseWriter, req *http.Request) {
html := ``
//Build Cookie
id, _ := uuid.NewV4()
cookie := &http.Cookie{
Name: "my-cookie",
Value: id.String(),
HttpOnly: true,
}
http.SetCookie(res, cookie)
//Store memcache
ctx := appengine.NewContext(req)
item := memcache.Item{
Key: id.String(),
Value: []byte("Matthew"),
}
memcache.Set(ctx, &item)
//Get uuid from cookie
cookieGet, _ := req.Cookie("my-cookie")
if cookieGet != nil {
html += `UUID from cookie: ` + cookieGet.Value + `<br>`
}
//Get uuid and value from memcache
ctx = appengine.NewContext(req)
item0, _ := memcache.Get(ctx, id.String())
if item0 != nil {
html += `Value from Memcache using uuid: ` + string(item0.Value) + `<br>`
}
res.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprint(res, html)
}
示例3: APIHandler
// APIHandler for addrshrt API
func APIHandler(w http.ResponseWriter, r *http.Request) {
apiRequest := models.ParseAPIRequestURL(*r.URL)
// method router
switch apiRequest.Method {
case "create":
// create our short URL
base64encoded, _ := url.QueryUnescape(apiRequest.Query)
shortURL := models.DecodeAndShortenURL(string(base64encoded))
// store it
c := appengine.NewContext(r)
err := storeShortURL(c, shortURL)
if err != nil {
fmt.Fprint(w, err.Error())
}
// respond with the UUID
w.Header().Add("Content-Type", "text/plain")
fmt.Fprintf(w, shortURL.ShortURL)
return
case "read":
c := appengine.NewContext(r)
surl, err := retrieveShortURL(c, apiRequest.Query)
if err != nil {
fmt.Fprint(w, err.Error())
}
w.Header().Add("Content-Type", "text/plain")
fmt.Fprintf(w, surl.OriginalURL)
return
}
}
示例4: FetchRatesJob
// FetchRatesJob - method get rates data from open suorce
func FetchRatesJob(w http.ResponseWriter, r *http.Request) {
_, success, err := updateFromSource2ForAppEngine(r)
if err != nil {
if r != nil {
ctx := appengine.NewContext(r)
logAE.Errorf(ctx, "FetchRatesJob Fatal: %v", err)
w.Header().Set("Cache-Control", "no-cache")
http.Error(w, fmt.Sprintf("FetchRatesJob Fatal: %v", err), http.StatusInternalServerError)
} else {
log.Fatal(err)
}
return
}
if success == false {
if r != nil {
ctx := appengine.NewContext(r)
logAE.Errorf(ctx, "FetchRatesJob Error")
w.Header().Set("Cache-Control", "no-cache")
http.Error(w, "FetchRatesJob Error", http.StatusInternalServerError)
}
return
}
executeDomainLogic(w, r)
}
示例5: init
// init is called before the application starts.
func init() {
m := martini.Classic()
m.Use(render.Renderer())
m.Use(func(res http.ResponseWriter, req *http.Request) {
authorization := &spark.Authorization{AccessToken: os.Getenv("SPARK_TOKEN")}
spark.InitClient(authorization)
ctx := appengine.NewContext(req)
spark.SetHttpClient(urlfetch.Client(ctx), ctx)
})
m.Post("/spark", binding.Json(SparkEvent{}), func(sparkEvent SparkEvent, res http.ResponseWriter, req *http.Request, r render.Render) {
ctx := appengine.NewContext(req)
client := urlfetch.Client(ctx)
message := spark.Message{ID: sparkEvent.Id}
message.Get()
log.Infof(ctx, message.Text)
if strings.HasPrefix(message.Text, "/") {
s := strings.Split(sparkEvent.Text, " ")
command := s[0]
log.Infof(ctx, "command = %s", command)
if command == "/routes" {
resp, _ := client.Get("http://galwaybus.herokuapp.com/routes.json")
defer resp.Body.Close()
contents, _ := ioutil.ReadAll(resp.Body)
log.Infof(ctx, "body = %s\n", contents)
var routeMap map[string]BusRoute
json.Unmarshal([]byte(contents), &routeMap)
text := "Routes:\n\n"
for _, route := range routeMap {
text = text + strconv.Itoa(route.Id) + " " + route.LongName + "\n"
}
message := spark.Message{
RoomID: sparkEvent.RoomId,
Text: text,
}
message.Post()
}
}
})
http.Handle("/", m)
}
示例6: deleteAll
func deleteAll(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {
lg, _ := loghttp.BuffLoggerUniversal(w, r)
err := r.ParseForm()
lg(err)
wpf(w, tplx.ExecTplHelper(tplx.Head, map[string]interface{}{"HtmlTitle": "Delete all filesystem data"}))
defer wpf(w, tplx.Foot)
confirm := r.FormValue("confirm")
if confirm != "yes" {
wpf(w, "All dsfs contents are deletes. All memfs contents are deleted<br>\n")
wpf(w, "Put a get param into the URL ?confirm - and set it to 'yes'<br>\n")
wpf(w, "Put a get param 'mountname' into url; i.e. mountname=mntftch<br>\n")
return
}
wpf(w, "<pre>\n")
defer wpf(w, "\n</pre>")
//
//
fs := dsfs.New(
dsfs.AeContext(appengine.NewContext(r)),
)
mountName := r.FormValue("mountname")
if mountName != "" {
wpf(w, "mountame = "+mountName+"\n")
fs = dsfs.New(
dsfs.AeContext(appengine.NewContext(r)),
dsfs.MountName(mountName),
)
}
wpf(w, "dsfs:\n")
msg, err := fs.DeleteAll()
if err != nil {
wpf(w, "err during delete %v\n", err)
}
wpf(w, msg)
memMapFileSys = memfs.New()
wpf(w, "\n")
wpf(w, "memMapFs new")
// cleanup must be manual
osFileSys = osfs.New()
}
示例7: DeleteSubtree
func DeleteSubtree(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {
lg, lge := loghttp.Logger(w, r)
err := r.ParseForm()
lge(err)
wpf(w, tplx.ExecTplHelper(tplx.Head, map[string]interface{}{"HtmlTitle": "Delete Subtree for curr FS"}))
defer wpf(w, tplx.Foot)
if r.Method == "POST" {
wpf(w, "<pre>\n")
defer wpf(w, "\n</pre>")
mountPoint := dsfs.MountPointLast()
if len(r.FormValue("mountname")) > 0 {
mountPoint = r.FormValue("mountname")
}
lg("mount point is %v", mountPoint)
pathPrefix := "impossible-value"
if len(r.FormValue("pathprefix")) > 0 {
pathPrefix = r.FormValue("pathprefix")
}
lg("pathprefix is %v", pathPrefix)
fs := getFS(appengine.NewContext(r), mountPoint)
lg("created fs %v-%v ", fs.Name(), fs.String())
lg("removing %q - and its subtree ...", pathPrefix)
err := fs.RemoveAll(pathPrefix)
lge(err)
errMc := memcache.Flush(appengine.NewContext(r))
lge(errMc)
if err == nil && errMc == nil {
lg("success")
}
} else {
tData := map[string]string{"Url": UriDeleteSubtree}
err := tplBase.ExecuteTemplate(w, "tplName01", tData)
lge(err)
}
}
示例8: addHandler
func addHandler(w http.ResponseWriter, r *http.Request) {
t := template.Must(template.ParseGlob("template/*")) // add sub-templates in /template
if r.Method == "GET" { // get method display form
t.ParseFiles("add.html") // parse add.html if 'method' = 'get'
ctx := appengine.NewContext(r) // get ctx
uploadURL, err := blobstore.UploadURL(ctx, "/add", nil) // get uploadurl for blobstore
if err != nil {
return
}
actionURL := map[string]string{"BlobActionURL": uploadURL.String()} // provide blobuploadurl to action field of the form
t.ExecuteTemplate(w, "base", actionURL)
}
if r.Method == "POST" {
ctx := appengine.NewContext(r)
r.ParseForm()
blobs, others, err := blobstore.ParseUpload(r) // get upload blob info
if err != nil {
w.Write([]byte(err.Error())) // error
return
}
files := blobs["img"] // name="img" in the html form
title := others.Get("title") // '' title
password := others.Get("password") // '' password
description := others.Get("description") // '' description
var blobKeys []string
var imgSrcs []string
for _, file := range files {
imgSrc := "/serve/?blobKey=" + string(file.BlobKey) // create imgsrc url from blobkey
imgSrcs = append(imgSrcs, imgSrc) //multiple images in singe post
blobKeys = append(blobKeys, string(file.BlobKey)) // also save blobkey in case for use.
}
var post m.Post // creating post and fill the fields.
post.Title = title
post.Password = password
post.Description = description
post.BlobKeys = blobKeys
post.Time = time.Now()
post.ImageSrc = imgSrcs
posts := []m.DataModel{post} // uploading posts
keys, _ := m.SaveDataModels(ctx, posts) // success?
i := strconv.Itoa(int(keys[0].IntID()))
http.Redirect(w, r, "/main/?id="+i, http.StatusFound) // redirect to /main/?uuid= with uuid
// http.Redirect(w, r, "/error", http.StatusNotFound)
}
}
示例9: logout
func logout(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
ctx := appengine.NewContext(req)
cookie, err := req.Cookie("session")
// cookie is not set
if err != nil {
http.Redirect(res, req, "/", 302)
return
}
// clear memcache
sd := memcache.Item{
Key: cookie.Value,
Value: []byte(""),
Expiration: time.Duration(1 * time.Microsecond),
}
memcache.Set(ctx, &sd)
// clear the cookie
cookie.MaxAge = -1
http.SetCookie(res, cookie)
// redirect
http.Redirect(res, req, "/", 302)
}
示例10: TestCreateSpot
func TestCreateSpot(t *testing.T) {
opt := aetest.Options{AppID: "t2jp-2015", StronglyConsistentDatastore: true}
inst, err := aetest.NewInstance(&opt)
defer inst.Close()
input, err := json.Marshal(Spot{SpotName: "foo", Body: "bar"})
req, err := inst.NewRequest("POST", "/edit/v1/spots", bytes.NewBuffer(input))
if err != nil {
t.Fatalf("Failed to create req: %v", err)
}
loginUser := user.User{Email: "[email protected]", Admin: false, ID: "111111"}
aetest.Login(&loginUser, req)
ctx := appengine.NewContext(req)
res := httptest.NewRecorder()
c := web.C{}
spotCreateHandler(c, res, req)
if res.Code != http.StatusCreated {
t.Fatalf("Fail to request spots create, status code: %v", res.Code)
}
spots := []Spot{}
_, err = datastore.NewQuery("Spot").Order("-UpdatedAt").GetAll(ctx, &spots)
for i := 0; i < len(spots); i++ {
t.Logf("SpotCode:%v", spots[i].SpotCode)
t.Logf("SpotName:%v", spots[i].SpotName)
}
if spots[0].SpotName != "foo" {
t.Fatalf("not expected value! :%v", spots[0].SpotName)
}
}
示例11: deleteData
func deleteData(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
u := user.Current(ctx)
keyVal := req.FormValue("keyVal")
key, err := datastore.DecodeKey(keyVal)
if err != nil {
http.Error(res, "Invalid data", http.StatusBadRequest)
log.Warningf(ctx, err.Error())
return
}
var l list
err = datastore.Get(ctx, key, &l)
if err != nil {
http.Error(res, "Invalid data", http.StatusBadRequest)
log.Warningf(ctx, err.Error())
return
}
if l.Owner != u.Email {
http.Error(res, "Not authorized to delete this entry", http.StatusUnauthorized)
log.Warningf(ctx, err.Error())
return
}
err = datastore.Delete(ctx, key)
if err != nil {
http.Error(res, "Server Error", http.StatusInternalServerError)
log.Errorf(ctx, err.Error())
return
}
}
示例12: submitAddHandler
func submitAddHandler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
if err := r.ParseForm(); err != nil {
serveErr(ctx, err, w)
return
}
success, err := recaptchaCheck(ctx, r.FormValue("g-recaptcha-response"), r.RemoteAddr)
if err != nil {
serveErr(ctx, err, w)
return
}
if !success {
log.Warningf(ctx, "reCAPTCHA check failed")
failedTmpl.ExecuteTemplate(w, "base", nil)
return
}
sub := Submission{
URL: template.URL(strings.TrimSpace(r.FormValue("url"))),
Submitted: time.Now(),
}
if _, err := datastore.Put(ctx, datastore.NewIncompleteKey(ctx, "Submission", nil), &sub); err != nil {
serveErr(ctx, err, w)
return
}
thanksTmpl.ExecuteTemplate(w, "base", nil)
}
示例13: createSession
func createSession(res http.ResponseWriter, req *http.Request, user User) {
ctx := appengine.NewContext(req)
// SET COOKIE
id, _ := uuid.NewV4()
cookie := &http.Cookie{
Name: "session",
Value: id.String(),
Path: "/",
// twenty minute session:
MaxAge: 60 * 20,
// UNCOMMENT WHEN DEPLOYED:
// Secure: true,
// HttpOnly: true,
}
http.SetCookie(res, cookie)
// SET MEMCACHE session data (sd)
json, err := json.Marshal(user)
if err != nil {
log.Errorf(ctx, "error marshalling during user creation: %v", err)
http.Error(res, err.Error(), 500)
return
}
sd := memcache.Item{
Key: id.String(),
Value: json,
}
memcache.Set(ctx, &sd)
}
示例14: HandleGoalDelete
func HandleGoalDelete(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
params := mux.Vars(r)
goalName, exists := params["goal"]
if !exists {
w.WriteHeader(http.StatusInternalServerError)
// add error notesmessage that "goal parameter is missing"
return
}
goal := model.Goal{}
goal.Name = goalName
err := goal.Delete(c)
if err == types.ErrorNoMatch {
http.Error(w, err.Error(), http.StatusOK)
return
} else if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
示例15: HandleGoalGet
func HandleGoalGet(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
params := mux.Vars(r)
goalName, exists := params["goal"]
if !exists {
http.Error(w, "Goal parameter is missing in URI", http.StatusBadRequest)
return
}
goal := model.Goal{}
goal.Name = goalName
// if given goal is not found, return appropriate error
if err := goal.Get(c); err == types.ErrorNoMatch {
http.Error(w, err.Error(), http.StatusBadRequest)
return
} else if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := json.NewEncoder(w).Encode(goal); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//w.WriteHeader(http.StatusOK) // removed because of "multiple response.WriteHeader calls" error
}