appengine.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. "context"
  11. "net/http"
  12. "github.com/golang/protobuf/proto"
  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. // Middleware wraps an http handler so that it can make GAE API calls
  51. var Middleware func(http.Handler) http.Handler = internal.Middleware
  52. // IsDevAppServer reports whether the App Engine app is running in the
  53. // development App Server.
  54. func IsDevAppServer() bool {
  55. return internal.IsDevAppServer()
  56. }
  57. // IsStandard reports whether the App Engine app is running in the standard
  58. // environment. This includes both the first generation runtimes (<= Go 1.9)
  59. // and the second generation runtimes (>= Go 1.11).
  60. func IsStandard() bool {
  61. return internal.IsStandard()
  62. }
  63. // IsFlex reports whether the App Engine app is running in the flexible environment.
  64. func IsFlex() bool {
  65. return internal.IsFlex()
  66. }
  67. // IsAppEngine reports whether the App Engine app is running on App Engine, in either
  68. // the standard or flexible environment.
  69. func IsAppEngine() bool {
  70. return internal.IsAppEngine()
  71. }
  72. // IsSecondGen reports whether the App Engine app is running on the second generation
  73. // runtimes (>= Go 1.11).
  74. func IsSecondGen() bool {
  75. return internal.IsSecondGen()
  76. }
  77. // NewContext returns a context for an in-flight HTTP request.
  78. // This function is cheap.
  79. func NewContext(req *http.Request) context.Context {
  80. return internal.ReqContext(req)
  81. }
  82. // WithContext returns a copy of the parent context
  83. // and associates it with an in-flight HTTP request.
  84. // This function is cheap.
  85. func WithContext(parent context.Context, req *http.Request) context.Context {
  86. return internal.WithContext(parent, req)
  87. }
  88. // BlobKey is a key for a blobstore blob.
  89. //
  90. // Conceptually, this type belongs in the blobstore package, but it lives in
  91. // the appengine package to avoid a circular dependency: blobstore depends on
  92. // datastore, and datastore needs to refer to the BlobKey type.
  93. type BlobKey string
  94. // GeoPoint represents a location as latitude/longitude in degrees.
  95. type GeoPoint struct {
  96. Lat, Lng float64
  97. }
  98. // Valid returns whether a GeoPoint is within [-90, 90] latitude and [-180, 180] longitude.
  99. func (g GeoPoint) Valid() bool {
  100. return -90 <= g.Lat && g.Lat <= 90 && -180 <= g.Lng && g.Lng <= 180
  101. }
  102. // APICallFunc defines a function type for handling an API call.
  103. // See WithCallOverride.
  104. type APICallFunc func(ctx context.Context, service, method string, in, out proto.Message) error
  105. // WithAPICallFunc returns a copy of the parent context
  106. // that will cause API calls to invoke f instead of their normal operation.
  107. //
  108. // This is intended for advanced users only.
  109. func WithAPICallFunc(ctx context.Context, f APICallFunc) context.Context {
  110. return internal.WithCallOverride(ctx, internal.CallOverrideFunc(f))
  111. }
  112. // APICall performs an API call.
  113. //
  114. // This is not intended for general use; it is exported for use in conjunction
  115. // with WithAPICallFunc.
  116. func APICall(ctx context.Context, service, method string, in, out proto.Message) error {
  117. return internal.Call(ctx, service, method, in, out)
  118. }