appengine.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. // TODO(dsymonds): Add a Call function here? Otherwise other packages can't access internal.Call.
  87. // BlobKey is a key for a blobstore blob.
  88. //
  89. // Conceptually, this type belongs in the blobstore package, but it lives in
  90. // the appengine package to avoid a circular dependency: blobstore depends on
  91. // datastore, and datastore needs to refer to the BlobKey type.
  92. type BlobKey string
  93. // GeoPoint represents a location as latitude/longitude in degrees.
  94. type GeoPoint struct {
  95. Lat, Lng float64
  96. }
  97. // Valid returns whether a GeoPoint is within [-90, 90] latitude and [-180, 180] longitude.
  98. func (g GeoPoint) Valid() bool {
  99. return -90 <= g.Lat && g.Lat <= 90 && -180 <= g.Lng && g.Lng <= 180
  100. }
  101. // APICallFunc defines a function type for handling an API call.
  102. // See WithCallOverride.
  103. type APICallFunc func(ctx context.Context, service, method string, in, out proto.Message) error
  104. // WithAPICallFunc returns a copy of the parent context
  105. // that will cause API calls to invoke f instead of their normal operation.
  106. //
  107. // This is intended for advanced users only.
  108. func WithAPICallFunc(ctx context.Context, f APICallFunc) context.Context {
  109. return internal.WithCallOverride(ctx, internal.CallOverrideFunc(f))
  110. }
  111. // APICall performs an API call.
  112. //
  113. // This is not intended for general use; it is exported for use in conjunction
  114. // with WithAPICallFunc.
  115. func APICall(ctx context.Context, service, method string, in, out proto.Message) error {
  116. return internal.Call(ctx, service, method, in, out)
  117. }