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

58
main.go
View File

@ -130,9 +130,12 @@ func main() {
settingsRouter := publicRouter.PathPrefix("/settings").Subrouter()
summaryRouter := publicRouter.PathPrefix("/summary").Subrouter()
apiRouter := router.PathPrefix("/api").Subrouter()
summaryApiRouter := apiRouter.PathPrefix("/summary").Subrouter()
heartbeatApiRouter := apiRouter.PathPrefix("/heartbeat").Subrouter()
healthApiRouter := apiRouter.PathPrefix("/health").Subrouter()
compatRouter := apiRouter.PathPrefix("/compat").Subrouter()
wakatimeV1Router := compatRouter.PathPrefix("/wakatime/v1").Subrouter()
shieldsV1Router := compatRouter.PathPrefix("/shields/v1").Subrouter()
wakatimeV1Router := compatRouter.PathPrefix("/wakatime/v1/users/{user}").Subrouter()
shieldsV1Router := compatRouter.PathPrefix("/shields/v1/{user}").Subrouter()
// Middlewares
recoveryMiddleware := handlers.RecoveryHandler()
@ -149,45 +152,22 @@ func main() {
summaryRouter.Use(authenticateMiddleware)
settingsRouter.Use(authenticateMiddleware)
apiRouter.Use(corsMiddleware, authenticateMiddleware)
heartbeatApiRouter.Use(wakatimeRelayMiddleware)
// Public Routes
publicRouter.Path("/").Methods(http.MethodGet).HandlerFunc(homeHandler.GetIndex)
publicRouter.Path("/login").Methods(http.MethodGet).HandlerFunc(loginHandler.GetIndex)
publicRouter.Path("/login").Methods(http.MethodPost).HandlerFunc(loginHandler.PostLogin)
publicRouter.Path("/logout").Methods(http.MethodPost).HandlerFunc(loginHandler.PostLogout)
publicRouter.Path("/signup").Methods(http.MethodGet).HandlerFunc(loginHandler.GetSignup)
publicRouter.Path("/signup").Methods(http.MethodPost).HandlerFunc(loginHandler.PostSignup)
publicRouter.Path("/imprint").Methods(http.MethodGet).HandlerFunc(imprintHandler.GetImprint)
// Route registrations
homeHandler.RegisterRoutes(publicRouter)
loginHandler.RegisterRoutes(publicRouter)
imprintHandler.RegisterRoutes(publicRouter)
summaryHandler.RegisterRoutes(summaryRouter)
settingsHandler.RegisterRoutes(settingsRouter)
// Summary Routes
summaryRouter.Methods(http.MethodGet).HandlerFunc(summaryHandler.GetIndex)
// Settings Routes
settingsRouter.Methods(http.MethodGet).HandlerFunc(settingsHandler.GetIndex)
settingsRouter.Path("/credentials").Methods(http.MethodPost).HandlerFunc(settingsHandler.PostCredentials)
settingsRouter.Path("/aliases").Methods(http.MethodPost).HandlerFunc(settingsHandler.PostAlias)
settingsRouter.Path("/aliases/delete").Methods(http.MethodPost).HandlerFunc(settingsHandler.DeleteAlias)
settingsRouter.Path("/language_mappings").Methods(http.MethodPost).HandlerFunc(settingsHandler.PostLanguageMapping)
settingsRouter.Path("/language_mappings/delete").Methods(http.MethodPost).HandlerFunc(settingsHandler.DeleteLanguageMapping)
settingsRouter.Path("/reset").Methods(http.MethodPost).HandlerFunc(settingsHandler.PostResetApiKey)
settingsRouter.Path("/badges").Methods(http.MethodPost).HandlerFunc(settingsHandler.PostToggleBadges)
settingsRouter.Path("/wakatime_integration").Methods(http.MethodPost).HandlerFunc(settingsHandler.PostSetWakatimeApiKey)
settingsRouter.Path("/regenerate").Methods(http.MethodPost).HandlerFunc(settingsHandler.PostRegenerateSummaries)
// API Routes
apiRouter.Path("/summary").Methods(http.MethodGet).HandlerFunc(summaryHandler.ApiGet)
apiRouter.Path("/health").Methods(http.MethodGet).HandlerFunc(healthHandler.ApiGet)
heartbeatsApiRouter := apiRouter.Path("/heartbeat").Methods(http.MethodPost).Subrouter()
heartbeatsApiRouter.Use(wakatimeRelayMiddleware)
heartbeatsApiRouter.Path("").HandlerFunc(heartbeatHandler.ApiPost)
// Wakatime compat V1 API Routes
wakatimeV1Router.Path("/users/{user}/all_time_since_today").Methods(http.MethodGet).HandlerFunc(wakatimeV1AllHandler.ApiGet)
wakatimeV1Router.Path("/users/{user}/summaries").Methods(http.MethodGet).HandlerFunc(wakatimeV1SummariesHandler.ApiGet)
// Shields.io compat API Routes
shieldsV1Router.PathPrefix("/{user}").Methods(http.MethodGet).HandlerFunc(shieldV1BadgeHandler.ApiGet)
// API Route registrations
summaryHandler.RegisterAPIRoutes(summaryApiRouter)
healthHandler.RegisterAPIRoutes(healthApiRouter)
heartbeatHandler.RegisterAPIRoutes(heartbeatApiRouter)
wakatimeV1AllHandler.RegisterAPIRoutes(wakatimeV1Router)
wakatimeV1SummariesHandler.RegisterAPIRoutes(wakatimeV1Router)
shieldV1BadgeHandler.RegisterAPIRoutes(shieldsV1Router)
// Static Routes
router.PathPrefix("/assets").Handler(http.FileServer(pkger.Dir("/static")))

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 {