Skip to content
This repository has been archived by the owner on Dec 23, 2023. It is now read-only.

Commit

Permalink
refactor: migrate to gorilla mux
Browse files Browse the repository at this point in the history
  • Loading branch information
muety committed Feb 20, 2021
1 parent 17481f6 commit d471182
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 72 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ require (
github.com/emersion/go-smtp v0.14.0
github.com/emvi/logbuch v1.1.2
github.com/google/uuid v1.2.0
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/jinzhu/configor v1.2.1
github.com/julienschmidt/httprouter v1.3.0
github.com/justinas/alice v1.2.0
github.com/timshannon/bolthold v0.0.0-20200817130212-4a25ab140645
go.etcd.io/bbolt v1.3.5 // indirect
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ github.com/emersion/go-smtp v0.14.0 h1:RYW203p+EcPjL8Z/ZpT9lZ6iOc8MG1MQzEx1UKEkX
github.com/emersion/go-smtp v0.14.0/go.mod h1:qm27SGYgoIPRot6ubfQ/GpiPy/g3PaZAVRxiO/sDUgQ=
github.com/emvi/logbuch v1.1.2 h1:uAHXXqEDboQmEFCNG7ZhCK7h6eMTVshvr6L0UrtC13w=
github.com/emvi/logbuch v1.1.2/go.mod h1:J2Wgbr3BuSc1JO+D2MBVh6q3WPVSK5GzktwWz8pvkKw=
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/jinzhu/configor v1.2.1 h1:OKk9dsR8i6HPOCZR8BcMtcEImAFjIhbJFZNyn5GCZko=
github.com/jinzhu/configor v1.2.1/go.mod h1:nX89/MOmDba7ZX7GCyU/VIaQ2Ar2aizBl2d3JLF/rDc=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/justinas/alice v1.2.0 h1:+MHSA/vccVCF4Uq37S42jwlkvI2Xzl7zTPCN5BnZNVo=
github.com/justinas/alice v1.2.0/go.mod h1:fN5HRH/reO/zrUflLfTN43t3vXvKzvZIENsNEe7i7qA=
github.com/timshannon/bolthold v0.0.0-20200817130212-4a25ab140645 h1:fbG8rLkRTpKD2rWD/vtQ6ceXZcnDglJssBieaPvieHw=
github.com/timshannon/bolthold v0.0.0-20200817130212-4a25ab140645/go.mod h1:jUigdmrbdCxcIDEFrq82t4X9805XZfwFZoYUap0ET/U=
go.etcd.io/bbolt v1.3.5 h1:XAzx9gjCb0Rxj7EoqcClPD1d5ZBxZJk0jbuoPHenBt0=
Expand Down
16 changes: 8 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package main

import (
"github.com/emvi/logbuch"
"github.com/julienschmidt/httprouter"
"github.com/justinas/alice"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
conf "github.com/muety/mailwhale/config"
"github.com/muety/mailwhale/service"
"github.com/muety/mailwhale/types"
Expand Down Expand Up @@ -40,17 +40,17 @@ func main() {
initDefaults()

// Global middlewares
recoverMiddleware := middleware.NewRecoverMiddleware()
recoverMiddleware := handlers.RecoveryHandler()
loggingMiddleware := middleware.NewLoggingMiddleware(logbuch.Info, []string{})
baseChain := alice.New(recoverMiddleware, loggingMiddleware)

// Configure routing
router := httprouter.New()
router := mux.NewRouter()
router.Use(recoverMiddleware, loggingMiddleware)

// Handlers
api.NewHealthHandler().Register(router, &baseChain)
api.NewMailHandler(sendService, clientService).Register(router, &baseChain)
api.NewClientHandler(clientService).Register(router, &baseChain)
api.NewHealthHandler().Register(router)
api.NewMailHandler(sendService, clientService).Register(router)
api.NewClientHandler(clientService).Register(router)

listen(router, config)
}
Expand Down
31 changes: 0 additions & 31 deletions web/middleware/recover.go

This file was deleted.

29 changes: 13 additions & 16 deletions web/routes/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package api

import (
"encoding/json"
"github.com/julienschmidt/httprouter"
"github.com/justinas/alice"
"github.com/gorilla/mux"
conf "github.com/muety/mailwhale/config"
"github.com/muety/mailwhale/service"
"github.com/muety/mailwhale/types"
Expand All @@ -26,15 +25,16 @@ func NewClientHandler(clientService *service.ClientService) *ClientHandler {
}
}

func (h *ClientHandler) Register(router *httprouter.Router, baseChain *alice.Chain) {
chain := baseChain.Extend(alice.New(
func (h *ClientHandler) Register(router *mux.Router) {
r := router.PathPrefix(routeClient).Subrouter()
r.Use(
middleware.NewAuthMiddleware(h.clientService, []string{conf.PermissionManageClient}),
))
router.Handler(http.MethodGet, routeClient+"/:name", chain.ThenFunc(h.getByName))
router.Handler(http.MethodPut, routeClient+"/:name", chain.ThenFunc(h.put))
router.Handler(http.MethodDelete, routeClient+"/:name", chain.ThenFunc(h.delete))
router.Handler(http.MethodGet, routeClient, chain.ThenFunc(h.getAll))
router.Handler(http.MethodPost, routeClient, chain.ThenFunc(h.post))
)
r.Path("/{name}").Methods(http.MethodGet).HandlerFunc(h.getByName)
r.Path("/{name}").Methods(http.MethodPut).HandlerFunc(h.put)
r.Path("/{name}").Methods(http.MethodDelete).HandlerFunc(h.delete)
r.Methods(http.MethodGet).HandlerFunc(h.getAll)
r.Methods(http.MethodPost).HandlerFunc(h.post)
}

func (h *ClientHandler) getAll(w http.ResponseWriter, r *http.Request) {
Expand All @@ -50,8 +50,7 @@ func (h *ClientHandler) getAll(w http.ResponseWriter, r *http.Request) {
}

func (h *ClientHandler) getByName(w http.ResponseWriter, r *http.Request) {
ps := httprouter.ParamsFromContext(r.Context())
client, err := h.clientService.GetByName(ps.ByName("name"))
client, err := h.clientService.GetByName(mux.Vars(r)["name"])
if err != nil {
util.RespondError(w, r, http.StatusNotFound, err)
return
Expand All @@ -76,13 +75,12 @@ func (h *ClientHandler) post(w http.ResponseWriter, r *http.Request) {
}

func (h *ClientHandler) put(w http.ResponseWriter, r *http.Request) {
ps := httprouter.ParamsFromContext(r.Context())
var payload types.Client
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
util.RespondError(w, r, http.StatusBadRequest, err)
return
}
payload.Name = ps.ByName("name")
payload.Name = mux.Vars(r)["name"]

client, err := h.clientService.Update(&payload)
if err != nil {
Expand All @@ -93,8 +91,7 @@ func (h *ClientHandler) put(w http.ResponseWriter, r *http.Request) {
}

func (h *ClientHandler) delete(w http.ResponseWriter, r *http.Request) {
ps := httprouter.ParamsFromContext(r.Context())
err := h.clientService.Delete(ps.ByName("name"))
err := h.clientService.Delete(mux.Vars(r)["name"])
if err != nil {
util.RespondError(w, r, http.StatusNotFound, err)
return
Expand Down
9 changes: 4 additions & 5 deletions web/routes/api/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package api

import (
"fmt"
"github.com/julienschmidt/httprouter"
"github.com/justinas/alice"
"github.com/gorilla/mux"
conf "github.com/muety/mailwhale/config"
"net/http"
)
Expand All @@ -20,11 +19,11 @@ func NewHealthHandler() *HealthHandler {
}
}

func (h *HealthHandler) Register(router *httprouter.Router, _ *alice.Chain) {
router.GET("/api/health", h.Get)
func (h *HealthHandler) Register(router *mux.Router) {
router.Path(routeHealth).Methods(http.MethodGet).HandlerFunc(h.Get)
}

func (h *HealthHandler) Get(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
func (h *HealthHandler) Get(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "text-plain")
w.Write([]byte(fmt.Sprintf("app=1\nversion=%s", h.config.Version)))
}
12 changes: 6 additions & 6 deletions web/routes/api/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package api

import (
"encoding/json"
"github.com/julienschmidt/httprouter"
"github.com/justinas/alice"
"github.com/gorilla/mux"
conf "github.com/muety/mailwhale/config"
"github.com/muety/mailwhale/service"
"github.com/muety/mailwhale/types"
Expand All @@ -29,11 +28,12 @@ func NewMailHandler(sendService *service.SendService, clientService *service.Cli
}
}

func (h *MailHandler) Register(router *httprouter.Router, baseChain *alice.Chain) {
chain := baseChain.Extend(alice.New(
func (h *MailHandler) Register(router *mux.Router) {
r := router.PathPrefix(routeMail).Subrouter()
r.Use(
middleware.NewAuthMiddleware(h.clientService, []string{conf.PermissionSendMail}),
))
router.Handler(http.MethodPost, routeMail, chain.ThenFunc(h.post))
)
r.Methods(http.MethodPost).HandlerFunc(h.post)
}

func (h *MailHandler) post(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit d471182

Please sign in to comment.