mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
Minor code cleanup.
This commit is contained in:
parent
121b8c9885
commit
fe1cc3ac88
18
main.go
18
main.go
@ -105,11 +105,11 @@ func main() {
|
|||||||
migrateLanguages(db, config)
|
migrateLanguages(db, config)
|
||||||
|
|
||||||
// Services
|
// Services
|
||||||
aliasSrvc := &services.AliasService{config, db}
|
aliasSrvc := &services.AliasService{Config: config, Db: db}
|
||||||
heartbeatSrvc := &services.HeartbeatService{config, db}
|
heartbeatSrvc := &services.HeartbeatService{Config: config, Db: db}
|
||||||
userSrvc := &services.UserService{config, db}
|
userSrvc := &services.UserService{Config: config, Db: db}
|
||||||
summarySrvc := &services.SummaryService{config, db, heartbeatSrvc, aliasSrvc}
|
summarySrvc := &services.SummaryService{Config: config, Db: db, HeartbeatService: heartbeatSrvc, AliasService: aliasSrvc}
|
||||||
aggregationSrvc := &services.AggregationService{config, db, userSrvc, summarySrvc, heartbeatSrvc}
|
aggregationSrvc := &services.AggregationService{Config: config, Db: db, UserService: userSrvc, SummaryService: summarySrvc, HeartbeatService: heartbeatSrvc}
|
||||||
|
|
||||||
// Aggregate heartbeats to summaries and persist them
|
// Aggregate heartbeats to summaries and persist them
|
||||||
go aggregationSrvc.Schedule()
|
go aggregationSrvc.Schedule()
|
||||||
@ -119,8 +119,8 @@ func main() {
|
|||||||
summaryHandler := &routes.SummaryHandler{SummarySrvc: summarySrvc}
|
summaryHandler := &routes.SummaryHandler{SummarySrvc: summarySrvc}
|
||||||
|
|
||||||
// Middlewares
|
// Middlewares
|
||||||
authenticate := &middlewares.AuthenticateMiddleware{UserSrvc: userSrvc}
|
authenticateMiddleware := &middlewares.AuthenticateMiddleware{UserSrvc: userSrvc}
|
||||||
cors := cors.New(cors.Options{
|
corsMiddleware := cors.New(cors.Options{
|
||||||
AllowedOrigins: []string{"*"},
|
AllowedOrigins: []string{"*"},
|
||||||
AllowedHeaders: []string{"*"},
|
AllowedHeaders: []string{"*"},
|
||||||
Debug: false,
|
Debug: false,
|
||||||
@ -139,9 +139,9 @@ func main() {
|
|||||||
|
|
||||||
// Sub-Routes Setup
|
// Sub-Routes Setup
|
||||||
router.PathPrefix("/api").Handler(negroni.Classic().
|
router.PathPrefix("/api").Handler(negroni.Classic().
|
||||||
With(cors).
|
With(corsMiddleware).
|
||||||
With(
|
With(
|
||||||
negroni.HandlerFunc(authenticate.Handle),
|
negroni.HandlerFunc(authenticateMiddleware.Handle),
|
||||||
negroni.Wrap(apiRouter),
|
negroni.Wrap(apiRouter),
|
||||||
))
|
))
|
||||||
router.PathPrefix("/").Handler(negroni.Classic().With(negroni.Wrap(http.FileServer(http.Dir("./static")))))
|
router.PathPrefix("/").Handler(negroni.Classic().With(negroni.Wrap(http.FileServer(http.Dir("./static")))))
|
||||||
|
@ -37,10 +37,10 @@ func (m *AuthenticateMiddleware) Handle(w http.ResponseWriter, r *http.Request,
|
|||||||
|
|
||||||
var user *models.User
|
var user *models.User
|
||||||
var userKey string
|
var userKey string
|
||||||
user, userKey, err := m.tryGetUserByApiKey(r)
|
user, userKey, err := m.tryGetUserByPassword(r)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
user, userKey, err = m.tryGetUserByPassword(r)
|
user, userKey, err = m.tryGetUserByApiKey(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -57,7 +57,7 @@ func (m *AuthenticateMiddleware) Handle(w http.ResponseWriter, r *http.Request,
|
|||||||
func (m *AuthenticateMiddleware) tryGetUserByApiKey(r *http.Request) (*models.User, string, error) {
|
func (m *AuthenticateMiddleware) tryGetUserByApiKey(r *http.Request) (*models.User, string, error) {
|
||||||
authHeader := strings.Split(r.Header.Get("Authorization"), " ")
|
authHeader := strings.Split(r.Header.Get("Authorization"), " ")
|
||||||
if len(authHeader) != 2 || authHeader[0] != "Basic" {
|
if len(authHeader) != 2 || authHeader[0] != "Basic" {
|
||||||
return nil, "", errors.New("Failed to extract API key")
|
return nil, "", errors.New("failed to extract API key")
|
||||||
}
|
}
|
||||||
|
|
||||||
key, err := base64.StdEncoding.DecodeString(authHeader[1])
|
key, err := base64.StdEncoding.DecodeString(authHeader[1])
|
||||||
@ -82,7 +82,7 @@ func (m *AuthenticateMiddleware) tryGetUserByApiKey(r *http.Request) (*models.Us
|
|||||||
func (m *AuthenticateMiddleware) tryGetUserByPassword(r *http.Request) (*models.User, string, error) {
|
func (m *AuthenticateMiddleware) tryGetUserByPassword(r *http.Request) (*models.User, string, error) {
|
||||||
authHeader := strings.Split(r.Header.Get("Authorization"), " ")
|
authHeader := strings.Split(r.Header.Get("Authorization"), " ")
|
||||||
if len(authHeader) != 2 || authHeader[0] != "Basic" {
|
if len(authHeader) != 2 || authHeader[0] != "Basic" {
|
||||||
return nil, "", errors.New("Failed to extract API key")
|
return nil, "", errors.New("failed to extract API key")
|
||||||
}
|
}
|
||||||
|
|
||||||
hash, err := base64.StdEncoding.DecodeString(authHeader[1])
|
hash, err := base64.StdEncoding.DecodeString(authHeader[1])
|
||||||
@ -97,7 +97,7 @@ func (m *AuthenticateMiddleware) tryGetUserByPassword(r *http.Request) (*models.
|
|||||||
re := regexp.MustCompile(`^(.+):(.+)$`)
|
re := regexp.MustCompile(`^(.+):(.+)$`)
|
||||||
groups := re.FindAllStringSubmatch(userKey, -1)
|
groups := re.FindAllStringSubmatch(userKey, -1)
|
||||||
if len(groups) == 0 || len(groups[0]) != 3 {
|
if len(groups) == 0 || len(groups[0]) != 3 {
|
||||||
return nil, "", errors.New("Failed to parse user agent string")
|
return nil, "", errors.New("failed to parse user agent string")
|
||||||
}
|
}
|
||||||
userId, password := groups[0][1], groups[0][2]
|
userId, password := groups[0][1], groups[0][2]
|
||||||
user, err = m.UserSrvc.GetUserById(userId)
|
user, err = m.UserSrvc.GetUserById(userId)
|
||||||
@ -107,7 +107,7 @@ func (m *AuthenticateMiddleware) tryGetUserByPassword(r *http.Request) (*models.
|
|||||||
passwordHash := md5.Sum([]byte(password))
|
passwordHash := md5.Sum([]byte(password))
|
||||||
passwordHashString := hex.EncodeToString(passwordHash[:])
|
passwordHashString := hex.EncodeToString(passwordHash[:])
|
||||||
if passwordHashString != user.Password {
|
if passwordHashString != user.Password {
|
||||||
return nil, "", errors.New("Invalid password")
|
return nil, "", errors.New("invalid password")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
user = cachedUser.(*models.User)
|
user = cachedUser.(*models.User)
|
||||||
|
@ -69,7 +69,7 @@ func (j *HeartbeatReqTime) Scan(value interface{}) error {
|
|||||||
*j = HeartbeatReqTime(value.(time.Time))
|
*j = HeartbeatReqTime(value.(time.Time))
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
return errors.New("Unsupported type")
|
return errors.New("unsupported type")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -62,13 +62,13 @@ func (h *SummaryHandler) Get(w http.ResponseWriter, r *http.Request) {
|
|||||||
from = utils.StartOfYear()
|
from = utils.StartOfYear()
|
||||||
default:
|
default:
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
w.Write([]byte("Missing 'from' parameter"))
|
w.Write([]byte("missing 'from' parameter"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
live := (params.Get("live") != "" && params.Get("live") != "false") || interval == IntervalToday
|
live := (params.Get("live") != "" && params.Get("live") != "false") || interval == IntervalToday
|
||||||
recompute := (params.Get("recompute") != "" && params.Get("recompute") != "false")
|
recompute := params.Get("recompute") != "" && params.Get("recompute") != "false"
|
||||||
to := utils.StartOfDay()
|
to := utils.StartOfDay()
|
||||||
if live {
|
if live {
|
||||||
to = time.Now()
|
to = time.Now()
|
||||||
|
@ -36,7 +36,7 @@ func (srv *AliasService) GetAliasOrDefault(userId string, summaryType uint8, val
|
|||||||
}
|
}
|
||||||
return value, nil
|
return value, nil
|
||||||
}
|
}
|
||||||
return "", errors.New("User aliases not initialized")
|
return "", errors.New("user aliases not initialized")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (src *AliasService) IsInitialized(userId string) bool {
|
func (src *AliasService) IsInitialized(userId string) bool {
|
||||||
|
@ -188,7 +188,7 @@ func (srv *SummaryService) aggregateBy(heartbeats []*models.Heartbeat, summaryTy
|
|||||||
|
|
||||||
func getMissingIntervals(from, to time.Time, existingSummaries []*models.Summary) []*Interval {
|
func getMissingIntervals(from, to time.Time, existingSummaries []*models.Summary) []*Interval {
|
||||||
if len(existingSummaries) == 0 {
|
if len(existingSummaries) == 0 {
|
||||||
return []*Interval{&Interval{from, to}}
|
return []*Interval{{from, to}}
|
||||||
}
|
}
|
||||||
|
|
||||||
intervals := make([]*Interval, 0)
|
intervals := make([]*Interval, 0)
|
||||||
|
@ -22,7 +22,7 @@ func ParseUserAgent(ua string) (string, string, error) {
|
|||||||
re := regexp.MustCompile(`^wakatime\/[\d+.]+\s\((\w+).*\)\s.+\s(\w+)\/.+$`)
|
re := regexp.MustCompile(`^wakatime\/[\d+.]+\s\((\w+).*\)\s.+\s(\w+)\/.+$`)
|
||||||
groups := re.FindAllStringSubmatch(ua, -1)
|
groups := re.FindAllStringSubmatch(ua, -1)
|
||||||
if len(groups) == 0 || len(groups[0]) != 3 {
|
if len(groups) == 0 || len(groups[0]) != 3 {
|
||||||
return "", "", errors.New("Failed to parse user agent string")
|
return "", "", errors.New("failed to parse user agent string")
|
||||||
}
|
}
|
||||||
return groups[0][1], groups[0][2], nil
|
return groups[0][1], groups[0][2], nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user