Logging, reports
This commit is contained in:
parent
9f36068c54
commit
9251ad8c6d
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -29,23 +30,23 @@ func C() Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
var (
|
var err error
|
||||||
path string
|
var path string
|
||||||
fd *os.File
|
|
||||||
contents []byte
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
|
|
||||||
flag.StringVar(&path, "config", "config.json", "Path to configuration file")
|
flag.StringVar(&path, "config", "config.json", "Path to configuration file")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
var fd *os.File
|
||||||
if fd, err = os.Open(path); err != nil {
|
if fd, err = os.Open(path); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
var contents []byte
|
||||||
if contents, err = ioutil.ReadAll(fd); err != nil {
|
if contents, err = ioutil.ReadAll(fd); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if err = json.Unmarshal(contents, &conf); err != nil {
|
if err = json.Unmarshal(contents, &conf); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.SetOutput(os.Stderr)
|
||||||
|
log.SetFlags(log.Ltime | log.Lshortfile)
|
||||||
}
|
}
|
||||||
|
|
4
db/db.go
4
db/db.go
|
@ -1,8 +1,6 @@
|
||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
|
||||||
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
"github.com/jmoiron/sqlx/reflectx"
|
"github.com/jmoiron/sqlx/reflectx"
|
||||||
|
@ -14,7 +12,7 @@ var (
|
||||||
|
|
||||||
func Connect(params string) (err error) {
|
func Connect(params string) (err error) {
|
||||||
db, err = sqlx.Connect("mysql", params)
|
db, err = sqlx.Connect("mysql", params)
|
||||||
db.Mapper = reflectx.NewMapperFunc("json", strings.ToLower)
|
db.Mapper = reflectx.NewMapper("json")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,8 +10,8 @@ type Token struct {
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
Quota uint64 `json:"quota"`
|
Quota uint64 `json:"quota"`
|
||||||
Remaining uint64 `json:"remaining"`
|
Remaining uint64 `json:"remaining"`
|
||||||
ResetAt time.Time `json:"reset_at"`
|
ResetAt time.Time `json:"reset_at" db:"reset_at"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const saveTokenQuery = `
|
const saveTokenQuery = `
|
||||||
|
|
|
@ -4,7 +4,7 @@ type User struct {
|
||||||
Login string `json:"login"`
|
Login string `json:"login"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
ID uint64 `json:"id"`
|
ID uint64 `json:"id"`
|
||||||
AvatarURL string `json:"avatar_url"`
|
AvatarURL string `json:"avatar_url" db:"avatar_url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const saveUserQuery = `
|
const saveUserQuery = `
|
||||||
|
|
|
@ -24,7 +24,7 @@ func authCallbackHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
code := r.FormValue("code")
|
code := r.FormValue("code")
|
||||||
log.Println("Got code: ", code)
|
log.Printf("Got code %q\n", code)
|
||||||
if _, login, err := task.Authenticate(code); err == nil {
|
if _, login, err := task.Authenticate(code); err == nil {
|
||||||
createSession(r, login)
|
createSession(r, login)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -2,8 +2,8 @@ package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/GeertJohan/go.rice"
|
"github.com/GeertJohan/go.rice"
|
||||||
|
@ -34,7 +34,7 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Start() {
|
func Start() {
|
||||||
fmt.Println("Starting server at http://localhost:8080")
|
log.Println("Starting server at http://localhost:8080")
|
||||||
http.ListenAndServe(":8080", nil)
|
http.ListenAndServe(":8080", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
13
task/auth.go
13
task/auth.go
|
@ -5,45 +5,49 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/localhots/empact/config"
|
"github.com/localhots/empact/config"
|
||||||
"github.com/localhots/empact/db"
|
"github.com/localhots/empact/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Authenticate(code string) (token, login string, err error) {
|
func Authenticate(code string) (token, login string, err error) {
|
||||||
|
report("Authenticate", time.Now())
|
||||||
if token, err = FetchAccessToken(code); err != nil {
|
if token, err = FetchAccessToken(code); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println("Got token: " + token)
|
log.Printf("Got token %q for code %q\n", token, code)
|
||||||
|
|
||||||
var user *db.User
|
var user *db.User
|
||||||
if user, err = FetchUserInfoWithToken(token); err != nil {
|
if user, err = FetchUserInfoWithToken(token); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
login = user.Login
|
login = user.Login
|
||||||
fmt.Println("Saving user", user)
|
log.Println("Saving user", user)
|
||||||
user.Save()
|
user.Save()
|
||||||
|
|
||||||
tok := &db.Token{
|
tok := &db.Token{
|
||||||
User: login,
|
User: login,
|
||||||
Token: token,
|
Token: token,
|
||||||
}
|
}
|
||||||
fmt.Println("Saving token", tok)
|
log.Println("Saving token", tok)
|
||||||
tok.Save()
|
tok.Save()
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func FetchAccessToken(code string) (token string, err error) {
|
func FetchAccessToken(code string) (token string, err error) {
|
||||||
|
report("FetchAccessToken", time.Now())
|
||||||
payload := url.Values{}
|
payload := url.Values{}
|
||||||
payload.Set("client_id", config.C().ClientID)
|
payload.Set("client_id", config.C().ClientID)
|
||||||
payload.Set("client_secret", config.C().ClientSecret)
|
payload.Set("client_secret", config.C().ClientSecret)
|
||||||
payload.Set("code", code)
|
payload.Set("code", code)
|
||||||
payload.Set("redirect_uri", config.C().RedirectURI)
|
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()))
|
buf := bytes.NewBuffer([]byte(payload.Encode()))
|
||||||
var resp *http.Response
|
var resp *http.Response
|
||||||
if resp, err = http.Post(config.C().AccessTokenURL, "application/x-www-form-urlencoded", buf); err != nil {
|
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) {
|
func FetchUserInfoWithToken(token string) (u *db.User, err error) {
|
||||||
|
report("FetchUserInfoWithToken", time.Now())
|
||||||
var resp *http.Response
|
var resp *http.Response
|
||||||
if resp, err = http.Get("https://api.github.com/user?access_token=" + token); err != nil {
|
if resp, err = http.Get("https://api.github.com/user?access_token=" + token); err != nil {
|
||||||
return
|
return
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
package task
|
package task
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/google/go-github/github"
|
"github.com/google/go-github/github"
|
||||||
"github.com/localhots/empact/db"
|
"github.com/localhots/empact/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SyncRepos(token, owner string) {
|
func SyncRepos(token, owner string) {
|
||||||
|
report("SyncRepos", time.Now())
|
||||||
client := newGithubClient(token)
|
client := newGithubClient(token)
|
||||||
opt := &github.RepositoryListByOrgOptions{
|
opt := &github.RepositoryListByOrgOptions{
|
||||||
ListOptions: github.ListOptions{},
|
ListOptions: github.ListOptions{},
|
||||||
|
@ -32,6 +35,7 @@ func SyncRepos(token, owner string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func SyncContrib(token, owner, repo string) {
|
func SyncContrib(token, owner, repo string) {
|
||||||
|
report("SyncContrib", time.Now())
|
||||||
client := newGithubClient(token)
|
client := newGithubClient(token)
|
||||||
contribs, resp, err := client.Repositories.ListContributorsStats(owner, repo)
|
contribs, resp, err := client.Repositories.ListContributorsStats(owner, repo)
|
||||||
saveResponseMeta(token, resp)
|
saveResponseMeta(token, resp)
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package task
|
package task
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
"code.google.com/p/goauth2/oauth"
|
"code.google.com/p/goauth2/oauth"
|
||||||
"github.com/google/go-github/github"
|
"github.com/google/go-github/github"
|
||||||
"github.com/localhots/empact/db"
|
"github.com/localhots/empact/db"
|
||||||
|
@ -25,3 +28,13 @@ func saveResponseMeta(token string, res *github.Response) {
|
||||||
}
|
}
|
||||||
tok.Save()
|
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)
|
||||||
|
}
|
Loading…
Reference in New Issue