appengine.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // NewContext returns a context for an in-flight HTTP request.
  56. // This function is cheap.
  57. func NewContext(req *http.Request) context.Context {
  58. return internal.ReqContext(req)
  59. }
  60. // WithContext returns a copy of the parent context
  61. // and associates it with an in-flight HTTP request.
  62. // This function is cheap.
  63. func WithContext(parent context.Context, req *http.Request) context.Context {
  64. return internal.WithContext(parent, req)
  65. }
  66. // TODO(dsymonds): Add a Call function here? Otherwise other packages can't access internal.Call.
  67. // BlobKey is a key for a blobstore blob.
  68. //
  69. // Conceptually, this type belongs in the blobstore package, but it lives in
  70. // the appengine package to avoid a circular dependency: blobstore depends on
  71. // datastore, and datastore needs to refer to the BlobKey type.
  72. type BlobKey string
  73. // GeoPoint represents a location as latitude/longitude in degrees.
  74. type GeoPoint struct {
  75. Lat, Lng float64
  76. }
  77. // Valid returns whether a GeoPoint is within [-90, 90] latitude and [-180, 180] longitude.
  78. func (g GeoPoint) Valid() bool {
  79. return -90 <= g.Lat && g.Lat <= 90 && -180 <= g.Lng && g.Lng <= 180
  80. }
  81. // APICallFunc defines a function type for handling an API call.
  82. // See WithCallOverride.
  83. type APICallFunc func(ctx context.Context, service, method string, in, out proto.Message) error
  84. // WithAPICallFunc returns a copy of the parent context
  85. // that will cause API calls to invoke f instead of their normal operation.
  86. //
  87. // This is intended for advanced users only.
  88. func WithAPICallFunc(ctx context.Context, f APICallFunc) context.Context {
  89. return internal.WithCallOverride(ctx, internal.CallOverrideFunc(f))
  90. }
  91. // APICall performs an API call.
  92. //
  93. // This is not intended for general use; it is exported for use in conjunction
  94. // with WithAPICallFunc.
  95. func APICall(ctx context.Context, service, method string, in, out proto.Message) error {
  96. return internal.Call(ctx, service, method, in, out)
  97. }