我正在学习 golang 和谷歌应用引擎数据存储。
我计划调用一个简单的 rest api 来保存和检索数据存储中的数据。
我遵循了一些 hello world 教程和官方入门指南。最后我想到的是这个。
`
package hello
import (
"fmt"
"log"
"net/http"
"encoding/json"
"cloud.google.com/go/datastore"
"golang.org/x/net/context"
"github.com/gorilla/mux"
"time"
)
type Task struct {
Description string `datastore:"description"`
Created time.Time `datastore:"created"`
id int64
}
func saveData(w http.ResponseWriter, r *http.Request){
fmt.Println("Endpoint Hit: saveData")
input := r.URL.Query().Get("input")
fmt.Println(input)
ctx := context.Background()
projectID := "api-project-426361742627"
client, err := datastore.NewClient(ctx, projectID)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
kind := "Task"
task := Task{
Description: input,
Created: time.Now(),
}
taskKey := datastore.IncompleteKey(kind, nil)
if _, err := client.Put(ctx, taskKey, &task); err != nil {
log.Fatalf("Failed to save task: %v", err)
}
fmt.Printf("Saved %v: %v\n", taskKey, task.Description)
json.NewEncoder(w).Encode(task)
}
func returnAll(w http.ResponseWriter, r *http.Request){
fmt.Println("Endpoint Hit: returnAll")
ctx := context.Background()
projectID := "api-project-426361742627"
client, err := datastore.NewClient(ctx, projectID)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
query := datastore.NewQuery("Task").Order("description")
var tasks []*Task
keys, err := client.GetAll(ctx, query, &tasks)
for i, key := range keys {
tasks[i].id = key.ID
}
// for _, t := range tasks {
// // fmt.Println( t.id, t.Description)
// }
json.NewEncoder(w).Encode(tasks)
}
func homePage(w http.ResponseWriter, r *http.Request){
fmt.Fprintf(w, "Welcome to the HomePage!")
fmt.Println("Endpoint Hit: homePage")
}
func handleRequests() {
myRouter := mux.NewRouter()
myRouter.HandleFunc("/", homePage)
myRouter.HandleFunc("/save", saveData)
myRouter.HandleFunc("/retrieve", returnAll)
http.Handle("/", myRouter)
}
func init() {
handleRequests()
}
`
现在我面临的问题是在这一行
客户端,错误:= datastore.NewClient(ctx,projectID)
我收到以下错误。
2017/09/26 07:15:16 http: panic serving 127.0.0.1:59488: not an App Engine context
goroutine 17 [running]:
net/http.(*conn).serve.func1(0xc82021e000)
/home/aditya/google-cloud-sdk/platform/google_appengine/goroot-1.6/src/net/http/server.go:1389 +0xc1
panic(0xa44740, 0xc82018a8c0)
/home/aditya/google-cloud-sdk/platform/google_appengine/goroot-1.6/src/runtime/panic.go:443 +0x4e9
google.golang.org/appengine/internal.fullyQualifiedAppID(0x7f4142e12be0, 0xc82000b4e0, 0x0, 0x0)
/home/aditya/work/src/google.golang.org/appengine/internal/identity_classic.go:54 +0x8b
google.golang.org/appengine/internal.FullyQualifiedAppID(0x7f4142e12be0, 0xc82000b4e0, 0x0, 0x0)
/home/aditya/work/src/google.golang.org/appengine/internal/api_common.go:77 +0xe2
google.golang.org/appengine/internal.AppID(0x7f4142e12be0, 0xc82000b4e0, 0x0, 0x0)
/home/aditya/work/src/google.golang.org/appengine/internal/identity.go:13 +0x37
google.golang.org/appengine.AppID(0x7f4142e12be0, 0xc82000b4e0, 0x0, 0x0)
/home/aditya/work/src/google.golang.org/appengine/identity.go:20 +0x37
golang.org/x/oauth2/google.FindDefaultCredentials(0x7f4142e12be0, 0xc82000b4e0, 0xc820222170, 0x1, 0x1, 0xc820222150, 0x0, 0x0)
/home/aditya/work/src/golang.org/x/oauth2/google/default.go:92 +0x5a8
google.golang.org/api/internal.Creds(0x7f4142e12be0, 0xc82000b4e0, 0xc820248240, 0xc8202452c0, 0x0, 0x0)
/home/aditya/work/src/google.golang.org/api/internal/creds.go:37 +0x173
google.golang.org/api/transport/grpc.Dial(0x7f4142e12be0, 0xc82000b4e0, 0xc820245638, 0x3, 0x3, 0x0, 0x0, 0x0)
/home/aditya/work/src/google.golang.org/api/transport/grpc/dial.go:47 +0x272
cloud.google.com/go/datastore.NewClient(0x7f4142e12be0, 0xc82000b4e0, 0xc0c320, 0x18, 0x0, 0x0, 0x0, 0x9, 0x0, 0x0)
/home/aditya/work/src/cloud.google.com/go/datastore/datastore.go:87 +0x87f
main76346.returnAll(0x7f4142dcb058, 0xc820234000, 0xc82022a000)
hello.go:51 +0x1b8
net/http.HandlerFunc.ServeHTTP(0xcd13d8, 0x7f4142dcb058, 0xc820234000, 0xc82022a000)
/home/aditya/google-cloud-sdk/platform/google_appengine/goroot-1.6/src/net/http/server.go:1618 +0x3a
github.com/gorilla/mux.(*Router).ServeHTTP(0xc820052660, 0x7f4142dcb058, 0xc820234000, 0xc82022a000)
/home/aditya/work/src/github.com/gorilla/mux/mux.go:133 +0x37c
net/http.(*ServeMux).ServeHTTP(0xc82000eab0, 0x7f4142dcb058, 0xc820234000, 0xc82022a000)
/home/aditya/google-cloud-sdk/platform/google_appengine/goroot-1.6/src/net/http/server.go:1910 +0x17d
appengine_internal.handleFilteredHTTP(0x7f4142dcb058, 0xc820234000, 0xc82022a000)
/home/aditya/google-cloud-sdk/platform/google_appengine/goroot-1.6/src/appengine_internal/api_dev.go:102 +0x409
net/http.HandlerFunc.ServeHTTP(0xccfe48, 0x7f4142dcb058, 0xc820234000, 0xc82022a000)
/home/aditya/google-cloud-sdk/platform/google_appengine/goroot-1.6/src/net/http/server.go:1618 +0x3a
net/http.serverHandler.ServeHTTP(0xc820078500, 0x7f4142dcb058, 0xc820234000, 0xc82022a000)
/home/aditya/google-cloud-sdk/platform/google_appengine/goroot-1.6/src/net/http/server.go:2081 +0x19e
net/http.(*conn).serve(0xc82021e000)
/home/aditya/google-cloud-sdk/platform/google_appengine/goroot-1.6/src/net/http/server.go:1472 +0xf2e
created by net/http.(*Server).Serve
/home/aditya/google-cloud-sdk/platform/google_appengine/goroot-1.6/src/net/http/server.go:2137 +0x44e
INFO 2017-09-26 07:15:16,800 module.py:821] default: "GET /retrieve HTTP/1.1" 500 3349
我不确定如何解决这个问题。我尝试使用
gcloud auth application-default login
授予对本地数据存储的访问权限,但它不工作。每当我在初始化数据存储新客户端时尝试保存数据或返回所有条目时,我都会遇到这个问题。
最佳答案
更新您的处理程序以使用从应用的处理程序请求派生的上下文而不是 context.Background()
func saveData(w http.ResponseWriter, r *http.Request){
// ..
fmt.Println(input)
ctx := appengine.NewContext(r)
projectID := "api-project-426361742627"
// ...
}
请记住,上下文也可以用作不透明的值容器,应用引擎环境会大量使用此功能。
包裹appengine docs .
关于google-app-engine - 无法存储在数据存储 gcloud 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46420331/
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)
我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie