aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar alecdwm 2015-12-18 12:14:19 +1000
committerGravatar alecdwm 2015-12-18 12:14:19 +1000
commitd891426114880f63ca1cf22e2a3e9a17042ce176 (patch)
treef32b5afdf851a16950fcba7472ea13fc00b22e3a
parentbbeaf040d688772e37114c06189fc61fe5a314aa (diff)
Use cache to prevent freaking out when Github unavailable
-rw-r--r--lib/errors.go25
-rw-r--r--lib/github.go57
-rw-r--r--lib/gogetme.go24
-rw-r--r--lib/pages.go4
4 files changed, 57 insertions, 53 deletions
diff --git a/lib/errors.go b/lib/errors.go
deleted file mode 100644
index 263cb17..0000000
--- a/lib/errors.go
+++ /dev/null
@@ -1,25 +0,0 @@
-// GoGetMe - go get your GitHub-hosted Go packages via your own domain
-// Copyright (C) 2015 alecdwm
-
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-
-// You should have received a copy of the GNU General Public License
-// along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-package lib
-
-import "github.com/Sirupsen/logrus"
-
-func handleError(str string, err error) {
- if err != nil {
- logrus.WithError(err).Error(str)
- }
-}
diff --git a/lib/github.go b/lib/github.go
index fb286cc..326bb37 100644
--- a/lib/github.go
+++ b/lib/github.go
@@ -20,9 +20,11 @@ import (
"encoding/json"
"fmt"
"net/http"
+
+ "github.com/Sirupsen/logrus"
)
-type GitHubUserData struct {
+type GithubUserData struct {
Login string `json:"login"`
Id int `json:"id"`
AvatarUrl string `json:"avatar_url"`
@@ -55,40 +57,57 @@ type GitHubUserData struct {
UpdatedAt string `json:"updated_at"`
}
-func getGitHubUserData(username string) GitHubUserData {
+func getGithubUserData(username string) GithubUserData {
// Fetch
- response, err := http.Get(fmt.Sprintf(
- "https://api.github.com/users/%s",
- username))
- handleError("Fetching GitHub user data", err)
- defer response.Body.Close()
+ response, err := http.Get(
+ fmt.Sprintf("https://api.github.com/users/%s", username))
+ if err != nil {
+ logrus.WithError(err).Error("Fetching Github user data")
+ } else {
+ defer response.Body.Close()
+ }
// Decode
- var githubUserData GitHubUserData
+ var githubUserData GithubUserData
err = json.NewDecoder(response.Body).Decode(&githubUserData)
- handleError("Parsing response as JSON", err)
+ if err != nil {
+ logrus.WithError(err).Error("Parsing response as JSON")
+ }
// Return
return githubUserData
}
-type GitHubUserRepo struct {
+type GithubUserRepo struct {
CloneUrl string `json:"clone_url"`
}
-func getGitHubUserRepos(username string) []GitHubUserRepo {
+var githubUserReposCache []GithubUserRepo
+
+func getGithubUserRepos(username string, useCache bool) []GithubUserRepo {
+ if useCache && len(githubUserReposCache) != 0 {
+ return githubUserReposCache
+ }
+
// Fetch
- response, err := http.Get(fmt.Sprintf(
- "https://api.github.com/users/%s/repos",
- username))
- handleError("Fetching GitHub user repos", err)
- defer response.Body.Close()
+ response, err1 := http.Get(
+ fmt.Sprintf("https://api.github.com/users/%s/repos", username))
+ if err1 != nil {
+ logrus.WithError(err1).Error("Fetching Github user repos")
+ } else {
+ defer response.Body.Close()
+ }
// Decode
- var githubUserRepos []GitHubUserRepo
- err = json.NewDecoder(response.Body).Decode(&githubUserRepos)
- handleError("Parsing response as JSON", err)
+ var githubUserRepos []GithubUserRepo
+ err2 := json.NewDecoder(response.Body).Decode(&githubUserRepos)
+ if err2 != nil {
+ logrus.WithError(err2).Error("Parsing response as JSON")
+ }
// Return
+ if err1 == nil && err2 == nil {
+ githubUserReposCache = githubUserRepos
+ }
return githubUserRepos
}
diff --git a/lib/gogetme.go b/lib/gogetme.go
index 5d60c7f..28b7209 100644
--- a/lib/gogetme.go
+++ b/lib/gogetme.go
@@ -30,12 +30,12 @@ import (
"net/http"
"time"
- log "github.com/Sirupsen/logrus"
+ "github.com/Sirupsen/logrus"
)
func Serve(customUrl, githubUser, listenPort string) {
// Process inputs
- githubUserData := getGitHubUserData(githubUser)
+ githubUserData := getGithubUserData(githubUser)
listenPort = ":" + listenPort
// Prepare handler
@@ -44,14 +44,16 @@ func Serve(customUrl, githubUser, listenPort string) {
})
// Listen and SERVE!
- log.Info("Handling connections")
+ logrus.Info("Handling connections")
err := http.ListenAndServe(listenPort, nil)
- handleError("Serving HTTP requests", err)
+ if err != nil {
+ logrus.WithError(err).Error("Serving HTTP requests")
+ }
}
-func handler(w http.ResponseWriter, r *http.Request, customUrl string, githubUserData GitHubUserData) {
+func handler(w http.ResponseWriter, r *http.Request, customUrl string, githubUserData GithubUserData) {
t := time.Now()
- log.WithFields(log.Fields{
+ logrus.WithFields(logrus.Fields{
"at": t.Format(time.RFC850),
"from": r.RemoteAddr,
}).Info("Handled Connection")
@@ -64,7 +66,7 @@ func handler(w http.ResponseWriter, r *http.Request, customUrl string, githubUse
} else {
requestRepo := fmt.Sprintf(
"https://github.com/%s.git", githubUserData.Login+"/"+thisPackage)
- githubUserRepos := getGitHubUserRepos(githubUserData.Login)
+ githubUserRepos := getGithubUserRepos(githubUserData.Login, true)
repoExists := false
for _, repo := range githubUserRepos {
@@ -72,6 +74,14 @@ func handler(w http.ResponseWriter, r *http.Request, customUrl string, githubUse
repoExists = true
}
}
+ if !repoExists {
+ githubUserRepos = getGithubUserRepos(githubUserData.Login, false)
+ for _, repo := range githubUserRepos {
+ if repo.CloneUrl == requestRepo {
+ repoExists = true
+ }
+ }
+ }
if !repoExists {
fmt.Fprint(w, redirectPage("Repository doesn't exist", "/"))
diff --git a/lib/pages.go b/lib/pages.go
index 05a4f38..deee19a 100644
--- a/lib/pages.go
+++ b/lib/pages.go
@@ -171,7 +171,7 @@ func redirectPage(msg, to string) (pageHtml string) {
return pageHtml
}
-func userProfilePage(customUrl string, githubUserData GitHubUserData) (pageHtml string) {
+func userProfilePage(customUrl string, githubUserData GithubUserData) (pageHtml string) {
pageHtml = fmt.Sprintf(`<!DOCTYPE html>
<html lang="en">
<head>
@@ -217,7 +217,7 @@ func userProfilePage(customUrl string, githubUserData GitHubUserData) (pageHtml
return pageHtml
}
-func packageProfilePage(customUrl, thisPackage string, githubUserData GitHubUserData) (pageHtml string) {
+func packageProfilePage(customUrl, thisPackage string, githubUserData GithubUserData) (pageHtml string) {
pageHtml = fmt.Sprintf(`<!DOCTYPE html>
<html lang="en">
<head>