本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
5.2KB

  1. // Copyright 2011 Google Inc. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package appengine
  5. import (
  6. "time"
  7. "golang.org/x/net/context"
  8. "google.golang.org/appengine/internal"
  9. pb "google.golang.org/appengine/internal/app_identity"
  10. modpb "google.golang.org/appengine/internal/modules"
  11. )
  12. // AppID returns the application ID for the current application.
  13. // The string will be a plain application ID (e.g. "appid"), with a
  14. // domain prefix for custom domain deployments (e.g. "example.com:appid").
  15. func AppID(c context.Context) string { return internal.AppID(c) }
  16. // DefaultVersionHostname returns the standard hostname of the default version
  17. // of the current application (e.g. "my-app.appspot.com"). This is suitable for
  18. // use in constructing URLs.
  19. func DefaultVersionHostname(c context.Context) string {
  20. return internal.DefaultVersionHostname(c)
  21. }
  22. // ModuleName returns the module name of the current instance.
  23. func ModuleName(c context.Context) string {
  24. return internal.ModuleName(c)
  25. }
  26. // ModuleHostname returns a hostname of a module instance.
  27. // If module is the empty string, it refers to the module of the current instance.
  28. // If version is empty, it refers to the version of the current instance if valid,
  29. // or the default version of the module of the current instance.
  30. // If instance is empty, ModuleHostname returns the load-balancing hostname.
  31. func ModuleHostname(c context.Context, module, version, instance string) (string, error) {
  32. req := &modpb.GetHostnameRequest{}
  33. if module != "" {
  34. req.Module = &module
  35. }
  36. if version != "" {
  37. req.Version = &version
  38. }
  39. if instance != "" {
  40. req.Instance = &instance
  41. }
  42. res := &modpb.GetHostnameResponse{}
  43. if err := internal.Call(c, "modules", "GetHostname", req, res); err != nil {
  44. return "", err
  45. }
  46. return *res.Hostname, nil
  47. }
  48. // VersionID returns the version ID for the current application.
  49. // It will be of the form "X.Y", where X is specified in app.yaml,
  50. // and Y is a number generated when each version of the app is uploaded.
  51. // It does not include a module name.
  52. func VersionID(c context.Context) string { return internal.VersionID(c) }
  53. // InstanceID returns a mostly-unique identifier for this instance.
  54. func InstanceID() string { return internal.InstanceID() }
  55. // Datacenter returns an identifier for the datacenter that the instance is running in.
  56. func Datacenter(c context.Context) string { return internal.Datacenter(c) }
  57. // ServerSoftware returns the App Engine release version.
  58. // In production, it looks like "Google App Engine/X.Y.Z".
  59. // In the development appserver, it looks like "Development/X.Y".
  60. func ServerSoftware() string { return internal.ServerSoftware() }
  61. // RequestID returns a string that uniquely identifies the request.
  62. func RequestID(c context.Context) string { return internal.RequestID(c) }
  63. // AccessToken generates an OAuth2 access token for the specified scopes on
  64. // behalf of service account of this application. This token will expire after
  65. // the returned time.
  66. func AccessToken(c context.Context, scopes ...string) (token string, expiry time.Time, err error) {
  67. req := &pb.GetAccessTokenRequest{Scope: scopes}
  68. res := &pb.GetAccessTokenResponse{}
  69. err = internal.Call(c, "app_identity_service", "GetAccessToken", req, res)
  70. if err != nil {
  71. return "", time.Time{}, err
  72. }
  73. return res.GetAccessToken(), time.Unix(res.GetExpirationTime(), 0), nil
  74. }
  75. // Certificate represents a public certificate for the app.
  76. type Certificate struct {
  77. KeyName string
  78. Data []byte // PEM-encoded X.509 certificate
  79. }
  80. // PublicCertificates retrieves the public certificates for the app.
  81. // They can be used to verify a signature returned by SignBytes.
  82. func PublicCertificates(c context.Context) ([]Certificate, error) {
  83. req := &pb.GetPublicCertificateForAppRequest{}
  84. res := &pb.GetPublicCertificateForAppResponse{}
  85. if err := internal.Call(c, "app_identity_service", "GetPublicCertificatesForApp", req, res); err != nil {
  86. return nil, err
  87. }
  88. var cs []Certificate
  89. for _, pc := range res.PublicCertificateList {
  90. cs = append(cs, Certificate{
  91. KeyName: pc.GetKeyName(),
  92. Data: []byte(pc.GetX509CertificatePem()),
  93. })
  94. }
  95. return cs, nil
  96. }
  97. // ServiceAccount returns a string representing the service account name, in
  98. // the form of an email address (typically app_id@appspot.gserviceaccount.com).
  99. func ServiceAccount(c context.Context) (string, error) {
  100. req := &pb.GetServiceAccountNameRequest{}
  101. res := &pb.GetServiceAccountNameResponse{}
  102. err := internal.Call(c, "app_identity_service", "GetServiceAccountName", req, res)
  103. if err != nil {
  104. return "", err
  105. }
  106. return res.GetServiceAccountName(), err
  107. }
  108. // SignBytes signs bytes using a private key unique to your application.
  109. func SignBytes(c context.Context, bytes []byte) (keyName string, signature []byte, err error) {
  110. req := &pb.SignForAppRequest{BytesToSign: bytes}
  111. res := &pb.SignForAppResponse{}
  112. if err := internal.Call(c, "app_identity_service", "SignForApp", req, res); err != nil {
  113. return "", nil, err
  114. }
  115. return res.GetKeyName(), res.GetSignatureBytes(), nil
  116. }
  117. func init() {
  118. internal.RegisterErrorCodeMap("app_identity_service", pb.AppIdentityServiceError_ErrorCode_name)
  119. internal.RegisterErrorCodeMap("modules", modpb.ModulesServiceError_ErrorCode_name)
  120. }
上海开阖软件有限公司 沪ICP备12045867号-1