Add --debug flag
This commit is contained in:
parent
fd8d2ec43e
commit
bad864ad86
15
database.go
15
database.go
|
@ -5,7 +5,6 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"time"
|
||||
|
@ -87,8 +86,8 @@ func (d *Database) authenticate(token string) (*Account, error) {
|
|||
}
|
||||
|
||||
if googleid == "" || googleid == "0" {
|
||||
log.Printf("Userinfo: %+v", userinfo)
|
||||
log.Printf("Access token: %+v", googleid)
|
||||
logDebugf("Userinfo: %+v", userinfo)
|
||||
logDebugf("Access token: %+v", googleid)
|
||||
return nil, errors.Wrap(err, "invalid access token")
|
||||
}
|
||||
|
||||
|
@ -177,10 +176,10 @@ func (d *Database) updateTopStreak(accountid int) error {
|
|||
|
||||
func (d *Database) calculateStreak(accountid int, tz *time.Location) (int, error) {
|
||||
t := time.Now().In(tz)
|
||||
log.Printf("caluclate start %v", t)
|
||||
logDebugf("caluclate start %v", t)
|
||||
if t.Hour() < 4 {
|
||||
t = t.AddDate(0, 0, -1)
|
||||
log.Printf("caluclate added %v", t)
|
||||
logDebugf("caluclate added %v", t)
|
||||
}
|
||||
|
||||
streak := 0
|
||||
|
@ -196,7 +195,7 @@ func (d *Database) calculateStreak(accountid int, tz *time.Location) (int, error
|
|||
}
|
||||
}
|
||||
|
||||
log.Printf("Calculated streak as %d", streak)
|
||||
logDebugf("Calculated streak as %d", streak)
|
||||
return streak, nil
|
||||
}
|
||||
|
||||
|
@ -205,7 +204,7 @@ func (d *Database) setStreak(streakday int, accountid int, tz *time.Location) er
|
|||
t = midnight(t.AddDate(0, 0, 2))
|
||||
t = t.Add(STREAK_BUFFER * time.Second)
|
||||
|
||||
log.Printf("SETTING STREAK Account %d, Day %d, TZ %s, Streak end: %d", accountid, streakday, tz.String(), t.Unix())
|
||||
logDebugf("SETTING STREAK Account %d, Day %d, TZ %s, Streak end: %d", accountid, streakday, tz.String(), t.Unix())
|
||||
|
||||
_, err := d.db.Exec("UPDATE accounts SET `streak`=?, `streakend`=? WHERE `id`=?", streakday, t.Unix(), accountid)
|
||||
if err != nil {
|
||||
|
@ -278,7 +277,7 @@ func (d *Database) sessionExistsForDate(date time.Time, accountid int) (bool, er
|
|||
sessionid := 0
|
||||
date = midnight(date).Add(STREAK_BUFFER * time.Second)
|
||||
|
||||
log.Printf("SESSION EXISTS %v - START %v END %v", date, date.Unix(), date.AddDate(0, 0, 1).Unix())
|
||||
logDebugf("SESSION EXISTS %v - START %v END %v", date, date.Unix(), date.AddDate(0, 0, 1).Unix())
|
||||
|
||||
err := d.db.QueryRow("SELECT `id` FROM sessions WHERE `account`=? AND `started`>=? AND `started`<? LIMIT 1", accountid, date.Unix(), date.AddDate(0, 0, 1).Unix()).Scan(&sessionid)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
|
|
23
main.go
23
main.go
|
@ -35,6 +35,8 @@ import (
|
|||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var PrintDebug bool
|
||||
|
||||
type Config struct {
|
||||
Om int
|
||||
SQLHost string
|
||||
|
@ -45,6 +47,16 @@ type Config struct {
|
|||
|
||||
var config *Config
|
||||
|
||||
func logDebug(message string) {
|
||||
if PrintDebug {
|
||||
log.Println(message)
|
||||
}
|
||||
}
|
||||
|
||||
func logDebugf(format string, v ...interface{}) {
|
||||
logDebug(fmt.Sprintf(format, v))
|
||||
}
|
||||
|
||||
func midnight(t time.Time) time.Time {
|
||||
year, month, day := t.Date()
|
||||
return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
|
||||
|
@ -59,6 +71,7 @@ func failOnError(err error) {
|
|||
func main() {
|
||||
var opts struct {
|
||||
ConfigFile string `short:"c" long:"config" description:"Configuration file"`
|
||||
Debug bool `short:"d" long:"debug" description:"Print debug information"`
|
||||
}
|
||||
_, err := flags.Parse(&opts)
|
||||
failOnError(err)
|
||||
|
@ -81,6 +94,8 @@ func main() {
|
|||
log.Fatal("Specify Om port in configuration file")
|
||||
}
|
||||
|
||||
PrintDebug = opts.Debug
|
||||
|
||||
d, err := NewDatabase(config.SQLHost, config.SQLUser, config.SQLPassword, config.SQLDatabase)
|
||||
failOnError(err)
|
||||
|
||||
|
@ -140,7 +155,7 @@ func main() {
|
|||
}
|
||||
if a == nil {
|
||||
w.Header().Set("x-MediNET", "signin")
|
||||
log.Printf("Asking to sign in %s %q", key, html.EscapeString(r.URL.RequestURI()))
|
||||
logDebugf("Asking to sign in %s %q", key, html.EscapeString(r.URL.RequestURI()))
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -266,7 +281,7 @@ func main() {
|
|||
return
|
||||
}
|
||||
|
||||
log.Printf("NEW SESSION %v - CALCULATED: %d, SUBMITTED: %d", t, streak, streakday)
|
||||
logDebugf("NEW SESSION %v - CALCULATED: %d, SUBMITTED: %d", t, streak, streakday)
|
||||
|
||||
if streak < streakday {
|
||||
streak = streakday
|
||||
|
@ -318,8 +333,8 @@ func main() {
|
|||
return
|
||||
}
|
||||
|
||||
log.Printf("App: %d API: %d Action: %s - %q", appver, apiver, action, html.EscapeString(r.URL.RequestURI()))
|
||||
log.Printf("Account ID: %d, JSON: %s", a.ID, string(j))
|
||||
logDebugf("App: %d API: %d Action: %s - %q", appver, apiver, action, html.EscapeString(r.URL.RequestURI()))
|
||||
logDebugf("Account ID: %d, JSON: %s", a.ID, string(j))
|
||||
})
|
||||
|
||||
log.Fatal(http.ListenAndServe(fmt.Sprintf("localhost:%d", config.Om), nil))
|
||||
|
|
Loading…
Reference in New Issue