appengine

package module
Version: v1.6.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2020 License: Apache-2.0 Imports: 9 Imported by: 6,009

README

Go App Engine packages

Build Status

This repository supports the Go runtime on App Engine standard. It provides APIs for interacting with App Engine services. Its canonical import path is google.golang.org/appengine.

See https://cloud.google.com/appengine/docs/go/ for more information.

File issue reports and feature requests on the GitHub's issue tracker.

Upgrading an App Engine app to the flexible environment

This package does not work on App Engine flexible.

There are many differences between the App Engine standard environment and the flexible environment.

See the documentation on upgrading to the flexible environment.

Directory structure

The top level directory of this repository is the appengine package. It contains the basic APIs (e.g. appengine.NewContext) that apply across APIs. Specific API packages are in subdirectories (e.g. datastore).

There is an internal subdirectory that contains service protocol buffers, plus packages required for connectivity to make API calls. App Engine apps should not directly import any package under internal.

Updating from legacy (import "appengine") packages

If you're currently using the bare appengine packages (that is, not these ones, imported via google.golang.org/appengine), then you can use the aefix tool to help automate an upgrade to these packages.

Run go get google.golang.org/appengine/cmd/aefix to install it.

1. Update import paths

The import paths for App Engine packages are now fully qualified, based at google.golang.org/appengine. You will need to update your code to use import paths starting with that; for instance, code importing appengine/datastore will now need to import google.golang.org/appengine/datastore.

2. Update code using deprecated, removed or modified APIs

Most App Engine services are available with exactly the same API. A few APIs were cleaned up, and there are some differences:

  • appengine.Context has been replaced with the Context type from golang.org/x/net/context.
  • Logging methods that were on appengine.Context are now functions in google.golang.org/appengine/log.
  • appengine.Timeout has been removed. Use context.WithTimeout instead.
  • appengine.Datacenter now takes a context.Context argument.
  • datastore.PropertyLoadSaver has been simplified to use slices in place of channels.
  • delay.Call now returns an error.
  • search.FieldLoadSaver now handles document metadata.
  • urlfetch.Transport no longer has a Deadline field; set a deadline on the context.Context instead.
  • aetest no longer declares its own Context type, and uses the standard one instead.
  • taskqueue.QueueStats no longer takes a maxTasks argument. That argument has been deprecated and unused for a long time.
  • appengine.BackendHostname and appengine.BackendInstance were for the deprecated backends feature. Use appengine.ModuleHostnameand appengine.ModuleName instead.
  • Most of appengine/file and parts of appengine/blobstore are deprecated. Use Google Cloud Storage if the feature you require is not present in the new blobstore package.
  • appengine/socket is not required on App Engine flexible environment / Managed VMs. Use the standard net package instead.

Key Encode/Decode compatibiltiy to help with datastore library migrations

Key compatibility updates have been added to help customers transition from google.golang.org/appengine/datastore to cloud.google.com/go/datastore. The EnableKeyConversion enables automatic conversion from a key encoded with cloud.google.com/go/datastore to google.golang.org/appengine/datastore key type.

Enabling key conversion

Enable key conversion by calling EnableKeyConversion(ctx) in the /_ah/start handler for basic and manual scaling or any handler in automatic scaling.

1. Basic or manual scaling

This start handler will enable key conversion for all handlers in the service.

http.HandleFunc("/_ah/start", func(w http.ResponseWriter, r *http.Request) {
    datastore.EnableKeyConversion(appengine.NewContext(r))
})
2. Automatic scaling

/_ah/start is not supported for automatic scaling and /_ah/warmup is not guaranteed to run, so you must call datastore.EnableKeyConversion(appengine.NewContext(r)) before you use code that needs key conversion.

You may want to add this to each of your handlers, or introduce middleware where it's called. EnableKeyConversion is safe for concurrent use. Any call to it after the first is ignored.

Documentation

Overview

Package appengine provides basic functionality for Google App Engine.

For more information on how to write Go apps for Google App Engine, see: https://cloud.google.com/appengine/docs/go/

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func APICall

func APICall(ctx context.Context, service, method string, in, out proto.Message) error

APICall performs an API call.

This is not intended for general use; it is exported for use in conjunction with WithAPICallFunc.

func AccessToken

func AccessToken(c context.Context, scopes ...string) (token string, expiry time.Time, err error)

AccessToken generates an OAuth2 access token for the specified scopes on behalf of service account of this application. This token will expire after the returned time.

func AppID

func AppID(c context.Context) string

AppID returns the application ID for the current application. The string will be a plain application ID (e.g. "appid"), with a domain prefix for custom domain deployments (e.g. "example.com:appid").

func BackgroundContext

func BackgroundContext() context.Context

BackgroundContext returns a context not associated with a request. This should only be used when not servicing a request. This only works in App Engine "flexible environment".

func Datacenter

func Datacenter(c context.Context) string

Datacenter returns an identifier for the datacenter that the instance is running in.

func DefaultVersionHostname

func DefaultVersionHostname(c context.Context) string

DefaultVersionHostname returns the standard hostname of the default version of the current application (e.g. "my-app.appspot.com"). This is suitable for use in constructing URLs.

func InstanceID

func InstanceID() string

InstanceID returns a mostly-unique identifier for this instance.

func IsAppEngine

func IsAppEngine() bool

IsAppEngine reports whether the App Engine app is running on App Engine, in either the standard or flexible environment.

func IsDevAppServer

func IsDevAppServer() bool

IsDevAppServer reports whether the App Engine app is running in the development App Server.

func IsFlex

func IsFlex() bool

IsFlex reports whether the App Engine app is running in the flexible environment.

func IsOverQuota

func IsOverQuota(err error) bool

IsOverQuota reports whether err represents an API call failure due to insufficient available quota.

func IsSecondGen

func IsSecondGen() bool

IsSecondGen reports whether the App Engine app is running on the second generation runtimes (>= Go 1.11).

func IsStandard

func IsStandard() bool

IsStandard reports whether the App Engine app is running in the standard environment. This includes both the first generation runtimes (<= Go 1.9) and the second generation runtimes (>= Go 1.11).

func IsTimeoutError

func IsTimeoutError(err error) bool

IsTimeoutError reports whether err is a timeout error.

func Main

func Main()

Main is the principal entry point for an app running in App Engine.

On App Engine Flexible it installs a trivial health checker if one isn't already registered, and starts listening on port 8080 (overridden by the $PORT environment variable).

See https://cloud.google.com/appengine/docs/flexible/custom-runtimes#health_check_requests for details on how to do your own health checking.

On App Engine Standard it ensures the server has started and is prepared to receive requests.

Main never returns.

Main is designed so that the app's main package looks like this:

package main

import (
        "google.golang.org/appengine"

        _ "myapp/package0"
        _ "myapp/package1"
)

func main() {
        appengine.Main()
}

The "myapp/packageX" packages are expected to register HTTP handlers in their init functions.

func ModuleHostname

func ModuleHostname(c context.Context, module, version, instance string) (string, error)

ModuleHostname returns a hostname of a module instance. If module is the empty string, it refers to the module of the current instance. If version is empty, it refers to the version of the current instance if valid, or the default version of the module of the current instance. If instance is empty, ModuleHostname returns the load-balancing hostname.

func ModuleName

func ModuleName(c context.Context) string

ModuleName returns the module name of the current instance.

func Namespace

func Namespace(c context.Context, namespace string) (context.Context, error)

Namespace returns a replacement context that operates within the given namespace.

func NewContext

func NewContext(req *http.Request) context.Context

NewContext returns a context for an in-flight HTTP request. This function is cheap.

func RequestID

func RequestID(c context.Context) string

RequestID returns a string that uniquely identifies the request.

func ServerSoftware

func ServerSoftware() string

ServerSoftware returns the App Engine release version. In production, it looks like "Google App Engine/X.Y.Z". In the development appserver, it looks like "Development/X.Y".

func ServiceAccount

func ServiceAccount(c context.Context) (string, error)

ServiceAccount returns a string representing the service account name, in the form of an email address (typically app_id@appspot.gserviceaccount.com).

func SignBytes

func SignBytes(c context.Context, bytes []byte) (keyName string, signature []byte, err error)

SignBytes signs bytes using a private key unique to your application.

func VersionID

func VersionID(c context.Context) string

VersionID returns the version ID for the current application. It will be of the form "X.Y", where X is specified in app.yaml, and Y is a number generated when each version of the app is uploaded. It does not include a module name.

func WithAPICallFunc

func WithAPICallFunc(ctx context.Context, f APICallFunc) context.Context

WithAPICallFunc returns a copy of the parent context that will cause API calls to invoke f instead of their normal operation.

This is intended for advanced users only.

func WithContext

func WithContext(parent context.Context, req *http.Request) context.Context

WithContext returns a copy of the parent context and associates it with an in-flight HTTP request. This function is cheap.

Types

type APICallFunc

type APICallFunc func(ctx context.Context, service, method string, in, out proto.Message) error

APICallFunc defines a function type for handling an API call. See WithCallOverride.

type BlobKey

type BlobKey string

BlobKey is a key for a blobstore blob.

Conceptually, this type belongs in the blobstore package, but it lives in the appengine package to avoid a circular dependency: blobstore depends on datastore, and datastore needs to refer to the BlobKey type.

type Certificate

type Certificate struct {
	KeyName string
	Data    []byte // PEM-encoded X.509 certificate
}

Certificate represents a public certificate for the app.

func PublicCertificates

func PublicCertificates(c context.Context) ([]Certificate, error)

PublicCertificates retrieves the public certificates for the app. They can be used to verify a signature returned by SignBytes.

type GeoPoint

type GeoPoint struct {
	Lat, Lng float64
}

GeoPoint represents a location as latitude/longitude in degrees.

func (GeoPoint) Valid

func (g GeoPoint) Valid() bool

Valid returns whether a GeoPoint is within [-90, 90] latitude and [-180, 180] longitude.

type MultiError

type MultiError []error

MultiError is returned by batch operations when there are errors with particular elements. Errors will be in a one-to-one correspondence with the input elements; successful elements will have a nil entry.

func (MultiError) Error

func (m MultiError) Error() string

Directories

Path Synopsis
Package aetest provides an API for running dev_appserver for use in tests.
Package aetest provides an API for running dev_appserver for use in tests.
Package blobstore provides a client for App Engine's persistent blob storage service.
Package blobstore provides a client for App Engine's persistent blob storage service.
Package capability exposes information about outages and scheduled downtime for specific API capabilities.
Package capability exposes information about outages and scheduled downtime for specific API capabilities.
Package channel implements the server side of App Engine's Channel API.
Package channel implements the server side of App Engine's Channel API.
Package cloudsql exposes access to Google Cloud SQL databases.
Package cloudsql exposes access to Google Cloud SQL databases.
cmd
aebundler
Program aebundler turns a Go app into a fully self-contained tar file.
Program aebundler turns a Go app into a fully self-contained tar file.
aedeploy
Program aedeploy assists with deploying App Engine "flexible environment" Go apps to production.
Program aedeploy assists with deploying App Engine "flexible environment" Go apps to production.
Package datastore provides a client for App Engine's datastore service.
Package datastore provides a client for App Engine's datastore service.
Package delay provides a way to execute code outside the scope of a user request by using the taskqueue API.
Package delay provides a way to execute code outside the scope of a user request by using the taskqueue API.
demos
Package file provides helper functions for using Google Cloud Storage.
Package file provides helper functions for using Google Cloud Storage.
Package image provides image services.
Package image provides image services.
Package log provides the means of writing and querying an application's logs from within an App Engine application.
Package log provides the means of writing and querying an application's logs from within an App Engine application.
Package mail provides the means of sending email from an App Engine application.
Package mail provides the means of sending email from an App Engine application.
Package memcache provides a client for App Engine's distributed in-memory key-value store for small chunks of arbitrary data.
Package memcache provides a client for App Engine's distributed in-memory key-value store for small chunks of arbitrary data.
Package module provides functions for interacting with modules.
Package module provides functions for interacting with modules.
Package remote_api implements the /_ah/remote_api endpoint.
Package remote_api implements the /_ah/remote_api endpoint.
Package runtime exposes information about the resource usage of the application.
Package runtime exposes information about the resource usage of the application.
Package search provides a client for App Engine's search service.
Package search provides a client for App Engine's search service.
Package socket provides outbound network sockets.
Package socket provides outbound network sockets.
Package taskqueue provides a client for App Engine's taskqueue service.
Package taskqueue provides a client for App Engine's taskqueue service.
Package urlfetch provides an http.RoundTripper implementation for fetching URLs via App Engine's urlfetch service.
Package urlfetch provides an http.RoundTripper implementation for fetching URLs via App Engine's urlfetch service.
Package user provides a client for App Engine's user authentication service.
Package user provides a client for App Engine's user authentication service.
Package xmpp provides the means to send and receive instant messages to and from users of XMPP-compatible services.
Package xmpp provides the means to send and receive instant messages to and from users of XMPP-compatible services.
Package internal provides support for package appengine.
Package internal provides support for package appengine.
aetesting
Package aetesting provides utilities for testing App Engine packages.
Package aetesting provides utilities for testing App Engine packages.
log

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL