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

refactor: replace gorilla mux with chi

This commit is contained in:
Ferdinand Mütsch
2023-03-03 20:40:50 +01:00
parent e495468be2
commit a6ef735ba1
32 changed files with 1407 additions and 1582 deletions

View File

@@ -5,7 +5,7 @@ import (
"errors"
"fmt"
"github.com/emvi/logbuch"
"github.com/gorilla/mux"
"github.com/go-chi/chi/v5"
conf "github.com/muety/wakapi/config"
"github.com/muety/wakapi/middlewares"
"github.com/muety/wakapi/models"
@@ -70,25 +70,27 @@ func NewSubscriptionHandler(
// https://stripe.com/docs/billing/quickstart?lang=go
func (h *SubscriptionHandler) RegisterRoutes(router *mux.Router) {
func (h *SubscriptionHandler) RegisterRoutes(router chi.Router) {
if !h.config.Subscriptions.Enabled {
return
}
subRouterPublic := router.PathPrefix("/subscription").Subrouter()
subRouterPublic.Path("/success").Methods(http.MethodGet).HandlerFunc(h.GetCheckoutSuccess)
subRouterPublic.Path("/cancel").Methods(http.MethodGet).HandlerFunc(h.GetCheckoutCancel)
subRouterPublic.Path("/webhook").Methods(http.MethodPost).HandlerFunc(h.PostWebhook)
subRouterPublic := chi.NewRouter()
subRouterPublic.Get("/success", h.GetCheckoutSuccess)
subRouterPublic.Get("/cancel", h.GetCheckoutCancel)
subRouterPublic.Post("/webhook", h.PostWebhook)
subRouterPrivate := subRouterPublic.PathPrefix("").Subrouter()
subRouterPrivate := chi.NewRouter()
subRouterPrivate.Use(
middlewares.NewAuthenticateMiddleware(h.userSrvc).
WithRedirectTarget(defaultErrorRedirectTarget()).
WithRedirectErrorMessage("unauthorized").
Handler,
WithRedirectErrorMessage("unauthorized").Handler,
)
subRouterPrivate.Path("/checkout").Methods(http.MethodPost).HandlerFunc(h.PostCheckout)
subRouterPrivate.Path("/portal").Methods(http.MethodPost).HandlerFunc(h.PostPortal)
subRouterPrivate.Post("/checkout", h.PostCheckout)
subRouterPrivate.Post("/portal", h.PostPortal)
subRouterPublic.Mount("/", subRouterPrivate)
router.Mount("/subscription", subRouterPublic)
}
func (h *SubscriptionHandler) PostCheckout(w http.ResponseWriter, r *http.Request) {