How to send 204 No Content with Go http package? - google-app-engine

I built a tiny sample app with Go on Google App Engine that sends string responses when different URLs are invoked. But how can I use Go's http package to send a 204 No Content response to clients?
package hello
import (
"fmt"
"net/http"
"appengine"
"appengine/memcache"
)
func init() {
http.HandleFunc("/", hello)
http.HandleFunc("/hits", showHits)
}
func hello(w http.ResponseWriter, r *http.Request) {
name := r.Header.Get("name")
fmt.Fprintf(w, "Hello %s!", name)
}
func showHits(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%d", hits(r))
}
func hits(r *http.Request) uint64 {
c := appengine.NewContext(r)
newValue, _ := memcache.Increment(c, "hits", 1, 0)
return newValue
}

According to the package docs:
func NoContent(w http.ResponseWriter, r *http.Request) {
// Set up any headers you want here.
w.WriteHeader(http.StatusNoContent) // send the headers with a 204 response code.
}
will send a 204 status to the client.

Sending 204 response from your script means your instance still need to run and cost you money. If you are looking for a caching solution. Google got it and it's called Edge Cache.
You only need to response with the following headers and Google will automatically cache your response in multiple servers nearest to the users (that is, replying with 204). This greatly enhance your site's speed and reduce instance cost.
w.Header().Set("Cache-Control", "public, max-age=86400")
w.Header().Set("Pragma", "Public")
You can adjust the max-age, but do it wisely.
By the way, it seems billing must be enabled in order to use Edge Cache

Related

Stackdriver Trace with Google App Engine Go 1.11 runtime

I would like to get Stackdriver Trace to work on the new Go 1.11 standard runtime on Google App Engine. This is all still labeled as beta so maybe it just doesn't quite work yet. However, I followed the step-by-step directions and it isn't working. I deployed the code (almost) exactly as it is listed in the link and my traces are flat (i.e. doesn't include the waterfall view I would expect with the incoming request at the top and the outgoing request nested underneath).
Sample code
package main
import (
"fmt"
"log"
"net/http"
"os"
"contrib.go.opencensus.io/exporter/stackdriver"
"contrib.go.opencensus.io/exporter/stackdriver/propagation"
"go.opencensus.io/plugin/ochttp"
"go.opencensus.io/trace"
)
func main() {
// Create and register a OpenCensus Stackdriver Trace exporter.
exporter, err := stackdriver.NewExporter(stackdriver.Options{
ProjectID: os.Getenv("GOOGLE_CLOUD_PROJECT"),
})
if err != nil {
log.Fatal(err)
}
trace.RegisterExporter(exporter)
client := &http.Client{
Transport: &ochttp.Transport{
// Use Google Cloud propagation format.
Propagation: &propagation.HTTPFormat{},
},
}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
req, _ := http.NewRequest("GET", "https://www.google.com/robots.txt", nil)
// The trace ID from the incoming request will be
// propagated to the outgoing request.
req = req.WithContext(r.Context())
// The outgoing request will be traced with r's trace ID.
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
fmt.Fprint(w, "OK")
})
http.Handle("/foo", handler)
log.Fatal(http.ListenAndServe(":"+os.Getenv("PORT"), &ochttp.Handler{}))
}
Sample trace:
As written in the reply comment in the original question, can you try the config for sampling?: trace.AlwaysSample()
You can find some comments about sampling rate in OpenCensus Trace documentation and godoc of OpenCensus Trace library:
By default, traces will be sampled relatively rarely. To change the sampling frequency for your entire program, call ApplyConfig. Use a ProbabilitySampler to sample a subset of traces, or use AlwaysSample to collect a trace on every run:

Parse multipart form on Google App Engine

A project I'm working on depends on having a service hosted on Google App Engine parse from SendGrid. The following code is an example of what we're doing:
package sendgrid_failure
import (
"net/http"
"fmt"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
)
func init() {
http.HandleFunc("/sendgrid/parse", sendGridHandler)
}
func sendGridHandler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
err := r.ParseMultipartForm(-1)
if err != nil {
log.Errorf(ctx, "Unable to parse form: %v", err)
}
fmt.Fprint(w, "Test.")
}
When SendGrid POSTs its multipart form, the console shows similar to the following:
2018/01/04 23:44:08 ERROR: Unable to parse form: open /tmp/multipart-445139883: no file writes permitted on App Engine
App Engine doesn't allow you to read/write files, but Golang appears to need it to parse. Is there an App Engine specific library to parse multipart forms, or should we be using a different method from the standard net/http library entirely? We're using the standard go runtime.
The documentation for ParseMultipartForm says:
The whole request body is parsed and up to a total of maxMemory bytes of its file parts are stored in memory, with the remainder stored on disk in temporary files.
The server attempts to write all files to disk because the application passed -1 as maxMemory. Use a value larger than the size of the files you expect to upload.

How to make a get request mid application

How would I go about creating a GET HTTP request mid application in GAE? I don't want to have it as a handler function, I simply have a URL that I need to get the response body from.
Use the urlfetch package.
ctx := appengine.NewContext(r) // r is the *http.Request arg to the handler
client := urlfetch.Client(ctx)
resp, err := client.Get("http://example.com")
if err != nil {
// handle the error
}
body := resp.Body // body is an io.Reader containing the response body
Here's a complete example.
I haven't used it so apologies if this doesn't work. According to GAE's docs you probably want to use urlfetch to get a *http.Client something like (N.B. the context package is standard in the just released Go 1.7):
import (
"context" // Go 1.7
// "golang.org/x/net/context" // Go < 1.7
"google.golang.org/appengine/urlfetch"
)
client := urlfetch.Client(context.Background())
resp, err := client.Get("http://example.com/")

How do I use a client ID for OAuth2 on App Engine in Go?

I have some fairly simple Go code running in AppEngine that should be using OAuth2 to fetch the list of files from the user's account. It seems to initialize the service OK but when it tries to fetch the file list, I get this error:
OAuthError: RoundTrip: no Token supplied
package foo
import (
"appengine"
"appengine/urlfetch"
"code.google.com/p/goauth2/oauth"
"code.google.com/p/google-api-go-client/drive/v2"
"fmt"
"net/http"
)
var config = &oauth.Config{
ClientId: "(redacted).apps.googleusercontent.com",
ClientSecret: "REDACTED",
Scope: "https://www.googleapis.com/auth/drive",
AuthURL: "https://accounts.google.com/o/oauth2/auth",
TokenURL: "https://accounts.google.com/o/oauth2/token",
}
func init() {
http.HandleFunc("/", home)
}
func home(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
transport := &oauth.Transport{
Config: config,
Transport: &urlfetch.Transport{Context: c}}
svc, err := drive.New(transport.Client())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
q := svc.Files.List()
_, err = q.Do()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "Success!")
}
I cannot figure out what I'm doing wrong here. Any help would be kindly appreciated.
This page is kinda old but it outlines the steps nicely with Go code.
http://golangtutorials.blogspot.com/2011/11/oauth2-3-legged-authorization-in-go-web.html
The token configuration is not enough; you first have to get a valid access token with the following steps:
Redirect the user to the page returned by AuthCodeURL. The user will be shown the name of your application and the requested permissions.
If the user grants the permissions, they will be redirected to the RedirectURL you gave in the configuration. The URL will contain a query parameter named code.
Retrieve the code parameter and pass it to Exchange. If everything went well, the requests should now be authenticated properly.

Google GO: routing request handling mystical declarations?

I'm dorking around with Google GO for the first time. I've extended the "hello world" application to try to have paths defined in the init section. Here's what I've done so far:
package hello
import (
"fmt"
"net/http"
)
func init() {
http.HandleFunc("/service", serviceHandler)
http.HandleFunc("/site", siteHandler)
http.HandleFunc("/", handler)
}
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, there")
}
func serviceHandler( w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "this is Services")
}
func siteHandler( w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "this is Sites")
}
Only the handler() callback is ever executed -- the others are ignored. E.g.: http://myserver/service/foo prints Hello, there. I had hoped that it would be this is Services.
Is there a better way to do service routing? Ideally, I would expect these to be separate scripts anyway, but it looks like Go has only one script, based on the fact that the app.yaml requires a special string _go_app in the script declaration.
Thanks!
According to the documentation at: http://golang.org/pkg/net/http/#ServeMux
path specs that do not have a trailing slash only match that path exactly. Add a slash to the end like so: http.HandleFunc("/service/", serviceHandler) and it will work as you expect.

Resources