The Go package, google.golang.org/appengine, provides IsDevAppServer which reports whether an App Engine app is running in the development App Server (e.g. localhost:8080
). However, this does not work unless the (deprecated) standalone SDK is used. See appengine.go#L57 for the implementation.
New GAE apps written in Go are basically a regular web server that can be compiled and started locally like any go program;
- old;
dev_appserver.py
- new;
go run main.go
Detecting a development server can be useful for to prevent CORS issues when running locally:
func setDevHeaders(w http.ResponseWriter) {
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:4200")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Access-Control-Request-Method", "POST, GET")
w.Header().Set("Access-Control-Expose-Headers", "Content-Disposition")
}
When needed I can then branch:
if appengine.IsDevAppServer() {
setDevHeaders(w)
}
What is the recommended way to achieve this in a standalone Go server running on App Engine?