[service/http]switch from Gorilla Mux to Chi (#357)

Signed-off-by: Jacky CSL <jcslmu@gmail.com>
This commit is contained in:
jackycsl 2023-03-21 12:11:22 +08:00 committed by GitHub
parent 3f795a2171
commit 8ba232d2a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 27 deletions

1
go.mod
View File

@ -15,6 +15,7 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-chi/chi/v5 v5.0.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect

2
go.sum
View File

@ -2,6 +2,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=

View File

@ -19,7 +19,7 @@ import (
"os"
"time"
"github.com/gorilla/mux"
"github.com/go-chi/chi/v5"
"github.com/dapr/go-sdk/actor"
"github.com/dapr/go-sdk/actor/config"
@ -34,13 +34,13 @@ func NewService(address string) common.Service {
}
// NewServiceWithMux creates new Service with existing http mux.
func NewServiceWithMux(address string, mux *mux.Router) common.Service {
func NewServiceWithMux(address string, mux *chi.Mux) common.Service {
return newServer(address, mux)
}
func newServer(address string, router *mux.Router) *Server {
func newServer(address string, router *chi.Mux) *Server {
if router == nil {
router = mux.NewRouter()
router = chi.NewRouter()
}
return &Server{
address: address,
@ -57,7 +57,7 @@ func newServer(address string, router *mux.Router) *Server {
// Server is the HTTP server wrapping mux many Dapr helpers.
type Server struct {
address string
mux *mux.Router
mux *chi.Mux
httpServer *http.Server
topicRegistrar internal.TopicRegistrar
authToken string

View File

@ -20,7 +20,7 @@ import (
"io"
"net/http"
"github.com/gorilla/mux"
"github.com/go-chi/chi/v5"
actorErr "github.com/dapr/go-sdk/actor/error"
"github.com/dapr/go-sdk/actor/runtime"
@ -86,7 +86,7 @@ func (s *Server) registerBaseHandler() {
fHealth := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
s.mux.HandleFunc("/healthz", fHealth).Methods(http.MethodGet)
s.mux.Get("/healthz", fHealth)
// register actor config handler
fRegister := func(w http.ResponseWriter, r *http.Request) {
@ -100,14 +100,13 @@ func (s *Server) registerBaseHandler() {
return
}
}
s.mux.HandleFunc("/dapr/config", fRegister).Methods(http.MethodGet)
s.mux.Get("/dapr/config", fRegister)
// register actor method invoke handler
fInvoke := func(w http.ResponseWriter, r *http.Request) {
varsMap := mux.Vars(r)
actorType := varsMap["actorType"]
actorID := varsMap["actorId"]
methodName := varsMap["methodName"]
actorType := chi.URLParam(r, "actorType")
actorID := chi.URLParam(r, "actorId")
methodName := chi.URLParam(r, "methodName")
reqData, _ := io.ReadAll(r.Body)
rspData, err := runtime.GetActorRuntimeInstance().InvokeActorMethod(actorType, actorID, methodName, reqData)
if err == actorErr.ErrActorTypeNotFound {
@ -121,13 +120,12 @@ func (s *Server) registerBaseHandler() {
w.WriteHeader(http.StatusOK)
_, _ = w.Write(rspData)
}
s.mux.HandleFunc("/actors/{actorType}/{actorId}/method/{methodName}", fInvoke).Methods(http.MethodPut)
s.mux.Put("/actors/{actorType}/{actorId}/method/{methodName}", fInvoke)
// register deactivate actor handler
fDelete := func(w http.ResponseWriter, r *http.Request) {
varsMap := mux.Vars(r)
actorType := varsMap["actorType"]
actorID := varsMap["actorId"]
actorType := chi.URLParam(r, "actorType")
actorID := chi.URLParam(r, "actorId")
err := runtime.GetActorRuntimeInstance().Deactivate(actorType, actorID)
if err == actorErr.ErrActorTypeNotFound || err == actorErr.ErrActorIDNotFound {
w.WriteHeader(http.StatusNotFound)
@ -137,14 +135,13 @@ func (s *Server) registerBaseHandler() {
}
w.WriteHeader(http.StatusOK)
}
s.mux.HandleFunc("/actors/{actorType}/{actorId}", fDelete).Methods(http.MethodDelete)
s.mux.Delete("/actors/{actorType}/{actorId}", fDelete)
// register actor reminder invoke handler
fReminder := func(w http.ResponseWriter, r *http.Request) {
varsMap := mux.Vars(r)
actorType := varsMap["actorType"]
actorID := varsMap["actorId"]
reminderName := varsMap["reminderName"]
actorType := chi.URLParam(r, "actorType")
actorID := chi.URLParam(r, "actorId")
reminderName := chi.URLParam(r, "reminderName")
reqData, _ := io.ReadAll(r.Body)
err := runtime.GetActorRuntimeInstance().InvokeReminder(actorType, actorID, reminderName, reqData)
if err == actorErr.ErrActorTypeNotFound {
@ -155,14 +152,13 @@ func (s *Server) registerBaseHandler() {
}
w.WriteHeader(http.StatusOK)
}
s.mux.HandleFunc("/actors/{actorType}/{actorId}/method/remind/{reminderName}", fReminder).Methods(http.MethodPut)
s.mux.Put("/actors/{actorType}/{actorId}/method/remind/{reminderName}", fReminder)
// register actor timer invoke handler
fTimer := func(w http.ResponseWriter, r *http.Request) {
varsMap := mux.Vars(r)
actorType := varsMap["actorType"]
actorID := varsMap["actorId"]
timerName := varsMap["timerName"]
actorType := chi.URLParam(r, "actorType")
actorID := chi.URLParam(r, "actorId")
timerName := chi.URLParam(r, "timerName")
reqData, _ := io.ReadAll(r.Body)
err := runtime.GetActorRuntimeInstance().InvokeTimer(actorType, actorID, timerName, reqData)
if err == actorErr.ErrActorTypeNotFound {
@ -173,7 +169,7 @@ func (s *Server) registerBaseHandler() {
}
w.WriteHeader(http.StatusOK)
}
s.mux.HandleFunc("/actors/{actorType}/{actorId}/method/timer/{timerName}", fTimer).Methods(http.MethodPut)
s.mux.Put("/actors/{actorType}/{actorId}/method/timer/{timerName}", fTimer)
}
// AddTopicEventHandler appends provided event handler with it's name to the service.