Debian Codegraph @

example/appengine/app.go

raw
1 // Copyright 2017 The go-github AUTHORS. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5
6 // Package demo provides an app that shows how to use the github package on
7 // Google App Engine.
8 package demo
9
10 import (
11 "fmt"
12 "net/http"
13 "os"
14
15 "github.com/google/go-github/v60/github"
16 "google.golang.org/appengine"
17 "google.golang.org/appengine/log"
18 )
19
20 func init() {
21 http.HandleFunc("/", handler)
22 }
23
24 func handler(w http.ResponseWriter, r *http.Request) {
25 if r.URL.Path != "/" {
26 http.NotFound(w, r)
27 return
28 }
29
30 ctx := appengine.NewContext(r)
31 client := github.NewClient(nil).WithAuthToken(os.Getenv("GITHUB_AUTH_TOKEN"))
32
33 commits, _, err := client.Repositories.ListCommits(ctx, "google", "go-github", nil)
34 if err != nil {
35 log.Errorf(ctx, "ListCommits: %v", err)
36 http.Error(w, err.Error(), http.StatusInternalServerError)
37 return
38 }
39
40 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
41 for _, commit := range commits {
42 fmt.Fprintln(w, commit.GetHTMLURL())
43 }
44 }