-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
|
||
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()) | ||
} | ||
} |
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-" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, 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. 😑 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. I'll leave it alone. |
||
|
||
rateLimitCategory := category(req.URL.Path) | ||
|
||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 aninit
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.