appengine.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. // IsDevAppServer reports whether the App Engine app is running in the
  16. // development App Server.
  17. func IsDevAppServer() bool {
  18. return internal.IsDevAppServer()
  19. }
  20. // NewContext returns a context for an in-flight HTTP request.
  21. // This function is cheap.
  22. func NewContext(req *http.Request) context.Context {
  23. return WithContext(context.Background(), req)
  24. }
  25. // WithContext returns a copy of the parent context
  26. // and associates it with an in-flight HTTP request.
  27. // This function is cheap.
  28. func WithContext(parent context.Context, req *http.Request) context.Context {
  29. return internal.WithContext(parent, req)
  30. }
  31. // TODO(dsymonds): Add a Call function here? Otherwise other packages can't access internal.Call.
  32. // BlobKey is a key for a blobstore blob.
  33. //
  34. // Conceptually, this type belongs in the blobstore package, but it lives in
  35. // the appengine package to avoid a circular dependency: blobstore depends on
  36. // datastore, and datastore needs to refer to the BlobKey type.
  37. type BlobKey string
  38. // GeoPoint represents a location as latitude/longitude in degrees.
  39. type GeoPoint struct {
  40. Lat, Lng float64
  41. }
  42. // Valid returns whether a GeoPoint is within [-90, 90] latitude and [-180, 180] longitude.
  43. func (g GeoPoint) Valid() bool {
  44. return -90 <= g.Lat && g.Lat <= 90 && -180 <= g.Lng && g.Lng <= 180
  45. }
  46. // APICallFunc defines a function type for handling an API call.
  47. // See WithCallOverride.
  48. type APICallFunc func(ctx context.Context, service, method string, in, out proto.Message) error
  49. // WithCallOverride returns a copy of the parent context
  50. // that will cause API calls to invoke f instead of their normal operation.
  51. //
  52. // This is intended for advanced users only.
  53. func WithAPICallFunc(ctx context.Context, f APICallFunc) context.Context {
  54. return internal.WithCallOverride(ctx, internal.CallOverrideFunc(f))
  55. }
  56. // APICall performs an API call.
  57. //
  58. // This is not intended for general use; it is exported for use in conjunction
  59. // with WithAPICallFunc.
  60. func APICall(ctx context.Context, service, method string, in, out proto.Message) error {
  61. return internal.Call(ctx, service, method, in, out)
  62. }