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.

135 lines
4.2 KiB

  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 provides basic functionality for Google App Engine.
  5. //
  6. // For more information on how to write Go apps for Google App Engine, see:
  7. // https://cloud.google.com/appengine/docs/go/
  8. package appengine // import "google.golang.org/appengine"
  9. import (
  10. "net/http"
  11. "github.com/golang/protobuf/proto"
  12. "golang.org/x/net/context"
  13. "google.golang.org/appengine/internal"
  14. )
  15. // The gophers party all night; the rabbits provide the beats.
  16. // Main is the principal entry point for an app running in App Engine.
  17. //
  18. // On App Engine Flexible it installs a trivial health checker if one isn't
  19. // already registered, and starts listening on port 8080 (overridden by the
  20. // $PORT environment variable).
  21. //
  22. // See https://cloud.google.com/appengine/docs/flexible/custom-runtimes#health_check_requests
  23. // for details on how to do your own health checking.
  24. //
  25. // On App Engine Standard it ensures the server has started and is prepared to
  26. // receive requests.
  27. //
  28. // Main never returns.
  29. //
  30. // Main is designed so that the app's main package looks like this:
  31. //
  32. // package main
  33. //
  34. // import (
  35. // "google.golang.org/appengine"
  36. //
  37. // _ "myapp/package0"
  38. // _ "myapp/package1"
  39. // )
  40. //
  41. // func main() {
  42. // appengine.Main()
  43. // }
  44. //
  45. // The "myapp/packageX" packages are expected to register HTTP handlers
  46. // in their init functions.
  47. func Main() {
  48. internal.Main()
  49. }
  50. // IsDevAppServer reports whether the App Engine app is running in the
  51. // development App Server.
  52. func IsDevAppServer() bool {
  53. return internal.IsDevAppServer()
  54. }
  55. // IsStandard reports whether the App Engine app is running in the standard
  56. // environment. This includes both the first generation runtimes (<= Go 1.9)
  57. // and the second generation runtimes (>= Go 1.11).
  58. func IsStandard() bool {
  59. return internal.IsStandard()
  60. }
  61. // IsFlex reports whether the App Engine app is running in the flexible environment.
  62. func IsFlex() bool {
  63. return internal.IsFlex()
  64. }
  65. // IsAppEngine reports whether the App Engine app is running on App Engine, in either
  66. // the standard or flexible environment.
  67. func IsAppEngine() bool {
  68. return internal.IsAppEngine()
  69. }
  70. // IsSecondGen reports whether the App Engine app is running on the second generation
  71. // runtimes (>= Go 1.11).
  72. func IsSecondGen() bool {
  73. return internal.IsSecondGen()
  74. }
  75. // NewContext returns a context for an in-flight HTTP request.
  76. // This function is cheap.
  77. func NewContext(req *http.Request) context.Context {
  78. return internal.ReqContext(req)
  79. }
  80. // WithContext returns a copy of the parent context
  81. // and associates it with an in-flight HTTP request.
  82. // This function is cheap.
  83. func WithContext(parent context.Context, req *http.Request) context.Context {
  84. return internal.WithContext(parent, req)
  85. }
  86. // BlobKey is a key for a blobstore blob.
  87. //
  88. // Conceptually, this type belongs in the blobstore package, but it lives in
  89. // the appengine package to avoid a circular dependency: blobstore depends on
  90. // datastore, and datastore needs to refer to the BlobKey type.
  91. type BlobKey string
  92. // GeoPoint represents a location as latitude/longitude in degrees.
  93. type GeoPoint struct {
  94. Lat, Lng float64
  95. }
  96. // Valid returns whether a GeoPoint is within [-90, 90] latitude and [-180, 180] longitude.
  97. func (g GeoPoint) Valid() bool {
  98. return -90 <= g.Lat && g.Lat <= 90 && -180 <= g.Lng && g.Lng <= 180
  99. }
  100. // APICallFunc defines a function type for handling an API call.
  101. // See WithCallOverride.
  102. type APICallFunc func(ctx context.Context, service, method string, in, out proto.Message) error
  103. // WithAPICallFunc returns a copy of the parent context
  104. // that will cause API calls to invoke f instead of their normal operation.
  105. //
  106. // This is intended for advanced users only.
  107. func WithAPICallFunc(ctx context.Context, f APICallFunc) context.Context {
  108. return internal.WithCallOverride(ctx, internal.CallOverrideFunc(f))
  109. }
  110. // APICall performs an API call.
  111. //
  112. // This is not intended for general use; it is exported for use in conjunction
  113. // with WithAPICallFunc.
  114. func APICall(ctx context.Context, service, method string, in, out proto.Message) error {
  115. return internal.Call(ctx, service, method, in, out)
  116. }