如何在Go AppEngine应用程序

时间:2016-08-14 12:17:48

标签: google-app-engine ssl go

我正在编写一个go appengine应用,需要从Stripe中获取一个页面。

基本上我使用官方Stripe API附带的these instructions。但是,当我使用dev_appserver.py运行时,我得到:

2016/08/14 12:03:15 Requesting POST api.stripe.com/v1/customers
2016/08/14 12:03:18 Error encountered from Stripe: {"type":"invalid_request_error","message":"Stripe no longer supports API requests made with TLS 1.0. Please initiate HTTPS connections with TLS 1.2 or later. You can learn more about this at https://stripe.com/blog/upgrading-tls.","request_id":"req_90O6reF1Mwi9yZ","status":401}

我发现Python AppEngine应用程序可以指定要在我的app.yaml中使用的SSL库(请参阅SSL support)。但是,如果我在app.yaml文件中添加libraries部分,我会得到:

$ (go_appengine/dev_appserver.py app)
Traceback (most recent call last):
  File "go_appengine/dev_appserver.py", line 89, in <module>
    _run_file(__file__, globals())
  File "go_appengine/dev_appserver.py", line 85, in _run_file
    execfile(_PATHS.script_file(script_name), globals_)
  File "/Users/kchodorow/gitroot/tt/go_appengine/google/appengine/tools/devappserver2/devappserver2.py", line 1040, in <module>
    main()
  File "/Users/kchodorow/gitroot/tt/go_appengine/google/appengine/tools/devappserver2/devappserver2.py", line 1033, in main
    dev_server.start(options)
  File "/Users/kchodorow/gitroot/tt/go_appengine/google/appengine/tools/devappserver2/devappserver2.py", line 758, in start
    options.config_paths, options.app_id)
  File "/Users/kchodorow/gitroot/tt/go_appengine/google/appengine/tools/devappserver2/application_configuration.py", line 831, in __init__
    module_configuration = ModuleConfiguration(config_path, app_id)
  File "/Users/kchodorow/gitroot/tt/go_appengine/google/appengine/tools/devappserver2/application_configuration.py", line 127, in __init__
    self._config_path)
  File "/Users/kchodorow/gitroot/tt/go_appengine/google/appengine/tools/devappserver2/application_configuration.py", line 424, in _parse_configuration
    config, files = appinfo_includes.ParseAndReturnIncludePaths(f)
  File "/Users/kchodorow/gitroot/tt/go_appengine/google/appengine/api/appinfo_includes.py", line 82, in ParseAndReturnIncludePaths
    appyaml = appinfo.LoadSingleAppInfo(appinfo_file)
  File "/Users/kchodorow/gitroot/tt/go_appengine/google/appengine/api/appinfo.py", line 2191, in LoadSingleAppInfo
    listener.Parse(app_info)
  File "/Users/kchodorow/gitroot/tt/go_appengine/google/appengine/api/yaml_listener.py", line 227, in Parse
    self._HandleEvents(self._GenerateEventParameters(stream, loader_class))
  File "/Users/kchodorow/gitroot/tt/go_appengine/google/appengine/api/yaml_listener.py", line 178, in _HandleEvents
    raise yaml_errors.EventError(e, event_object)
google.appengine.api.yaml_errors.EventError: libraries entries are only supported by the "python27" runtime
  in "app/app.yaml", line 25, column 1

这是有道理的,因为我没有使用Python。我真的需要一种方法来为Go设置它。

我的app.yaml文件如下:

application: app-name
version: alpha-001
runtime: go
api_version: go1

handlers:
...

runtime更改为python27可以摆脱库错误,但显然我的go代码无效。

有关启用TLS 1.2的任何想法,包括dev appserver和production?

2 个答案:

答案 0 :(得分:4)

来自Go net / http文档:

  

要控制代理,TLS配置,保持活动,压缩和其他设置,请创建传输:

tr := &http.Transport{
    TLSClientConfig:    &tls.Config{...},
    DisableCompression: true,
}
client := &http.Client{Transport: tr}

从它出现的appengine documentation开始,您可以像往常一样使用net / http包进行一些调整。

答案 1 :(得分:0)

您应该创建并设置具有TLS配置并使用AppEngine服务的新传输。可以通过将默认拨号功能替换为AppEngine SDK(带上下文)定义的功能来完成。

// NewClient sets up an HTTP/2 client for a certificate and context
func NewClient(ctx context.Context) (*http.Client, error) {
    config := &tls.Config{}
    transport := &http.Transport{
        TLSClientConfig: config,
        Dial: func(network, addr string) (net.Conn, error) {
            // this uses the appengine service to create the actual client
            return socket.Dial(ctx, network, addr)
        },
    }
    return &http.Client{Transport: transport}, nil
}