Google App Engine Recipe
Google App Engine (GAE) provides a range of hosting options from pure PaaS (App Engine Classic) through Managed VMs to fully self-managed or container-driven Compute Engine instances. Echo works great with all of these but requires a few changes to the usual examples to run on the AppEngine Classic and Managed VM options. With a small amount of effort though it’s possible to produce a codebase that will run on these and also non-managed platforms automatically.
We’ll walk through the changes needed to support each option.
Standalone
Wait? What? I thought this was about AppEngine! Bear with me - the easiest way to show the changes required is to start with a setup for standalone and work from there plus there’s no reason we wouldn’t want to retain the ability to run our app anywhere, right?
We take advantage of the go build constraints or tags to change how we create and run the Echo server for each platform while keeping the rest of the application (e.g. handler wireup) the same across all of them.
app.govarinit()
app.go
{{< embed “google-app-engine/app.go” >}}
main()
app-standalone.go
{{< embed “google-app-engine/app-standalone.go” >}}
init()emain()Groupapi
users.go
{{< embed “google-app-engine/users.go” >}}
If we run our app it should execute as it did before when everything was in one file although we have at least gained the ability to organize our handlers a little more cleanly.
AppEngine Classic and Managed VMs
So far we’ve seen how to split apart the Echo creation and setup but still have the same app that still only runs standalone. Now we’ll see hwo those changes allow us to add support for AppEngine hosting.
Refer to the AppEngine site for full configuration and deployment information.
app.yaml configuration file
app.yaml
app-classic.yamlapp-vm.yaml
app-engine.yaml
{{< embed “google-app-engine/app-engine.yaml” >}}
Router configuration
app-standalone.go
createMux()main()http.Handler
app-engine.go
{{< embed “google-app-engine/app-engine.go” >}}
google.golang.org/appengine
app-managed.go
{{< embed “google-app-engine/app-managed.go” >}}
app.yaml
We can also run locally using the Google AppEngine SDK for Go either emulating AppEngine Classic:
goapp serve
gcloud config set project [your project id]
gcloud preview app run .
And of course we can deploy our app to both of these platforms for easy and inexpensive auto-scaling joy.
Depending on what your app actually does it’s possible you may need to make other changes to allow switching between AppEngine provided service such as Datastore and alternative storage implementations such as MongoDB. A combination of go interfaces and build constraints can make this fairly straightforward but is outside the scope of this recipe.