mirror of
https://github.com/samsonjs/retrogit.git
synced 2026-04-27 15:07:43 +00:00
Break out accounts, digests, caching and session configuration out of githup.go.
This commit is contained in:
parent
0b7b99b696
commit
b0811da602
6 changed files with 217 additions and 181 deletions
3
TODO
3
TODO
|
|
@ -1,5 +1,6 @@
|
||||||
TODO
|
TODO
|
||||||
- Break up github.go
|
- Break up indexHandler
|
||||||
|
- Single email sending button
|
||||||
- Loop over registered accounts and send them email
|
- Loop over registered accounts and send them email
|
||||||
- Flash message and sign out when OAuth token has expired/is invalid
|
- Flash message and sign out when OAuth token has expired/is invalid
|
||||||
- Handle pagination for user repository list
|
- Handle pagination for user repository list
|
||||||
|
|
|
||||||
43
app/account.go
Normal file
43
app/account.go
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
package githop
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/gob"
|
||||||
|
|
||||||
|
"appengine"
|
||||||
|
"appengine/datastore"
|
||||||
|
|
||||||
|
"code.google.com/p/goauth2/oauth"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Account struct {
|
||||||
|
GitHubUserId int `datastore:",noindex"`
|
||||||
|
// The datastore API doesn't store maps, and the token contains one. We
|
||||||
|
// thefore store a gob-serialized version instead.
|
||||||
|
OAuthTokenSerialized []byte
|
||||||
|
OAuthToken oauth.Token `datastore:"-,"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAccount(c appengine.Context, gitHubUserId int) (*Account, error) {
|
||||||
|
key := datastore.NewKey(c, "Account", "", int64(gitHubUserId), nil)
|
||||||
|
account := new(Account)
|
||||||
|
err := datastore.Get(c, key, account)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
r := bytes.NewBuffer(account.OAuthTokenSerialized)
|
||||||
|
err = gob.NewDecoder(r).Decode(&account.OAuthToken)
|
||||||
|
return account, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (account *Account) Put(c appengine.Context) error {
|
||||||
|
w := new(bytes.Buffer)
|
||||||
|
err := gob.NewEncoder(w).Encode(&account.OAuthToken)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
account.OAuthTokenSerialized = w.Bytes()
|
||||||
|
key := datastore.NewKey(c, "Account", "", int64(account.GitHubUserId), nil)
|
||||||
|
_, err = datastore.Put(c, key, account)
|
||||||
|
return err
|
||||||
|
}
|
||||||
62
app/caching_transport.go
Normal file
62
app/caching_transport.go
Normal file
|
|
@ -0,0 +1,62 @@
|
||||||
|
package githop
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httputil"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"appengine"
|
||||||
|
"appengine/memcache"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Simple http.RoundTripper implementation which wraps an existing transport and
|
||||||
|
// caches all responses for GET and HEAD requests. Meant to speed up the
|
||||||
|
// iteration cycle during development.
|
||||||
|
type CachingTransport struct {
|
||||||
|
Transport http.RoundTripper
|
||||||
|
Context appengine.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *CachingTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
|
||||||
|
if req.Method != "GET" && req.Method != "HEAD" {
|
||||||
|
return t.Transport.RoundTrip(req)
|
||||||
|
}
|
||||||
|
cacheKey := "CachingTransport:" + req.URL.String() + "#"
|
||||||
|
authorizationHeaders, ok := req.Header["Authorization"]
|
||||||
|
if ok {
|
||||||
|
cacheKey += strings.Join(authorizationHeaders, "#")
|
||||||
|
} else {
|
||||||
|
cacheKey += "Unauthorized"
|
||||||
|
}
|
||||||
|
|
||||||
|
cachedRespItem, err := memcache.Get(t.Context, cacheKey)
|
||||||
|
if err != nil && err != memcache.ErrCacheMiss {
|
||||||
|
t.Context.Errorf("Error getting cached response: %v", err)
|
||||||
|
return t.Transport.RoundTrip(req)
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
cacheRespBuffer := bytes.NewBuffer(cachedRespItem.Value)
|
||||||
|
resp, err := http.ReadResponse(bufio.NewReader(cacheRespBuffer), req)
|
||||||
|
if err == nil {
|
||||||
|
return resp, nil
|
||||||
|
} else {
|
||||||
|
t.Context.Errorf("Error readings bytes for cached response: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
resp, err = t.Transport.RoundTrip(req)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
respBytes, err := httputil.DumpResponse(resp, true)
|
||||||
|
if err != nil {
|
||||||
|
t.Context.Errorf("Error dumping bytes for cached response: %v", err)
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
err = memcache.Set(t.Context, &memcache.Item{Key: cacheKey, Value: respBytes})
|
||||||
|
if err != nil {
|
||||||
|
t.Context.Errorf("Error setting cached response: %v", err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
63
app/digest.go
Normal file
63
app/digest.go
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
package githop
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sort"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/go-github/github"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RepoDigest struct {
|
||||||
|
Repo *github.Repository
|
||||||
|
Commits []github.RepositoryCommit
|
||||||
|
}
|
||||||
|
|
||||||
|
// sort.Interface implementation for sorting RepoDigests.
|
||||||
|
type ByRepoFullName []*RepoDigest
|
||||||
|
|
||||||
|
func (a ByRepoFullName) Len() int { return len(a) }
|
||||||
|
func (a ByRepoFullName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||||
|
func (a ByRepoFullName) Less(i, j int) bool { return *a[i].Repo.FullName < *a[j].Repo.FullName }
|
||||||
|
|
||||||
|
type Digest struct {
|
||||||
|
User *github.User
|
||||||
|
StartTime time.Time
|
||||||
|
EndTime time.Time
|
||||||
|
RepoDigests []*RepoDigest
|
||||||
|
}
|
||||||
|
|
||||||
|
func (digest *Digest) Fetch(repos []github.Repository, githubClient *github.Client) error {
|
||||||
|
type RepoDigestResponse struct {
|
||||||
|
repoDigest *RepoDigest
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
ch := make(chan *RepoDigestResponse)
|
||||||
|
for _, repo := range repos {
|
||||||
|
go func(repo github.Repository) {
|
||||||
|
commits, _, err := githubClient.Repositories.ListCommits(
|
||||||
|
*repo.Owner.Login,
|
||||||
|
*repo.Name,
|
||||||
|
&github.CommitsListOptions{
|
||||||
|
Author: *digest.User.Login,
|
||||||
|
Since: digest.StartTime,
|
||||||
|
Until: digest.EndTime,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
ch <- &RepoDigestResponse{nil, err}
|
||||||
|
} else {
|
||||||
|
ch <- &RepoDigestResponse{&RepoDigest{&repo, commits}, nil}
|
||||||
|
}
|
||||||
|
}(repo)
|
||||||
|
}
|
||||||
|
for i := 0; i < len(repos); i++ {
|
||||||
|
select {
|
||||||
|
case r := <-ch:
|
||||||
|
if r.err != nil {
|
||||||
|
return r.err
|
||||||
|
}
|
||||||
|
digest.RepoDigests = append(digest.RepoDigests, r.repoDigest)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Sort(ByRepoFullName(digest.RepoDigests))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
181
app/githop.go
181
app/githop.go
|
|
@ -1,23 +1,14 @@
|
||||||
package githop
|
package githop
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"bytes"
|
|
||||||
"encoding/base64"
|
|
||||||
"encoding/gob"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"appengine"
|
"appengine"
|
||||||
"appengine/datastore"
|
|
||||||
"appengine/memcache"
|
|
||||||
"appengine/urlfetch"
|
"appengine/urlfetch"
|
||||||
|
|
||||||
"code.google.com/p/goauth2/oauth"
|
"code.google.com/p/goauth2/oauth"
|
||||||
|
|
@ -31,126 +22,6 @@ var githubOauthConfig oauth.Config
|
||||||
var sessionStore *sessions.CookieStore
|
var sessionStore *sessions.CookieStore
|
||||||
var sessionConfig SessionConfig
|
var sessionConfig SessionConfig
|
||||||
|
|
||||||
type SessionConfig struct {
|
|
||||||
AuthenticationKey string
|
|
||||||
EncryptionKey string
|
|
||||||
CookieName string
|
|
||||||
UserIdKey string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Account struct {
|
|
||||||
GitHubUserId int `datastore:",noindex"`
|
|
||||||
// The datastore API doesn't store maps, and the token contains one. We
|
|
||||||
// thefore store a gob-serialized version instead.
|
|
||||||
OAuthTokenSerialized []byte
|
|
||||||
OAuthToken oauth.Token `datastore:"-,"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAccount(c appengine.Context, gitHubUserId int) (*Account, error) {
|
|
||||||
key := datastore.NewKey(c, "Account", "", int64(gitHubUserId), nil)
|
|
||||||
account := new(Account)
|
|
||||||
err := datastore.Get(c, key, account)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
r := bytes.NewBuffer(account.OAuthTokenSerialized)
|
|
||||||
err = gob.NewDecoder(r).Decode(&account.OAuthToken)
|
|
||||||
return account, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (account *Account) Put(c appengine.Context) error {
|
|
||||||
w := new(bytes.Buffer)
|
|
||||||
err := gob.NewEncoder(w).Encode(&account.OAuthToken)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
account.OAuthTokenSerialized = w.Bytes()
|
|
||||||
key := datastore.NewKey(c, "Account", "", int64(account.GitHubUserId), nil)
|
|
||||||
_, err = datastore.Put(c, key, account)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
type RepoDigest struct {
|
|
||||||
Repo *github.Repository
|
|
||||||
Commits []github.RepositoryCommit
|
|
||||||
}
|
|
||||||
|
|
||||||
// sort.Interface implementation for sorting RepoDigests.
|
|
||||||
type ByRepoFullName []*RepoDigest
|
|
||||||
|
|
||||||
func (a ByRepoFullName) Len() int { return len(a) }
|
|
||||||
func (a ByRepoFullName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
||||||
func (a ByRepoFullName) Less(i, j int) bool { return *a[i].Repo.FullName < *a[j].Repo.FullName }
|
|
||||||
|
|
||||||
type Digest struct {
|
|
||||||
User *github.User
|
|
||||||
StartTime time.Time
|
|
||||||
EndTime time.Time
|
|
||||||
RepoDigests []*RepoDigest
|
|
||||||
}
|
|
||||||
|
|
||||||
func (digest *Digest) Fetch(repos []github.Repository, githubClient *github.Client) error {
|
|
||||||
type RepoDigestResponse struct {
|
|
||||||
repoDigest *RepoDigest
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
ch := make(chan *RepoDigestResponse)
|
|
||||||
for _, repo := range repos {
|
|
||||||
go func(repo github.Repository) {
|
|
||||||
commits, _, err := githubClient.Repositories.ListCommits(
|
|
||||||
*repo.Owner.Login,
|
|
||||||
*repo.Name,
|
|
||||||
&github.CommitsListOptions{
|
|
||||||
Author: *digest.User.Login,
|
|
||||||
Since: digest.StartTime,
|
|
||||||
Until: digest.EndTime,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
ch <- &RepoDigestResponse{nil, err}
|
|
||||||
} else {
|
|
||||||
ch <- &RepoDigestResponse{&RepoDigest{&repo, commits}, nil}
|
|
||||||
}
|
|
||||||
}(repo)
|
|
||||||
}
|
|
||||||
for i := 0; i < len(repos); i++ {
|
|
||||||
select {
|
|
||||||
case r := <-ch:
|
|
||||||
if r.err != nil {
|
|
||||||
return r.err
|
|
||||||
}
|
|
||||||
digest.RepoDigests = append(digest.RepoDigests, r.repoDigest)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sort.Sort(ByRepoFullName(digest.RepoDigests))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func initSessionConfig() {
|
|
||||||
configBytes, err := ioutil.ReadFile("config/session.json")
|
|
||||||
if err != nil {
|
|
||||||
log.Panicf("Could not read session config: %s", err.Error())
|
|
||||||
}
|
|
||||||
err = json.Unmarshal(configBytes, &sessionConfig)
|
|
||||||
if err != nil {
|
|
||||||
log.Panicf("Could not parse session config %s: %s", configBytes, err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
authenticationKey, err := base64.StdEncoding.DecodeString(sessionConfig.AuthenticationKey)
|
|
||||||
if err != nil {
|
|
||||||
log.Panicf("Could not decode session config authentication key %s: %s", sessionConfig.AuthenticationKey, err.Error())
|
|
||||||
}
|
|
||||||
encryptionKey, err := base64.StdEncoding.DecodeString(sessionConfig.EncryptionKey)
|
|
||||||
if err != nil {
|
|
||||||
log.Panicf("Could not decode session config encryption key %s: %s", sessionConfig.EncryptionKey, err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
sessionStore = sessions.NewCookieStore(authenticationKey, encryptionKey)
|
|
||||||
sessionStore.Options.Path = "/"
|
|
||||||
sessionStore.Options.MaxAge = 86400 * 30
|
|
||||||
sessionStore.Options.HttpOnly = true
|
|
||||||
sessionStore.Options.Secure = !appengine.IsDevAppServer()
|
|
||||||
}
|
|
||||||
|
|
||||||
func initGithubOAuthConfig() {
|
func initGithubOAuthConfig() {
|
||||||
path := "config/github-oauth"
|
path := "config/github-oauth"
|
||||||
if appengine.IsDevAppServer() {
|
if appengine.IsDevAppServer() {
|
||||||
|
|
@ -171,7 +42,7 @@ func initGithubOAuthConfig() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
initSessionConfig()
|
sessionStore, sessionConfig = initSession()
|
||||||
initGithubOAuthConfig()
|
initGithubOAuthConfig()
|
||||||
|
|
||||||
router = mux.NewRouter()
|
router = mux.NewRouter()
|
||||||
|
|
@ -335,53 +206,3 @@ func githubOAuthTransport(r *http.Request) *oauth.Transport {
|
||||||
Transport: cachingTransport,
|
Transport: cachingTransport,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simple http.RoundTripper implementation which wraps an existing transport and
|
|
||||||
// caches all responses for GET and HEAD requests. Meant to speed up the
|
|
||||||
// iteration cycle during development.
|
|
||||||
type CachingTransport struct {
|
|
||||||
Transport http.RoundTripper
|
|
||||||
Context appengine.Context
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *CachingTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
|
|
||||||
if req.Method != "GET" && req.Method != "HEAD" {
|
|
||||||
return t.Transport.RoundTrip(req)
|
|
||||||
}
|
|
||||||
cacheKey := "CachingTransport:" + req.URL.String() + "#"
|
|
||||||
authorizationHeaders, ok := req.Header["Authorization"]
|
|
||||||
if ok {
|
|
||||||
cacheKey += strings.Join(authorizationHeaders, "#")
|
|
||||||
} else {
|
|
||||||
cacheKey += "Unauthorized"
|
|
||||||
}
|
|
||||||
|
|
||||||
cachedRespItem, err := memcache.Get(t.Context, cacheKey)
|
|
||||||
if err != nil && err != memcache.ErrCacheMiss {
|
|
||||||
t.Context.Errorf("Error getting cached response: %v", err)
|
|
||||||
return t.Transport.RoundTrip(req)
|
|
||||||
}
|
|
||||||
if err == nil {
|
|
||||||
cacheRespBuffer := bytes.NewBuffer(cachedRespItem.Value)
|
|
||||||
resp, err := http.ReadResponse(bufio.NewReader(cacheRespBuffer), req)
|
|
||||||
if err == nil {
|
|
||||||
return resp, nil
|
|
||||||
} else {
|
|
||||||
t.Context.Errorf("Error readings bytes for cached response: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
resp, err = t.Transport.RoundTrip(req)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
respBytes, err := httputil.DumpResponse(resp, true)
|
|
||||||
if err != nil {
|
|
||||||
t.Context.Errorf("Error dumping bytes for cached response: %v", err)
|
|
||||||
return resp, nil
|
|
||||||
}
|
|
||||||
err = memcache.Set(t.Context, &memcache.Item{Key: cacheKey, Value: respBytes})
|
|
||||||
if err != nil {
|
|
||||||
t.Context.Errorf("Error setting cached response: %v", err)
|
|
||||||
}
|
|
||||||
return resp, nil
|
|
||||||
}
|
|
||||||
|
|
|
||||||
46
app/session.go
Normal file
46
app/session.go
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
package githop
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"appengine"
|
||||||
|
|
||||||
|
"github.com/gorilla/sessions"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SessionConfig struct {
|
||||||
|
AuthenticationKey string
|
||||||
|
EncryptionKey string
|
||||||
|
CookieName string
|
||||||
|
UserIdKey string
|
||||||
|
}
|
||||||
|
|
||||||
|
func initSession() (sessionStore *sessions.CookieStore, sessionConfig SessionConfig) {
|
||||||
|
configBytes, err := ioutil.ReadFile("config/session.json")
|
||||||
|
if err != nil {
|
||||||
|
log.Panicf("Could not read session config: %s", err.Error())
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(configBytes, &sessionConfig)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicf("Could not parse session config %s: %s", configBytes, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
authenticationKey, err := base64.StdEncoding.DecodeString(sessionConfig.AuthenticationKey)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicf("Could not decode session config authentication key %s: %s", sessionConfig.AuthenticationKey, err.Error())
|
||||||
|
}
|
||||||
|
encryptionKey, err := base64.StdEncoding.DecodeString(sessionConfig.EncryptionKey)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicf("Could not decode session config encryption key %s: %s", sessionConfig.EncryptionKey, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
sessionStore = sessions.NewCookieStore(authenticationKey, encryptionKey)
|
||||||
|
sessionStore.Options.Path = "/"
|
||||||
|
sessionStore.Options.MaxAge = 86400 * 30
|
||||||
|
sessionStore.Options.HttpOnly = true
|
||||||
|
sessionStore.Options.Secure = !appengine.IsDevAppServer()
|
||||||
|
return
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue