diff --git a/config/config.go b/config/config.go index 406f270..970f2de 100644 --- a/config/config.go +++ b/config/config.go @@ -4,6 +4,7 @@ import ( "encoding/json" "flag" "io/ioutil" + "log" "os" ) @@ -29,23 +30,23 @@ func C() Config { } func init() { - var ( - path string - fd *os.File - contents []byte - err error - ) - + var err error + var path string flag.StringVar(&path, "config", "config.json", "Path to configuration file") flag.Parse() + var fd *os.File if fd, err = os.Open(path); err != nil { panic(err) } + var contents []byte if contents, err = ioutil.ReadAll(fd); err != nil { panic(err) } if err = json.Unmarshal(contents, &conf); err != nil { panic(err) } + + log.SetOutput(os.Stderr) + log.SetFlags(log.Ltime | log.Lshortfile) } diff --git a/db/db.go b/db/db.go index 32b2f88..e84f551 100644 --- a/db/db.go +++ b/db/db.go @@ -1,8 +1,6 @@ package db import ( - "strings" - _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx/reflectx" @@ -14,7 +12,7 @@ var ( func Connect(params string) (err error) { db, err = sqlx.Connect("mysql", params) - db.Mapper = reflectx.NewMapperFunc("json", strings.ToLower) + db.Mapper = reflectx.NewMapper("json") return } diff --git a/db/token.go b/db/token.go index 93c91cd..a7e995f 100644 --- a/db/token.go +++ b/db/token.go @@ -10,8 +10,8 @@ type Token struct { Token string `json:"token"` Quota uint64 `json:"quota"` Remaining uint64 `json:"remaining"` - ResetAt time.Time `json:"reset_at"` - CreatedAt time.Time `json:"created_at"` + ResetAt time.Time `json:"reset_at" db:"reset_at"` + CreatedAt time.Time `json:"created_at" db:"created_at"` } const saveTokenQuery = ` diff --git a/db/user.go b/db/user.go index 76d1fbf..e945134 100644 --- a/db/user.go +++ b/db/user.go @@ -4,7 +4,7 @@ type User struct { Login string `json:"login"` Name string `json:"name"` ID uint64 `json:"id"` - AvatarURL string `json:"avatar_url"` + AvatarURL string `json:"avatar_url" db:"avatar_url"` } const saveUserQuery = ` diff --git a/server/auth.go b/server/auth.go index 0fb55b5..f713698 100644 --- a/server/auth.go +++ b/server/auth.go @@ -24,7 +24,7 @@ func authCallbackHandler(w http.ResponseWriter, r *http.Request) { } code := r.FormValue("code") - log.Println("Got code: ", code) + log.Printf("Got code %q\n", code) if _, login, err := task.Authenticate(code); err == nil { createSession(r, login) } else { diff --git a/server/server.go b/server/server.go index 186f195..a3336f2 100644 --- a/server/server.go +++ b/server/server.go @@ -2,8 +2,8 @@ package server import ( "encoding/json" - "fmt" "html/template" + "log" "net/http" "github.com/GeertJohan/go.rice" @@ -34,7 +34,7 @@ func init() { } func Start() { - fmt.Println("Starting server at http://localhost:8080") + log.Println("Starting server at http://localhost:8080") http.ListenAndServe(":8080", nil) } diff --git a/task/auth.go b/task/auth.go index 572779c..1692339 100644 --- a/task/auth.go +++ b/task/auth.go @@ -5,45 +5,49 @@ import ( "encoding/json" "fmt" "io/ioutil" + "log" "net/http" "net/url" + "time" "github.com/localhots/empact/config" "github.com/localhots/empact/db" ) func Authenticate(code string) (token, login string, err error) { + report("Authenticate", time.Now()) if token, err = FetchAccessToken(code); err != nil { return } - fmt.Println("Got token: " + token) + log.Printf("Got token %q for code %q\n", token, code) var user *db.User if user, err = FetchUserInfoWithToken(token); err != nil { return } login = user.Login - fmt.Println("Saving user", user) + log.Println("Saving user", user) user.Save() tok := &db.Token{ User: login, Token: token, } - fmt.Println("Saving token", tok) + log.Println("Saving token", tok) tok.Save() return } func FetchAccessToken(code string) (token string, err error) { + report("FetchAccessToken", time.Now()) payload := url.Values{} payload.Set("client_id", config.C().ClientID) payload.Set("client_secret", config.C().ClientSecret) payload.Set("code", code) payload.Set("redirect_uri", config.C().RedirectURI) - fmt.Println("Requesting token") + log.Printf("Requesting token for code %q", code) buf := bytes.NewBuffer([]byte(payload.Encode())) var resp *http.Response if resp, err = http.Post(config.C().AccessTokenURL, "application/x-www-form-urlencoded", buf); err != nil { @@ -64,6 +68,7 @@ func FetchAccessToken(code string) (token string, err error) { } func FetchUserInfoWithToken(token string) (u *db.User, err error) { + report("FetchUserInfoWithToken", time.Now()) var resp *http.Response if resp, err = http.Get("https://api.github.com/user?access_token=" + token); err != nil { return diff --git a/task/sync.go b/task/sync.go index d6a1763..6fc2ec4 100644 --- a/task/sync.go +++ b/task/sync.go @@ -1,11 +1,14 @@ package task import ( + "time" + "github.com/google/go-github/github" "github.com/localhots/empact/db" ) func SyncRepos(token, owner string) { + report("SyncRepos", time.Now()) client := newGithubClient(token) opt := &github.RepositoryListByOrgOptions{ ListOptions: github.ListOptions{}, @@ -32,6 +35,7 @@ func SyncRepos(token, owner string) { } func SyncContrib(token, owner, repo string) { + report("SyncContrib", time.Now()) client := newGithubClient(token) contribs, resp, err := client.Repositories.ListContributorsStats(owner, repo) saveResponseMeta(token, resp) diff --git a/task/common.go b/task/task.go similarity index 66% rename from task/common.go rename to task/task.go index 0e79bdc..871373e 100644 --- a/task/common.go +++ b/task/task.go @@ -1,6 +1,9 @@ package task import ( + "log" + "time" + "code.google.com/p/goauth2/oauth" "github.com/google/go-github/github" "github.com/localhots/empact/db" @@ -25,3 +28,13 @@ func saveResponseMeta(token string, res *github.Response) { } tok.Save() } + +func report(task string, start time.Time) { + duration := time.Since(start).Nanoseconds() + outcome := "done" + if err := recover(); err != nil { + outcome = "failed" + } + + log.Printf("Task %s %s; time: %d (%dms)\n", task, outcome, duration, duration/1000000) +}