mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
refactor: significant changes related to routing and general code cleanup
This commit is contained in:
parent
4838300086
commit
a2368ff76a
78
main.go
78
main.go
@ -9,6 +9,7 @@ import (
|
|||||||
conf "github.com/muety/wakapi/config"
|
conf "github.com/muety/wakapi/config"
|
||||||
"github.com/muety/wakapi/migrations"
|
"github.com/muety/wakapi/migrations"
|
||||||
"github.com/muety/wakapi/repositories"
|
"github.com/muety/wakapi/repositories"
|
||||||
|
"github.com/muety/wakapi/routes/api"
|
||||||
"gorm.io/gorm/logger"
|
"gorm.io/gorm/logger"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -18,7 +19,6 @@ import (
|
|||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/muety/wakapi/middlewares"
|
"github.com/muety/wakapi/middlewares"
|
||||||
customMiddleware "github.com/muety/wakapi/middlewares/custom"
|
|
||||||
"github.com/muety/wakapi/routes"
|
"github.com/muety/wakapi/routes"
|
||||||
shieldsV1Routes "github.com/muety/wakapi/routes/compat/shields/v1"
|
shieldsV1Routes "github.com/muety/wakapi/routes/compat/shields/v1"
|
||||||
wtV1Routes "github.com/muety/wakapi/routes/compat/wakatime/v1"
|
wtV1Routes "github.com/muety/wakapi/routes/compat/wakatime/v1"
|
||||||
@ -122,69 +122,57 @@ func main() {
|
|||||||
go aggregationService.Schedule()
|
go aggregationService.Schedule()
|
||||||
go miscService.ScheduleCountTotalTime()
|
go miscService.ScheduleCountTotalTime()
|
||||||
|
|
||||||
// TODO: move endpoint registration to the respective routes files
|
|
||||||
|
|
||||||
routes.Init()
|
routes.Init()
|
||||||
|
|
||||||
// Handlers
|
// API Handlers
|
||||||
summaryHandler := routes.NewSummaryHandler(summaryService)
|
healthApiHandler := api.NewHealthApiHandler(db)
|
||||||
healthHandler := routes.NewHealthHandler(db)
|
heartbeatApiHandler := api.NewHeartbeatApiHandler(heartbeatService, languageMappingService)
|
||||||
heartbeatHandler := routes.NewHeartbeatHandler(heartbeatService, languageMappingService)
|
summaryApiHandler := api.NewSummaryApiHandler(summaryService)
|
||||||
settingsHandler := routes.NewSettingsHandler(userService, summaryService, aliasService, aggregationService, languageMappingService)
|
|
||||||
homeHandler := routes.NewHomeHandler(keyValueService)
|
// Compat Handlers
|
||||||
loginHandler := routes.NewLoginHandler(userService)
|
|
||||||
imprintHandler := routes.NewImprintHandler(keyValueService)
|
|
||||||
wakatimeV1AllHandler := wtV1Routes.NewAllTimeHandler(summaryService)
|
wakatimeV1AllHandler := wtV1Routes.NewAllTimeHandler(summaryService)
|
||||||
wakatimeV1SummariesHandler := wtV1Routes.NewSummariesHandler(summaryService)
|
wakatimeV1SummariesHandler := wtV1Routes.NewSummariesHandler(summaryService)
|
||||||
shieldV1BadgeHandler := shieldsV1Routes.NewBadgeHandler(summaryService, userService)
|
shieldV1BadgeHandler := shieldsV1Routes.NewBadgeHandler(summaryService, userService)
|
||||||
|
|
||||||
|
// MVC Handlers
|
||||||
|
summaryHandler := routes.NewSummaryHandler(summaryService, userService)
|
||||||
|
settingsHandler := routes.NewSettingsHandler(userService, summaryService, aliasService, aggregationService, languageMappingService)
|
||||||
|
homeHandler := routes.NewHomeHandler(keyValueService)
|
||||||
|
loginHandler := routes.NewLoginHandler(userService)
|
||||||
|
imprintHandler := routes.NewImprintHandler(keyValueService)
|
||||||
|
|
||||||
// Setup Routers
|
// Setup Routers
|
||||||
router := mux.NewRouter()
|
router := mux.NewRouter()
|
||||||
publicRouter := router.PathPrefix("/").Subrouter()
|
rootRouter := router.PathPrefix("/").Subrouter()
|
||||||
settingsRouter := publicRouter.PathPrefix("/settings").Subrouter()
|
|
||||||
summaryRouter := publicRouter.PathPrefix("/summary").Subrouter()
|
|
||||||
apiRouter := router.PathPrefix("/api").Subrouter()
|
apiRouter := router.PathPrefix("/api").Subrouter()
|
||||||
summaryApiRouter := apiRouter.PathPrefix("/summary").Subrouter()
|
compatApiRouter := apiRouter.PathPrefix("/compat").Subrouter()
|
||||||
heartbeatApiRouter := apiRouter.PathPrefix("/heartbeat").Subrouter()
|
|
||||||
healthApiRouter := apiRouter.PathPrefix("/health").Subrouter()
|
|
||||||
compatRouter := apiRouter.PathPrefix("/compat").Subrouter()
|
|
||||||
wakatimeV1Router := compatRouter.PathPrefix("/wakatime/v1/users/{user}").Subrouter()
|
|
||||||
shieldsV1Router := compatRouter.PathPrefix("/shields/v1/{user}").Subrouter()
|
|
||||||
|
|
||||||
// Middlewares
|
// Globally used middlewares
|
||||||
recoveryMiddleware := handlers.RecoveryHandler()
|
recoveryMiddleware := handlers.RecoveryHandler()
|
||||||
loggingMiddleware := middlewares.NewLoggingMiddleware(
|
loggingMiddleware := middlewares.NewLoggingMiddleware(log.New(os.Stdout, "", log.LstdFlags))
|
||||||
// Use logbuch here once https://github.com/emvi/logbuch/issues/4 is realized
|
|
||||||
log.New(os.Stdout, "", log.LstdFlags),
|
|
||||||
)
|
|
||||||
corsMiddleware := handlers.CORS()
|
corsMiddleware := handlers.CORS()
|
||||||
authenticateMiddleware := middlewares.NewAuthenticateMiddleware(
|
authenticateMiddleware := middlewares.NewAuthenticateMiddleware(userService, []string{"/api/health", "/api/compat/shields/v1"}).Handler
|
||||||
userService,
|
|
||||||
[]string{"/api/health", "/api/compat/shields/v1"},
|
|
||||||
).Handler
|
|
||||||
wakatimeRelayMiddleware := customMiddleware.NewWakatimeRelayMiddleware().Handler
|
|
||||||
|
|
||||||
// Router configs
|
// Router configs
|
||||||
router.Use(loggingMiddleware, recoveryMiddleware)
|
router.Use(loggingMiddleware, recoveryMiddleware)
|
||||||
summaryRouter.Use(authenticateMiddleware)
|
|
||||||
settingsRouter.Use(authenticateMiddleware)
|
|
||||||
apiRouter.Use(corsMiddleware, authenticateMiddleware)
|
apiRouter.Use(corsMiddleware, authenticateMiddleware)
|
||||||
heartbeatApiRouter.Use(wakatimeRelayMiddleware)
|
|
||||||
|
|
||||||
// Route registrations
|
// Route registrations
|
||||||
homeHandler.RegisterRoutes(publicRouter)
|
homeHandler.RegisterRoutes(rootRouter)
|
||||||
loginHandler.RegisterRoutes(publicRouter)
|
loginHandler.RegisterRoutes(rootRouter)
|
||||||
imprintHandler.RegisterRoutes(publicRouter)
|
imprintHandler.RegisterRoutes(rootRouter)
|
||||||
summaryHandler.RegisterRoutes(summaryRouter)
|
summaryHandler.RegisterRoutes(rootRouter)
|
||||||
settingsHandler.RegisterRoutes(settingsRouter)
|
settingsHandler.RegisterRoutes(rootRouter)
|
||||||
|
|
||||||
// API Route registrations
|
// API route registrations
|
||||||
summaryHandler.RegisterAPIRoutes(summaryApiRouter)
|
summaryApiHandler.RegisterRoutes(apiRouter)
|
||||||
healthHandler.RegisterAPIRoutes(healthApiRouter)
|
healthApiHandler.RegisterRoutes(apiRouter)
|
||||||
heartbeatHandler.RegisterAPIRoutes(heartbeatApiRouter)
|
heartbeatApiHandler.RegisterRoutes(apiRouter)
|
||||||
wakatimeV1AllHandler.RegisterAPIRoutes(wakatimeV1Router)
|
|
||||||
wakatimeV1SummariesHandler.RegisterAPIRoutes(wakatimeV1Router)
|
// Compat route registrations
|
||||||
shieldV1BadgeHandler.RegisterAPIRoutes(shieldsV1Router)
|
wakatimeV1AllHandler.RegisterRoutes(compatApiRouter)
|
||||||
|
wakatimeV1SummariesHandler.RegisterRoutes(compatApiRouter)
|
||||||
|
shieldV1BadgeHandler.RegisterRoutes(compatApiRouter)
|
||||||
|
|
||||||
// Static Routes
|
// Static Routes
|
||||||
router.PathPrefix("/assets").Handler(http.FileServer(pkger.Dir("/static")))
|
router.PathPrefix("/assets").Handler(http.FileServer(pkger.Dir("/static")))
|
||||||
|
33
routes/api/health.go
Normal file
33
routes/api/health.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HealthApiHandler struct {
|
||||||
|
db *gorm.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHealthApiHandler(db *gorm.DB) *HealthApiHandler {
|
||||||
|
return &HealthApiHandler{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *HealthApiHandler) RegisterRoutes(router *mux.Router) {
|
||||||
|
r := router.PathPrefix("/health").Subrouter()
|
||||||
|
r.Methods(http.MethodGet).HandlerFunc(h.Get)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *HealthApiHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var dbStatus int
|
||||||
|
if sqlDb, err := h.db.DB(); err == nil {
|
||||||
|
if err := sqlDb.Ping(); err == nil {
|
||||||
|
dbStatus = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
|
w.Write([]byte(fmt.Sprintf("app=1\ndb=%d", dbStatus)))
|
||||||
|
}
|
@ -1,10 +1,11 @@
|
|||||||
package routes
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/emvi/logbuch"
|
"github.com/emvi/logbuch"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
conf "github.com/muety/wakapi/config"
|
conf "github.com/muety/wakapi/config"
|
||||||
|
customMiddleware "github.com/muety/wakapi/middlewares/custom"
|
||||||
"github.com/muety/wakapi/services"
|
"github.com/muety/wakapi/services"
|
||||||
"github.com/muety/wakapi/utils"
|
"github.com/muety/wakapi/utils"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -12,14 +13,14 @@ import (
|
|||||||
"github.com/muety/wakapi/models"
|
"github.com/muety/wakapi/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HeartbeatHandler struct {
|
type HeartbeatApiHandler struct {
|
||||||
config *conf.Config
|
config *conf.Config
|
||||||
heartbeatSrvc services.IHeartbeatService
|
heartbeatSrvc services.IHeartbeatService
|
||||||
languageMappingSrvc services.ILanguageMappingService
|
languageMappingSrvc services.ILanguageMappingService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHeartbeatHandler(heartbeatService services.IHeartbeatService, languageMappingService services.ILanguageMappingService) *HeartbeatHandler {
|
func NewHeartbeatApiHandler(heartbeatService services.IHeartbeatService, languageMappingService services.ILanguageMappingService) *HeartbeatApiHandler {
|
||||||
return &HeartbeatHandler{
|
return &HeartbeatApiHandler{
|
||||||
config: conf.Get(),
|
config: conf.Get(),
|
||||||
heartbeatSrvc: heartbeatService,
|
heartbeatSrvc: heartbeatService,
|
||||||
languageMappingSrvc: languageMappingService,
|
languageMappingSrvc: languageMappingService,
|
||||||
@ -30,13 +31,15 @@ type heartbeatResponseVm struct {
|
|||||||
Responses [][]interface{} `json:"responses"`
|
Responses [][]interface{} `json:"responses"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *HeartbeatHandler) RegisterRoutes(router *mux.Router) {}
|
func (h *HeartbeatApiHandler) RegisterRoutes(router *mux.Router) {
|
||||||
|
r := router.PathPrefix("/heartbeat").Subrouter()
|
||||||
func (h *HeartbeatHandler) RegisterAPIRoutes(router *mux.Router) {
|
r.Use(
|
||||||
router.Methods(http.MethodPost).HandlerFunc(h.ApiPost)
|
customMiddleware.NewWakatimeRelayMiddleware().Handler,
|
||||||
|
)
|
||||||
|
router.Methods(http.MethodPost).HandlerFunc(h.Post)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *HeartbeatHandler) ApiPost(w http.ResponseWriter, r *http.Request) {
|
func (h *HeartbeatApiHandler) Post(w http.ResponseWriter, r *http.Request) {
|
||||||
var heartbeats []*models.Heartbeat
|
var heartbeats []*models.Heartbeat
|
||||||
user := r.Context().Value(models.UserKey).(*models.User)
|
user := r.Context().Value(models.UserKey).(*models.User)
|
||||||
opSys, editor, _ := utils.ParseUserAgent(r.Header.Get("User-Agent"))
|
opSys, editor, _ := utils.ParseUserAgent(r.Header.Get("User-Agent"))
|
37
routes/api/summary.go
Normal file
37
routes/api/summary.go
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
conf "github.com/muety/wakapi/config"
|
||||||
|
su "github.com/muety/wakapi/routes/utils"
|
||||||
|
"github.com/muety/wakapi/services"
|
||||||
|
"github.com/muety/wakapi/utils"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SummaryApiHandler struct {
|
||||||
|
config *conf.Config
|
||||||
|
summarySrvc services.ISummaryService
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSummaryApiHandler(summaryService services.ISummaryService) *SummaryApiHandler {
|
||||||
|
return &SummaryApiHandler{
|
||||||
|
summarySrvc: summaryService,
|
||||||
|
config: conf.Get(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *SummaryApiHandler) RegisterRoutes(router *mux.Router) {
|
||||||
|
router.Methods(http.MethodGet).HandlerFunc(h.Get)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *SummaryApiHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||||
|
summary, err, status := su.LoadUserSummary(h.summarySrvc, r)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(status)
|
||||||
|
w.Write([]byte(err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
utils.RespondJSON(w, http.StatusOK, summary)
|
||||||
|
}
|
@ -31,13 +31,12 @@ func NewBadgeHandler(summaryService services.ISummaryService, userService servic
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *BadgeHandler) RegisterRoutes(router *mux.Router) {}
|
func (h *BadgeHandler) RegisterRoutes(router *mux.Router) {
|
||||||
|
r := router.PathPrefix("/shields/v1/{user}").Subrouter()
|
||||||
func (h *BadgeHandler) RegisterAPIRoutes(router *mux.Router) {
|
r.Methods(http.MethodGet).HandlerFunc(h.Get)
|
||||||
router.Methods(http.MethodGet).HandlerFunc(h.ApiGet)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *BadgeHandler) ApiGet(w http.ResponseWriter, r *http.Request) {
|
func (h *BadgeHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||||
intervalReg := regexp.MustCompile(intervalPattern)
|
intervalReg := regexp.MustCompile(intervalPattern)
|
||||||
entityFilterReg := regexp.MustCompile(entityFilterPattern)
|
entityFilterReg := regexp.MustCompile(entityFilterPattern)
|
||||||
|
|
||||||
|
@ -24,13 +24,11 @@ func NewAllTimeHandler(summaryService services.ISummaryService) *AllTimeHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *AllTimeHandler) RegisterRoutes(router *mux.Router) {}
|
func (h *AllTimeHandler) RegisterRoutes(router *mux.Router) {
|
||||||
|
router.Path("/wakatime/v1/users/{user}/all_time_since_today").Methods(http.MethodGet).HandlerFunc(h.Get)
|
||||||
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) {
|
func (h *AllTimeHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
values, _ := url.ParseQuery(r.URL.RawQuery)
|
values, _ := url.ParseQuery(r.URL.RawQuery)
|
||||||
|
|
||||||
|
@ -25,10 +25,8 @@ func NewSummariesHandler(summaryService services.ISummaryService) *SummariesHand
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *SummariesHandler) RegisterRoutes(router *mux.Router) {}
|
func (h *SummariesHandler) RegisterRoutes(router *mux.Router) {
|
||||||
|
router.Path("/wakatime/v1/users/{user}/summaries").Methods(http.MethodGet).HandlerFunc(h.Get)
|
||||||
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
|
// TODO: Support parameters: project, branches, timeout, writes_only, timezone
|
||||||
@ -36,7 +34,7 @@ func (h *SummariesHandler) RegisterAPIRoutes(router *mux.Router) {
|
|||||||
// Timezone can be specified via an offset suffix (e.g. +02:00) in date strings.
|
// Timezone can be specified via an offset suffix (e.g. +02:00) in date strings.
|
||||||
// Requires https://github.com/muety/wakapi/issues/108.
|
// Requires https://github.com/muety/wakapi/issues/108.
|
||||||
|
|
||||||
func (h *SummariesHandler) ApiGet(w http.ResponseWriter, r *http.Request) {
|
func (h *SummariesHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
requestedUser := vars["user"]
|
requestedUser := vars["user"]
|
||||||
authorizedUser := r.Context().Value(models.UserKey).(*models.User)
|
authorizedUser := r.Context().Value(models.UserKey).(*models.User)
|
||||||
|
@ -4,5 +4,4 @@ import "github.com/gorilla/mux"
|
|||||||
|
|
||||||
type Handler interface {
|
type Handler interface {
|
||||||
RegisterRoutes(router *mux.Router)
|
RegisterRoutes(router *mux.Router)
|
||||||
RegisterAPIRoutes(router *mux.Router)
|
|
||||||
}
|
}
|
||||||
|
@ -1,34 +0,0 @@
|
|||||||
package routes
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/gorilla/mux"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
type HealthHandler struct {
|
|
||||||
db *gorm.DB
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
if err := sqlDb.Ping(); err == nil {
|
|
||||||
dbStatus = 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "text/plain")
|
|
||||||
w.Write([]byte(fmt.Sprintf("app=1\ndb=%d", dbStatus)))
|
|
||||||
}
|
|
@ -32,8 +32,6 @@ func (h *HomeHandler) RegisterRoutes(router *mux.Router) {
|
|||||||
router.Path("/").Methods(http.MethodGet).HandlerFunc(h.GetIndex)
|
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) {
|
func (h *HomeHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
|
||||||
if h.config.IsDev() {
|
if h.config.IsDev() {
|
||||||
loadTemplates()
|
loadTemplates()
|
||||||
|
@ -25,8 +25,6 @@ func (h *ImprintHandler) RegisterRoutes(router *mux.Router) {
|
|||||||
router.Path("/imprint").Methods(http.MethodGet).HandlerFunc(h.GetImprint)
|
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) {
|
func (h *ImprintHandler) GetImprint(w http.ResponseWriter, r *http.Request) {
|
||||||
if h.config.IsDev() {
|
if h.config.IsDev() {
|
||||||
loadTemplates()
|
loadTemplates()
|
||||||
|
@ -32,8 +32,6 @@ func (h *LoginHandler) RegisterRoutes(router *mux.Router) {
|
|||||||
router.Path("/signup").Methods(http.MethodPost).HandlerFunc(h.PostSignup)
|
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) {
|
func (h *LoginHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
|
||||||
if h.config.IsDev() {
|
if h.config.IsDev() {
|
||||||
loadTemplates()
|
loadTemplates()
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/gorilla/schema"
|
"github.com/gorilla/schema"
|
||||||
conf "github.com/muety/wakapi/config"
|
conf "github.com/muety/wakapi/config"
|
||||||
|
"github.com/muety/wakapi/middlewares"
|
||||||
"github.com/muety/wakapi/models"
|
"github.com/muety/wakapi/models"
|
||||||
"github.com/muety/wakapi/models/view"
|
"github.com/muety/wakapi/models/view"
|
||||||
"github.com/muety/wakapi/services"
|
"github.com/muety/wakapi/services"
|
||||||
@ -41,12 +42,14 @@ func NewSettingsHandler(userService services.IUserService, summaryService servic
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *SettingsHandler) RegisterRoutes(router *mux.Router) {
|
func (h *SettingsHandler) RegisterRoutes(router *mux.Router) {
|
||||||
router.Methods(http.MethodGet).HandlerFunc(h.GetIndex)
|
r := router.PathPrefix("/settings").Subrouter()
|
||||||
router.Methods(http.MethodPost).HandlerFunc(h.PostIndex)
|
r.Use(
|
||||||
|
middlewares.NewAuthenticateMiddleware(h.userSrvc, []string{}).Handler,
|
||||||
|
)
|
||||||
|
r.Methods(http.MethodGet).HandlerFunc(h.GetIndex)
|
||||||
|
r.Methods(http.MethodPost).HandlerFunc(h.PostIndex)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *SettingsHandler) RegisterAPIRoutes(router *mux.Router) {}
|
|
||||||
|
|
||||||
func (h *SettingsHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
|
func (h *SettingsHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
|
||||||
if h.config.IsDev() {
|
if h.config.IsDev() {
|
||||||
loadTemplates()
|
loadTemplates()
|
||||||
|
@ -3,8 +3,10 @@ package routes
|
|||||||
import (
|
import (
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
conf "github.com/muety/wakapi/config"
|
conf "github.com/muety/wakapi/config"
|
||||||
|
"github.com/muety/wakapi/middlewares"
|
||||||
"github.com/muety/wakapi/models"
|
"github.com/muety/wakapi/models"
|
||||||
"github.com/muety/wakapi/models/view"
|
"github.com/muety/wakapi/models/view"
|
||||||
|
su "github.com/muety/wakapi/routes/utils"
|
||||||
"github.com/muety/wakapi/services"
|
"github.com/muety/wakapi/services"
|
||||||
"github.com/muety/wakapi/utils"
|
"github.com/muety/wakapi/utils"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -12,33 +14,24 @@ import (
|
|||||||
|
|
||||||
type SummaryHandler struct {
|
type SummaryHandler struct {
|
||||||
config *conf.Config
|
config *conf.Config
|
||||||
|
userSrvc services.IUserService
|
||||||
summarySrvc services.ISummaryService
|
summarySrvc services.ISummaryService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSummaryHandler(summaryService services.ISummaryService) *SummaryHandler {
|
func NewSummaryHandler(summaryService services.ISummaryService, userService services.IUserService) *SummaryHandler {
|
||||||
return &SummaryHandler{
|
return &SummaryHandler{
|
||||||
summarySrvc: summaryService,
|
summarySrvc: summaryService,
|
||||||
|
userSrvc: userService,
|
||||||
config: conf.Get(),
|
config: conf.Get(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *SummaryHandler) RegisterRoutes(router *mux.Router) {
|
func (h *SummaryHandler) RegisterRoutes(router *mux.Router) {
|
||||||
router.Methods(http.MethodGet).HandlerFunc(h.GetIndex)
|
r := router.PathPrefix("/summary").Subrouter()
|
||||||
}
|
r.Use(
|
||||||
|
middlewares.NewAuthenticateMiddleware(h.userSrvc, []string{}).Handler,
|
||||||
func (h *SummaryHandler) RegisterAPIRoutes(router *mux.Router) {
|
)
|
||||||
router.Methods(http.MethodGet).HandlerFunc(h.ApiGet)
|
r.Methods(http.MethodGet).HandlerFunc(h.GetIndex)
|
||||||
}
|
|
||||||
|
|
||||||
func (h *SummaryHandler) ApiGet(w http.ResponseWriter, r *http.Request) {
|
|
||||||
summary, err, status := h.loadUserSummary(r)
|
|
||||||
if err != nil {
|
|
||||||
w.WriteHeader(status)
|
|
||||||
w.Write([]byte(err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
utils.RespondJSON(w, http.StatusOK, summary)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *SummaryHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
|
func (h *SummaryHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -52,7 +45,7 @@ func (h *SummaryHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
|
|||||||
r.URL.RawQuery = q.Encode()
|
r.URL.RawQuery = q.Encode()
|
||||||
}
|
}
|
||||||
|
|
||||||
summary, err, status := h.loadUserSummary(r)
|
summary, err, status := su.LoadUserSummary(h.summarySrvc, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(status)
|
w.WriteHeader(status)
|
||||||
templates[conf.SummaryTemplate].Execute(w, h.buildViewModel(r).WithError(err.Error()))
|
templates[conf.SummaryTemplate].Execute(w, h.buildViewModel(r).WithError(err.Error()))
|
||||||
@ -77,25 +70,6 @@ func (h *SummaryHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
|
|||||||
templates[conf.SummaryTemplate].Execute(w, vm)
|
templates[conf.SummaryTemplate].Execute(w, vm)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *SummaryHandler) loadUserSummary(r *http.Request) (*models.Summary, error, int) {
|
|
||||||
summaryParams, err := utils.ParseSummaryParams(r)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err, http.StatusBadRequest
|
|
||||||
}
|
|
||||||
|
|
||||||
var retrieveSummary services.SummaryRetriever = h.summarySrvc.Retrieve
|
|
||||||
if summaryParams.Recompute {
|
|
||||||
retrieveSummary = h.summarySrvc.Summarize
|
|
||||||
}
|
|
||||||
|
|
||||||
summary, err := h.summarySrvc.Aliased(summaryParams.From, summaryParams.To, summaryParams.User, retrieveSummary)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err, http.StatusInternalServerError
|
|
||||||
}
|
|
||||||
|
|
||||||
return summary, nil, http.StatusOK
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *SummaryHandler) buildViewModel(r *http.Request) *view.SummaryViewModel {
|
func (h *SummaryHandler) buildViewModel(r *http.Request) *view.SummaryViewModel {
|
||||||
return &view.SummaryViewModel{
|
return &view.SummaryViewModel{
|
||||||
Success: r.URL.Query().Get("success"),
|
Success: r.URL.Query().Get("success"),
|
||||||
|
27
routes/utils/summary_utils.go
Normal file
27
routes/utils/summary_utils.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/muety/wakapi/models"
|
||||||
|
"github.com/muety/wakapi/services"
|
||||||
|
"github.com/muety/wakapi/utils"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func LoadUserSummary(ss services.ISummaryService, r *http.Request) (*models.Summary, error, int) {
|
||||||
|
summaryParams, err := utils.ParseSummaryParams(r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err, http.StatusBadRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
var retrieveSummary services.SummaryRetriever = ss.Retrieve
|
||||||
|
if summaryParams.Recompute {
|
||||||
|
retrieveSummary = ss.Summarize
|
||||||
|
}
|
||||||
|
|
||||||
|
summary, err := ss.Aliased(summaryParams.From, summaryParams.To, summaryParams.User, retrieveSummary)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err, http.StatusInternalServerError
|
||||||
|
}
|
||||||
|
|
||||||
|
return summary, nil, http.StatusOK
|
||||||
|
}
|
@ -1 +1 @@
|
|||||||
1.22.0
|
1.22.1
|
Loading…
x
Reference in New Issue
Block a user