Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace gorilla/mux with go-chi/chi/v5 #307

Merged
merged 1 commit into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/facebookgo/ensure v0.0.0-20160127193407-b4ab57deab51
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 // indirect
github.com/facebookgo/subset v0.0.0-20150612182917-8dac2c3c4870 // indirect
github.com/gorilla/mux v1.8.0
github.com/go-chi/chi/v5 v5.0.8
github.com/json-iterator/go v1.1.10
github.com/pkg/errors v0.8.1
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 h1:JWuenKqqX8nojt
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052/go.mod h1:UbMTZqLaRiH3MsBH8va0n7s1pQYcu3uTb8G4tygF4Zg=
github.com/facebookgo/subset v0.0.0-20150612182917-8dac2c3c4870 h1:E2s37DuLxFhQDg5gKsWoLBOB0n+ZW8s599zru8FJ2/Y=
github.com/facebookgo/subset v0.0.0-20150612182917-8dac2c3c4870/go.mod h1:5tD+neXqOorC30/tWg0LCSkrqj/AR6gu8yY8/fpw1q0=
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
Expand Down
8 changes: 4 additions & 4 deletions mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"strings"
"sync"

"github.com/gorilla/mux"
"github.com/go-chi/chi/v5"
)

type MockServer interface {
Expand Down Expand Up @@ -110,9 +110,9 @@ func NewMockServer() MockServer {
ms := mockServer{}

// Add all our handlers
r := mux.NewRouter()
r := chi.NewRouter()

func(r *mux.Router) {
r.Route("/v3", func(r chi.Router) {
ms.addIPRoutes(r)
ms.addExportRoutes(r)
ms.addDomainRoutes(r)
Expand All @@ -129,7 +129,7 @@ func NewMockServer() MockServer {
ms.addCredentialsRoutes(r)
ms.addStatsRoutes(r)
ms.addTagsRoutes(r)
}(r.PathPrefix("/v3").Subrouter())
})
ms.addValidationRoutes(r)

// Start the server
Expand Down
18 changes: 9 additions & 9 deletions mock_bounces.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
"net/url"
"time"

"github.com/gorilla/mux"
"github.com/go-chi/chi/v5"
)

func (ms *mockServer) addBouncesRoutes(r *mux.Router) {
r.HandleFunc("/{domain}/bounces", ms.listBounces).Methods(http.MethodGet)
r.HandleFunc("/{domain}/bounces/{address}", ms.getBounce).Methods(http.MethodGet)
r.HandleFunc("/{domain}/bounces/{address}", ms.deleteBounce).Methods(http.MethodDelete)
r.HandleFunc("/{domain}/bounces", ms.deleteBouncesList).Methods(http.MethodDelete)
r.HandleFunc("/{domain}/bounces", ms.createBounce).Methods(http.MethodPost)
func (ms *mockServer) addBouncesRoutes(r chi.Router) {
r.Get("/{domain}/bounces", ms.listBounces)
r.Get("/{domain}/bounces/{address}", ms.getBounce)
r.Delete("/{domain}/bounces/{address}", ms.deleteBounce)
r.Delete("/{domain}/bounces", ms.deleteBouncesList)
r.Post("/{domain}/bounces", ms.createBounce)

ms.bounces = append(ms.bounces, Bounce{
CreatedAt: RFC2822Time(time.Now()),
Expand Down Expand Up @@ -97,7 +97,7 @@ func (ms *mockServer) getBounce(w http.ResponseWriter, r *http.Request) {
ms.mutex.Lock()

for _, bounce := range ms.bounces {
if bounce.Address == mux.Vars(r)["address"] {
if bounce.Address == chi.URLParam(r, "address") {
toJSON(w, bounce)
return
}
Expand Down Expand Up @@ -169,7 +169,7 @@ func (ms *mockServer) deleteBounce(w http.ResponseWriter, r *http.Request) {
ms.mutex.Lock()

for i, bounce := range ms.bounces {
if bounce.Address == mux.Vars(r)["address"] {
if bounce.Address == chi.URLParam(r, "address") {
ms.bounces = append(ms.bounces[:i], ms.bounces[i+1:len(ms.bounces)]...)

toJSON(w, map[string]interface{}{
Expand Down
16 changes: 8 additions & 8 deletions mock_complaints.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"net/url"
"time"

"github.com/gorilla/mux"
"github.com/go-chi/chi/v5"
)

func (ms *mockServer) addComplaintsRoutes(r *mux.Router) {
r.HandleFunc("/{domain}/complaints", ms.listComplaints).Methods(http.MethodGet)
r.HandleFunc("/{domain}/complaints/{address}", ms.getComplaint).Methods(http.MethodGet)
r.HandleFunc("/{domain}/complaints/{address}", ms.deleteComplaint).Methods(http.MethodDelete)
r.HandleFunc("/{domain}/complaints", ms.createComplaint).Methods(http.MethodPost)
func (ms *mockServer) addComplaintsRoutes(r chi.Router) {
r.Get("/{domain}/complaints", ms.listComplaints)
r.Get("/{domain}/complaints/{address}", ms.getComplaint)
r.Delete("/{domain}/complaints/{address}", ms.deleteComplaint)
r.Post("/{domain}/complaints", ms.createComplaint)

ms.complaints = append(ms.complaints, Complaint{
CreatedAt: RFC2822Time(time.Now()),
Expand Down Expand Up @@ -92,7 +92,7 @@ func (ms *mockServer) getComplaint(w http.ResponseWriter, r *http.Request) {
ms.mutex.Lock()

for _, complaint := range ms.complaints {
if complaint.Address == mux.Vars(r)["address"] {
if complaint.Address == chi.URLParam(r, "address") {
toJSON(w, complaint)
return
}
Expand Down Expand Up @@ -162,7 +162,7 @@ func (ms *mockServer) deleteComplaint(w http.ResponseWriter, r *http.Request) {
ms.mutex.Lock()

for i, complaint := range ms.complaints {
if complaint.Address == mux.Vars(r)["address"] {
if complaint.Address == chi.URLParam(r, "address") {
ms.complaints = append(ms.complaints[:i], ms.complaints[i+1:len(ms.complaints)]...)

toJSON(w, map[string]interface{}{
Expand Down
20 changes: 10 additions & 10 deletions mock_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"net/http"
"time"

"github.com/gorilla/mux"
"github.com/go-chi/chi/v5"
)

func (ms *mockServer) addCredentialsRoutes(r *mux.Router) {
r.HandleFunc("/domains/{domain}/credentials", ms.listCredentials).Methods(http.MethodGet)
r.HandleFunc("/domains/{domain}/credentials/{login}", ms.updateCredential).Methods(http.MethodPut)
r.HandleFunc("/domains/{domain}/credentials/{login}", ms.deleteCredential).Methods(http.MethodDelete)
r.HandleFunc("/domains/{domain}/credentials", ms.createCredential).Methods(http.MethodPost)
func (ms *mockServer) addCredentialsRoutes(r chi.Router) {
r.Get("/domains/{domain}/credentials", ms.listCredentials)
r.Put("/domains/{domain}/credentials/{login}", ms.updateCredential)
r.Delete("/domains/{domain}/credentials/{login}", ms.deleteCredential)
r.Post("/domains/{domain}/credentials", ms.createCredential)

ms.credentials = append(ms.credentials, Credential{
CreatedAt: RFC2822Time(time.Now()),
Expand Down Expand Up @@ -103,8 +103,8 @@ func (ms *mockServer) deleteCredential(w http.ResponseWriter, r *http.Request) {
defer ms.mutex.Unlock()
ms.mutex.Lock()

login := mux.Vars(r)["login"]
domain := mux.Vars(r)["domain"]
login := chi.URLParam(r, "login")
domain := chi.URLParam(r, "domain")

for i, credential := range ms.credentials {
if credential.Login == login || credential.Login == login+"@"+domain {
Expand All @@ -127,8 +127,8 @@ func (ms *mockServer) updateCredential(w http.ResponseWriter, r *http.Request) {
defer ms.mutex.Unlock()
ms.mutex.Lock()

domain := mux.Vars(r)["domain"]
login := mux.Vars(r)["login"]
domain := chi.URLParam(r, "domain")
login := chi.URLParam(r, "login")
if err := r.ParseForm(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
Expand Down
62 changes: 31 additions & 31 deletions mock_domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"net/http"
"time"

"github.com/gorilla/mux"
"github.com/go-chi/chi/v5"
)

type DomainContainer struct {
Expand All @@ -16,7 +16,7 @@ type DomainContainer struct {
TagLimits *TagLimits `json:"limits,omitempty"`
}

func (ms *mockServer) addDomainRoutes(r *mux.Router) {
func (ms *mockServer) addDomainRoutes(r chi.Router) {

ms.domainList = append(ms.domainList, DomainContainer{
Domain: Domain{
Expand Down Expand Up @@ -81,24 +81,24 @@ func (ms *mockServer) addDomainRoutes(r *mux.Router) {
},
})

r.HandleFunc("/domains", ms.listDomains).Methods(http.MethodGet)
r.HandleFunc("/domains", ms.createDomain).Methods(http.MethodPost)
r.HandleFunc("/domains/{domain}", ms.getDomain).Methods(http.MethodGet)
r.HandleFunc("/domains/{domain}/verify", ms.getDomain).Methods(http.MethodPut)
r.HandleFunc("/domains/{domain}", ms.deleteDomain).Methods(http.MethodDelete)
//r.HandleFunc("/domains/{domain}/credentials", ms.getCredentials).Methods(http.MethodGet)
//r.HandleFunc("/domains/{domain}/credentials", ms.createCredentials).Methods(http.MethodPost)
//r.HandleFunc("/domains/{domain}/credentials/{login}", ms.updateCredentials).Methods(http.MethodPut)
//r.HandleFunc("/domains/{domain}/credentials/{login}", ms.deleteCredentials).Methods(http.MethodDelete)
r.HandleFunc("/domains/{domain}/connection", ms.getConnection).Methods(http.MethodGet)
r.HandleFunc("/domains/{domain}/connection", ms.updateConnection).Methods(http.MethodPut)
r.HandleFunc("/domains/{domain}/tracking", ms.getTracking).Methods(http.MethodGet)
r.HandleFunc("/domains/{domain}/tracking/click", ms.updateClickTracking).Methods(http.MethodPut)
r.HandleFunc("/domains/{domain}/tracking/open", ms.updateOpenTracking).Methods(http.MethodPut)
r.HandleFunc("/domains/{domain}/tracking/unsubscribe", ms.updateUnsubTracking).Methods(http.MethodPut)
r.HandleFunc("/domains/{domain}/limits/tag", ms.getTagLimits).Methods(http.MethodGet)
r.HandleFunc("/domains/{domain}/dkim_selector", ms.updateDKIMSelector).Methods(http.MethodPut)
r.HandleFunc("/domains/{domain}/web_prefix", ms.updateWebPrefix).Methods(http.MethodPut)
r.Get("/domains", ms.listDomains)
r.Post("/domains", ms.createDomain)
r.Get("/domains/{domain}", ms.getDomain)
r.Put("/domains/{domain}/verify", ms.getDomain)
r.Delete("/domains/{domain}", ms.deleteDomain)
//r.Get("/domains/{domain}/credentials", ms.getCredentials)
//r.Post("/domains/{domain}/credentials", ms.createCredentials)
//r.Put("/domains/{domain}/credentials/{login}", ms.updateCredentials)
//r.Delete("/domains/{domain}/credentials/{login}", ms.deleteCredentials)
r.Get("/domains/{domain}/connection", ms.getConnection)
r.Put("/domains/{domain}/connection", ms.updateConnection)
r.Get("/domains/{domain}/tracking", ms.getTracking)
r.Put("/domains/{domain}/tracking/click", ms.updateClickTracking)
r.Put("/domains/{domain}/tracking/open", ms.updateOpenTracking)
r.Put("/domains/{domain}/tracking/unsubscribe", ms.updateUnsubTracking)
r.Get("/domains/{domain}/limits/tag", ms.getTagLimits)
r.Put("/domains/{domain}/dkim_selector", ms.updateDKIMSelector)
r.Put("/domains/{domain}/web_prefix", ms.updateWebPrefix)
}

func (ms *mockServer) listDomains(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -145,7 +145,7 @@ func (ms *mockServer) getDomain(w http.ResponseWriter, r *http.Request) {
ms.mutex.Lock()

for _, d := range ms.domainList {
if d.Domain.Name == mux.Vars(r)["domain"] {
if d.Domain.Name == chi.URLParam(r, "domain") {
d.Connection = nil
toJSON(w, d)
return
Expand Down Expand Up @@ -179,7 +179,7 @@ func (ms *mockServer) deleteDomain(w http.ResponseWriter, r *http.Request) {

result := ms.domainList[:0]
for _, domain := range ms.domainList {
if domain.Domain.Name == mux.Vars(r)["domain"] {
if domain.Domain.Name == chi.URLParam(r, "domain") {
continue
}
result = append(result, domain)
Expand All @@ -200,7 +200,7 @@ func (ms *mockServer) getConnection(w http.ResponseWriter, r *http.Request) {
ms.mutex.Lock()

for _, d := range ms.domainList {
if d.Domain.Name == mux.Vars(r)["domain"] {
if d.Domain.Name == chi.URLParam(r, "domain") {
resp := domainConnectionResponse{
Connection: *d.Connection,
}
Expand All @@ -217,7 +217,7 @@ func (ms *mockServer) updateConnection(w http.ResponseWriter, r *http.Request) {
ms.mutex.Lock()

for i, d := range ms.domainList {
if d.Domain.Name == mux.Vars(r)["domain"] {
if d.Domain.Name == chi.URLParam(r, "domain") {
ms.domainList[i].Connection = &DomainConnection{
RequireTLS: stringToBool(r.FormValue("require_tls")),
SkipVerification: stringToBool(r.FormValue("skip_verification")),
Expand All @@ -235,7 +235,7 @@ func (ms *mockServer) getTracking(w http.ResponseWriter, r *http.Request) {
ms.mutex.Lock()

for _, d := range ms.domainList {
if d.Domain.Name == mux.Vars(r)["domain"] {
if d.Domain.Name == chi.URLParam(r, "domain") {
resp := domainTrackingResponse{
Tracking: *d.Tracking,
}
Expand All @@ -252,7 +252,7 @@ func (ms *mockServer) updateClickTracking(w http.ResponseWriter, r *http.Request
ms.mutex.Lock()

for i, d := range ms.domainList {
if d.Domain.Name == mux.Vars(r)["domain"] {
if d.Domain.Name == chi.URLParam(r, "domain") {
ms.domainList[i].Tracking.Click.Active = stringToBool(r.FormValue("active"))
toJSON(w, okResp{Message: "Domain tracking settings have been updated"})
return
Expand All @@ -267,7 +267,7 @@ func (ms *mockServer) updateOpenTracking(w http.ResponseWriter, r *http.Request)
ms.mutex.Lock()

for i, d := range ms.domainList {
if d.Domain.Name == mux.Vars(r)["domain"] {
if d.Domain.Name == chi.URLParam(r, "domain") {
ms.domainList[i].Tracking.Open.Active = stringToBool(r.FormValue("active"))
toJSON(w, okResp{Message: "Domain tracking settings have been updated"})
return
Expand All @@ -282,7 +282,7 @@ func (ms *mockServer) updateUnsubTracking(w http.ResponseWriter, r *http.Request
ms.mutex.Lock()

for i, d := range ms.domainList {
if d.Domain.Name == mux.Vars(r)["domain"] {
if d.Domain.Name == chi.URLParam(r, "domain") {
ms.domainList[i].Tracking.Unsubscribe.Active = stringToBool(r.FormValue("active"))
if len(r.FormValue("html_footer")) != 0 {
ms.domainList[i].Tracking.Unsubscribe.HTMLFooter = r.FormValue("html_footer")
Expand All @@ -303,7 +303,7 @@ func (ms *mockServer) getTagLimits(w http.ResponseWriter, r *http.Request) {
ms.mutex.Lock()

for _, d := range ms.domainList {
if d.Domain.Name == mux.Vars(r)["domain"] {
if d.Domain.Name == chi.URLParam(r, "domain") {
if d.TagLimits == nil {
w.WriteHeader(http.StatusNotFound)
toJSON(w, okResp{Message: "no limits defined for domain"})
Expand All @@ -322,7 +322,7 @@ func (ms *mockServer) updateDKIMSelector(w http.ResponseWriter, r *http.Request)
ms.mutex.Lock()

for _, d := range ms.domainList {
if d.Domain.Name == mux.Vars(r)["domain"] {
if d.Domain.Name == chi.URLParam(r, "domain") {
if r.FormValue("dkim_selector") == "" {
toJSON(w, okResp{Message: "dkim_selector param required"})
return
Expand All @@ -340,7 +340,7 @@ func (ms *mockServer) updateWebPrefix(w http.ResponseWriter, r *http.Request) {
ms.mutex.Lock()

for _, d := range ms.domainList {
if d.Domain.Name == mux.Vars(r)["domain"] {
if d.Domain.Name == chi.URLParam(r, "domain") {
if r.FormValue("web_prefix") == "" {
toJSON(w, okResp{Message: "web_prefix param required"})
return
Expand Down
6 changes: 3 additions & 3 deletions mock_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"net/url"
"time"

"github.com/gorilla/mux"
"github.com/go-chi/chi/v5"
"github.com/mailgun/mailgun-go/v4/events"
)

func (ms *mockServer) addEventRoutes(r *mux.Router) {
r.HandleFunc("/{domain}/events", ms.listEvents).Methods(http.MethodGet)
func (ms *mockServer) addEventRoutes(r chi.Router) {
r.Get("/{domain}/events", ms.listEvents)

var (
tags = []string{"tag1", "tag2"}
Expand Down
14 changes: 7 additions & 7 deletions mock_exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"net/http"
"strconv"

"github.com/gorilla/mux"
"github.com/go-chi/chi/v5"
)

func (ms *mockServer) addExportRoutes(r *mux.Router) {
r.HandleFunc("/exports", ms.postExports).Methods(http.MethodPost)
r.HandleFunc("/exports", ms.listExports).Methods(http.MethodGet)
r.HandleFunc("/exports/{id}", ms.getExport).Methods(http.MethodGet)
r.HandleFunc("/exports/{id}/download_url", ms.getExportLink).Methods(http.MethodGet)
func (ms *mockServer) addExportRoutes(r chi.Router) {
r.Post("/exports", ms.postExports)
r.Get("/exports", ms.listExports)
r.Get("/exports/{id}", ms.getExport)
r.Get("/exports/{id}/download_url", ms.getExportLink)
}

func (ms *mockServer) postExports(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -39,7 +39,7 @@ func (ms *mockServer) getExport(w http.ResponseWriter, r *http.Request) {
defer ms.mutex.Unlock()
ms.mutex.Lock()
for _, export := range ms.exportList {
if export.ID == mux.Vars(r)["id"] {
if export.ID == chi.URLParam(r, "id") {
toJSON(w, export)
return
}
Expand Down
Loading