1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

chore: move route registration into the handler classes themselves (resolve #57)

This commit is contained in:
Ferdinand Mütsch
2021-01-30 10:34:52 +01:00
parent a6aff07b21
commit 417d4789ab
12 changed files with 109 additions and 39 deletions

View File

@@ -31,6 +31,12 @@ func NewBadgeHandler(summaryService services.ISummaryService, userService servic
}
}
func (h *BadgeHandler) RegisterRoutes(router *mux.Router) {}
func (h *BadgeHandler) RegisterAPIRoutes(router *mux.Router) {
router.Methods(http.MethodGet).HandlerFunc(h.ApiGet)
}
func (h *BadgeHandler) ApiGet(w http.ResponseWriter, r *http.Request) {
intervalReg := regexp.MustCompile(intervalPattern)
entityFilterReg := regexp.MustCompile(entityFilterPattern)

View File

@@ -24,6 +24,12 @@ func NewAllTimeHandler(summaryService services.ISummaryService) *AllTimeHandler
}
}
func (h *AllTimeHandler) RegisterRoutes(router *mux.Router) {}
func (h *AllTimeHandler) RegisterAPIRoutes(router *mux.Router) {
router.Path("/all_time_since_today").Methods(http.MethodGet).HandlerFunc(h.ApiGet)
}
func (h *AllTimeHandler) ApiGet(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
values, _ := url.ParseQuery(r.URL.RawQuery)

View File

@@ -25,6 +25,12 @@ func NewSummariesHandler(summaryService services.ISummaryService) *SummariesHand
}
}
func (h *SummariesHandler) RegisterRoutes(router *mux.Router) {}
func (h *SummariesHandler) RegisterAPIRoutes(router *mux.Router) {
router.Path("/summaries").Methods(http.MethodGet).HandlerFunc(h.ApiGet)
}
/*
TODO: support parameters: project, branches, timeout, writes_only, timezone
https://wakatime.com/developers#summaries

8
routes/handler.go Normal file
View File

@@ -0,0 +1,8 @@
package routes
import "github.com/gorilla/mux"
type Handler interface {
RegisterRoutes(router *mux.Router)
RegisterAPIRoutes(router *mux.Router)
}

View File

@@ -2,6 +2,7 @@ package routes
import (
"fmt"
"github.com/gorilla/mux"
"gorm.io/gorm"
"net/http"
)
@@ -14,6 +15,12 @@ func NewHealthHandler(db *gorm.DB) *HealthHandler {
return &HealthHandler{db: db}
}
func (h *HealthHandler) RegisterRoutes(router *mux.Router) {}
func (h *HealthHandler) RegisterAPIRoutes(router *mux.Router) {
router.Methods(http.MethodGet).HandlerFunc(h.ApiGet)
}
func (h *HealthHandler) ApiGet(w http.ResponseWriter, r *http.Request) {
var dbStatus int
if sqlDb, err := h.db.DB(); err == nil {

View File

@@ -2,6 +2,7 @@ package routes
import (
"encoding/json"
"github.com/gorilla/mux"
conf "github.com/muety/wakapi/config"
"net/http"
"os"
@@ -30,6 +31,12 @@ type heartbeatResponseVm struct {
Responses [][]interface{} `json:"responses"`
}
func (h *HeartbeatHandler) RegisterRoutes(router *mux.Router) {}
func (h *HeartbeatHandler) RegisterAPIRoutes(router *mux.Router) {
router.Methods(http.MethodPost).HandlerFunc(h.ApiPost)
}
func (h *HeartbeatHandler) ApiPost(w http.ResponseWriter, r *http.Request) {
var heartbeats []*models.Heartbeat
user := r.Context().Value(models.UserKey).(*models.User)

View File

@@ -2,6 +2,7 @@ package routes
import (
"fmt"
"github.com/gorilla/mux"
"github.com/gorilla/schema"
conf "github.com/muety/wakapi/config"
"github.com/muety/wakapi/models"
@@ -27,6 +28,12 @@ func NewHomeHandler(keyValueService services.IKeyValueService) *HomeHandler {
}
}
func (h *HomeHandler) RegisterRoutes(router *mux.Router) {
router.Path("/").Methods(http.MethodGet).HandlerFunc(h.GetIndex)
}
func (h *HomeHandler) RegisterAPIRoutes(router *mux.Router) {}
func (h *HomeHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
if h.config.IsDev() {
loadTemplates()

View File

@@ -1,6 +1,7 @@
package routes
import (
"github.com/gorilla/mux"
conf "github.com/muety/wakapi/config"
"github.com/muety/wakapi/models"
"github.com/muety/wakapi/models/view"
@@ -20,6 +21,12 @@ func NewImprintHandler(keyValueService services.IKeyValueService) *ImprintHandle
}
}
func (h *ImprintHandler) RegisterRoutes(router *mux.Router) {
router.Path("/imprint").Methods(http.MethodGet).HandlerFunc(h.GetImprint)
}
func (h *ImprintHandler) RegisterAPIRoutes(router *mux.Router) {}
func (h *ImprintHandler) GetImprint(w http.ResponseWriter, r *http.Request) {
if h.config.IsDev() {
loadTemplates()

View File

@@ -2,6 +2,7 @@ package routes
import (
"fmt"
"github.com/gorilla/mux"
conf "github.com/muety/wakapi/config"
"github.com/muety/wakapi/middlewares"
"github.com/muety/wakapi/models"
@@ -23,6 +24,16 @@ func NewLoginHandler(userService services.IUserService) *LoginHandler {
}
}
func (h *LoginHandler) RegisterRoutes(router *mux.Router) {
router.Path("/login").Methods(http.MethodGet).HandlerFunc(h.GetIndex)
router.Path("/login").Methods(http.MethodPost).HandlerFunc(h.PostLogin)
router.Path("/logout").Methods(http.MethodPost).HandlerFunc(h.PostLogout)
router.Path("/signup").Methods(http.MethodGet).HandlerFunc(h.GetSignup)
router.Path("/signup").Methods(http.MethodPost).HandlerFunc(h.PostSignup)
}
func (h *LoginHandler) RegisterAPIRoutes(router *mux.Router) {}
func (h *LoginHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
if h.config.IsDev() {
loadTemplates()

View File

@@ -2,6 +2,7 @@ package routes
import (
"fmt"
"github.com/gorilla/mux"
"github.com/gorilla/schema"
conf "github.com/muety/wakapi/config"
"github.com/muety/wakapi/models"
@@ -35,6 +36,21 @@ func NewSettingsHandler(userService services.IUserService, summaryService servic
}
}
func (h *SettingsHandler) RegisterRoutes(router *mux.Router) {
router.Methods(http.MethodGet).HandlerFunc(h.GetIndex)
router.Path("/credentials").Methods(http.MethodPost).HandlerFunc(h.PostCredentials)
router.Path("/aliases").Methods(http.MethodPost).HandlerFunc(h.PostAlias)
router.Path("/aliases/delete").Methods(http.MethodPost).HandlerFunc(h.DeleteAlias)
router.Path("/language_mappings").Methods(http.MethodPost).HandlerFunc(h.PostLanguageMapping)
router.Path("/language_mappings/delete").Methods(http.MethodPost).HandlerFunc(h.DeleteLanguageMapping)
router.Path("/reset").Methods(http.MethodPost).HandlerFunc(h.PostResetApiKey)
router.Path("/badges").Methods(http.MethodPost).HandlerFunc(h.PostToggleBadges)
router.Path("/wakatime_integration").Methods(http.MethodPost).HandlerFunc(h.PostSetWakatimeApiKey)
router.Path("/regenerate").Methods(http.MethodPost).HandlerFunc(h.PostRegenerateSummaries)
}
func (h *SettingsHandler) RegisterAPIRoutes(router *mux.Router) {}
func (h *SettingsHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
if h.config.IsDev() {
loadTemplates()

View File

@@ -1,6 +1,7 @@
package routes
import (
"github.com/gorilla/mux"
conf "github.com/muety/wakapi/config"
"github.com/muety/wakapi/models"
"github.com/muety/wakapi/models/view"
@@ -21,6 +22,14 @@ func NewSummaryHandler(summaryService services.ISummaryService) *SummaryHandler
}
}
func (h *SummaryHandler) RegisterRoutes(router *mux.Router) {
router.Methods(http.MethodGet).HandlerFunc(h.GetIndex)
}
func (h *SummaryHandler) RegisterAPIRoutes(router *mux.Router) {
router.Methods(http.MethodGet).HandlerFunc(h.ApiGet)
}
func (h *SummaryHandler) ApiGet(w http.ResponseWriter, r *http.Request) {
summary, err, status := h.loadUserSummary(r)
if err != nil {