Skip to content

Fix Google App Engine support and provide working example #697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions example/appengine/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2017 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// The demo app shows how to use the github package on Google App Engine.
package demo

import (
"fmt"
"net/http"
"os"

"github.com/google/go-github/github"
"golang.org/x/oauth2"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
)

func init() {
http.HandleFunc("/", handler)
}

func handler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}

ctx := appengine.NewContext(r)
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_AUTH_TOKEN")},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So a new client must be made for every incoming request? That seems pretty unfortunate.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was expedient for this demo, but I believe it is possible to make a single client as a package global during initialization.

One caveat is that you can't call os.Getenv on App Engine within an init function due to the environment not being fully passed-in on app startup. The details are quite hairy.

So in cases like that, you could have a sync.Once block get the environment variable value and create the client before its first use... but I wanted to have a simple example here and not complicate things too much.


commits, _, err := client.Repositories.ListCommits(ctx, "google", "go-github", nil)
if err != nil {
log.Errorf(ctx, "ListCommits: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "text/plain; charset=utf-8")
for _, commit := range commits {
fmt.Fprintln(w, commit.GetHTMLURL())
}
}
14 changes: 14 additions & 0 deletions example/appengine/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2017 The go-github AUTHORS. All rights reserved.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

runtime: go
api_version: go1

handlers:
- url: /.*
script: _go_app

env_variables:
GITHUB_AUTH_TOKEN: "-your-auth-token-here-"
2 changes: 1 addition & 1 deletion github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func parseRate(r *http.Response) Rate {
// The provided ctx must be non-nil. If it is canceled or times out,
// ctx.Err() will be returned.
func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Response, error) {
ctx, req = withContext(ctx, req)
req = withContext(ctx, req)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one of those rare times where I feel it might be worth breaking the "context is always the first parameter" pattern.

Here, withContext is basically a method-as-a-function that adds ctx to req. I think seeing it like this:

req = withContext(req, ctx)

Might read better...

Well, after writing it up and looking at it for a few minutes, I'm not so sure it looks better. It seems about about the same. 😑

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I'll leave it alone.


rateLimitCategory := category(req.URL.Path)

Expand Down
2 changes: 1 addition & 1 deletion github/repos_contents.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (s *RepositoriesService) GetArchiveLink(ctx context.Context, owner, repo st
}
var resp *http.Response
// Use http.DefaultTransport if no custom Transport is configured
ctx, req = withContext(ctx, req)
req = withContext(ctx, req)
if s.client.client.Transport == nil {
resp, err = http.DefaultTransport.RoundTrip(req)
} else {
Expand Down
2 changes: 1 addition & 1 deletion github/repos_releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func (s *RepositoriesService) DownloadReleaseAsset(ctx context.Context, owner, r
}
defer func() { s.client.client.CheckRedirect = saveRedirect }()

ctx, req = withContext(ctx, req)
req = withContext(ctx, req)
resp, err := s.client.client.Do(req)
if err != nil {
if !strings.Contains(err.Error(), "disable redirect") {
Expand Down
7 changes: 3 additions & 4 deletions github/with_appengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ package github
import (
"context"
"net/http"

"google.golang.org/appengine"
)

func withContext(ctx context.Context, req *http.Request) (context.Context, *http.Request) {
return appengine.WithContext(ctx, req), req
func withContext(ctx context.Context, req *http.Request) *http.Request {
// No-op because App Engine adds context to a request differently.
return req
}
4 changes: 2 additions & 2 deletions github/without_appengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ import (
"net/http"
)

func withContext(ctx context.Context, req *http.Request) (context.Context, *http.Request) {
return ctx, req.WithContext(ctx)
func withContext(ctx context.Context, req *http.Request) *http.Request {
return req.WithContext(ctx)
}