Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
MG-30 - Auth: Migrate from bone to chi (#47)
Browse files Browse the repository at this point in the history
* Migrate from bone to chi

Signed-off-by: felix.gateru <[email protected]>

* Migrate from bone to chi

Signed-off-by: felix.gateru <[email protected]>

* Add subroutes

Signed-off-by: felix.gateru <[email protected]>

* Update auth service

Signed-off-by: felix.gateru <[email protected]>

* update bootstrap

Signed-off-by: felix.gateru <[email protected]>

* Update multiplexer

Signed-off-by: felix.gateru <[email protected]>

* Refactor user and things handler

Signed-off-by: felix.gateru <[email protected]>

* Refactor user and things handler

Signed-off-by: felix.gateru <[email protected]>

---------

Signed-off-by: felix.gateru <[email protected]>
  • Loading branch information
felixgateru authored Nov 27, 2023
1 parent eb019ee commit 004782d
Show file tree
Hide file tree
Showing 20 changed files with 321 additions and 314 deletions.
16 changes: 8 additions & 8 deletions auth/api/http/domains/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/absmach/magistrala/internal/apiutil"
mfclients "github.com/absmach/magistrala/pkg/clients"
"github.com/absmach/magistrala/pkg/errors"
"github.com/go-zoo/bone"
"github.com/go-chi/chi/v5"
)

func decodeCreateDomainRequest(_ context.Context, r *http.Request) (interface{}, error) {
Expand All @@ -33,7 +33,7 @@ func decodeCreateDomainRequest(_ context.Context, r *http.Request) (interface{},
func decodeRetrieveDomainRequest(_ context.Context, r *http.Request) (interface{}, error) {
req := retrieveDomainRequest{
token: apiutil.ExtractBearerToken(r),
domainID: bone.GetValue(r, "domainID"),
domainID: chi.URLParam(r, "domainID"),
}
return req, nil
}
Expand All @@ -45,7 +45,7 @@ func decodeUpdateDomainRequest(_ context.Context, r *http.Request) (interface{},

req := updateDomainReq{
token: apiutil.ExtractBearerToken(r),
domainID: bone.GetValue(r, "domainID"),
domainID: chi.URLParam(r, "domainID"),
}

if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
Expand All @@ -71,15 +71,15 @@ func decodeListDomainRequest(ctx context.Context, r *http.Request) (interface{},
func decodeEnableDomainRequest(_ context.Context, r *http.Request) (interface{}, error) {
req := enableDomainReq{
token: apiutil.ExtractBearerToken(r),
domainID: bone.GetValue(r, "domainID"),
domainID: chi.URLParam(r, "domainID"),
}
return req, nil
}

func decodeDisableDomainRequest(_ context.Context, r *http.Request) (interface{}, error) {
req := disableDomainReq{
token: apiutil.ExtractBearerToken(r),
domainID: bone.GetValue(r, "domainID"),
domainID: chi.URLParam(r, "domainID"),
}
return req, nil
}
Expand All @@ -91,7 +91,7 @@ func decodeAssignUsersRequest(_ context.Context, r *http.Request) (interface{},

req := assignUsersReq{
token: apiutil.ExtractBearerToken(r),
domainID: bone.GetValue(r, "domainID"),
domainID: chi.URLParam(r, "domainID"),
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return nil, errors.Wrap(apiutil.ErrValidation, errors.Wrap(err, errors.ErrMalformedEntity))
Expand All @@ -107,7 +107,7 @@ func decodeUnassignUsersRequest(_ context.Context, r *http.Request) (interface{}

req := unassignUsersReq{
token: apiutil.ExtractBearerToken(r),
domainID: bone.GetValue(r, "domainID"),
domainID: chi.URLParam(r, "domainID"),
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return nil, errors.Wrap(apiutil.ErrValidation, errors.Wrap(err, errors.ErrMalformedEntity))
Expand All @@ -123,7 +123,7 @@ func decodeListUserDomainsRequest(ctx context.Context, r *http.Request) (interfa
}
req := listUserDomainsReq{
token: apiutil.ExtractBearerToken(r),
userID: bone.GetValue(r, "userID"),
userID: chi.URLParam(r, "userID"),
page: page,
}
return req, nil
Expand Down
117 changes: 59 additions & 58 deletions auth/api/http/domains/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,82 +8,83 @@ import (
"github.com/absmach/magistrala/internal/api"
"github.com/absmach/magistrala/internal/apiutil"
"github.com/absmach/magistrala/logger"
"github.com/go-chi/chi/v5"
kithttp "github.com/go-kit/kit/transport/http"
"github.com/go-zoo/bone"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

func MakeHandler(svc auth.Service, r *bone.Mux, logger logger.Logger) *bone.Mux {
func MakeHandler(svc auth.Service, mux *chi.Mux, logger logger.Logger) *chi.Mux {
opts := []kithttp.ServerOption{
kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, api.EncodeError)),
}

dr := bone.New()
mux.Route("/domains", func(r chi.Router) {
r.Post("/", otelhttp.NewHandler(kithttp.NewServer(
createDomainEndpoint(svc),
decodeCreateDomainRequest,
api.EncodeResponse,
opts...,
), "create_domain").ServeHTTP)

dr.Post("", otelhttp.NewHandler(kithttp.NewServer(
createDomainEndpoint(svc),
decodeCreateDomainRequest,
api.EncodeResponse,
opts...,
), "create_domain"))
r.Get("/", otelhttp.NewHandler(kithttp.NewServer(
listDomainsEndpoint(svc),
decodeListDomainRequest,
api.EncodeResponse,
opts...,
), "list_domains").ServeHTTP)

dr.Get("/:domainID", otelhttp.NewHandler(kithttp.NewServer(
retrieveDomainEndpoint(svc),
decodeRetrieveDomainRequest,
api.EncodeResponse,
opts...,
), "view_domain"))
r.Route("/{domainID}", func(r chi.Router) {
r.Get("/", otelhttp.NewHandler(kithttp.NewServer(
retrieveDomainEndpoint(svc),
decodeRetrieveDomainRequest,
api.EncodeResponse,
opts...,
), "view_domain").ServeHTTP)

dr.Patch("/:domainID", otelhttp.NewHandler(kithttp.NewServer(
updateDomainEndpoint(svc),
decodeUpdateDomainRequest,
api.EncodeResponse,
opts...,
), "update_domain"))
r.Patch("/", otelhttp.NewHandler(kithttp.NewServer(
updateDomainEndpoint(svc),
decodeUpdateDomainRequest,
api.EncodeResponse,
opts...,
), "update_domain").ServeHTTP)

dr.Get("", otelhttp.NewHandler(kithttp.NewServer(
listDomainsEndpoint(svc),
decodeListDomainRequest,
api.EncodeResponse,
opts...,
), "list_domains"))
r.Post("/enable", otelhttp.NewHandler(kithttp.NewServer(
enableDomainEndpoint(svc),
decodeEnableDomainRequest,
api.EncodeResponse,
opts...,
), "enable_domain").ServeHTTP)

dr.Post("/:domainID/enable", otelhttp.NewHandler(kithttp.NewServer(
enableDomainEndpoint(svc),
decodeEnableDomainRequest,
api.EncodeResponse,
opts...,
), "enable_domain"))

dr.Post("/:domainID/disable", otelhttp.NewHandler(kithttp.NewServer(
disableDomainEndpoint(svc),
decodeDisableDomainRequest,
api.EncodeResponse,
opts...,
), "disable_domain"))

dr.Post("/:domainID/users/assign", otelhttp.NewHandler(kithttp.NewServer(
assignDomainUsersEndpoint(svc),
decodeAssignUsersRequest,
api.EncodeResponse,
opts...,
), "assign_domain_users"))

dr.Post("/:domainID/users/unassign", otelhttp.NewHandler(kithttp.NewServer(
unassignDomainUsersEndpoint(svc),
decodeUnassignUsersRequest,
api.EncodeResponse,
opts...,
), "unassign_domain_users"))
r.Post("/disable", otelhttp.NewHandler(kithttp.NewServer(
disableDomainEndpoint(svc),
decodeDisableDomainRequest,
api.EncodeResponse,
opts...,
), "disable_domain").ServeHTTP)

r.SubRoute("/domains", dr)
r.Route("/users", func(r chi.Router) {
r.Post("/assign", otelhttp.NewHandler(kithttp.NewServer(
assignDomainUsersEndpoint(svc),
decodeAssignUsersRequest,
api.EncodeResponse,
opts...,
), "assign_domain_users").ServeHTTP)

r.Get("/users/:userID/domains", otelhttp.NewHandler(kithttp.NewServer(
r.Post("/unassign", otelhttp.NewHandler(kithttp.NewServer(
unassignDomainUsersEndpoint(svc),
decodeUnassignUsersRequest,
api.EncodeResponse,
opts...,
), "unassign_domain_users").ServeHTTP)
})
})
})
mux.Get("/users/{userID}/domains", otelhttp.NewHandler(kithttp.NewServer(
listUserDomainsEndpoint(svc),
decodeListUserDomainsRequest,
api.EncodeResponse,
opts...,
), "list_domains_by_user_id"))
), "list_domains_by_user_id").ServeHTTP)

return r
return mux
}
49 changes: 25 additions & 24 deletions auth/api/http/keys/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,39 @@ import (
"github.com/absmach/magistrala/internal/apiutil"
"github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/errors"
"github.com/go-chi/chi/v5"
kithttp "github.com/go-kit/kit/transport/http"
"github.com/go-zoo/bone"
)

const contentType = "application/json"

// MakeHandler returns a HTTP handler for API endpoints.
func MakeHandler(svc auth.Service, mux *bone.Mux, logger logger.Logger) *bone.Mux {
func MakeHandler(svc auth.Service, mux *chi.Mux, logger logger.Logger) *chi.Mux {
opts := []kithttp.ServerOption{
kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, encodeError)),
}
mux.Post("/keys", kithttp.NewServer(
issueEndpoint(svc),
decodeIssue,
encodeResponse,
opts...,
))

mux.Get("/keys/:id", kithttp.NewServer(
(retrieveEndpoint(svc)),
decodeKeyReq,
encodeResponse,
opts...,
))

mux.Delete("/keys/:id", kithttp.NewServer(
(revokeEndpoint(svc)),
decodeKeyReq,
encodeResponse,
opts...,
))

mux.Route("/keys", func(r chi.Router) {
r.Post("/", kithttp.NewServer(
issueEndpoint(svc),
decodeIssue,
encodeResponse,
opts...,
).ServeHTTP)

r.Get("/{id}", kithttp.NewServer(
(retrieveEndpoint(svc)),
decodeKeyReq,
encodeResponse,
opts...,
).ServeHTTP)

r.Delete("/{id}", kithttp.NewServer(
(revokeEndpoint(svc)),
decodeKeyReq,
encodeResponse,
opts...,
).ServeHTTP)
})
return mux
}

Expand All @@ -65,7 +66,7 @@ func decodeIssue(_ context.Context, r *http.Request) (interface{}, error) {
func decodeKeyReq(_ context.Context, r *http.Request) (interface{}, error) {
req := keyReq{
token: apiutil.ExtractBearerToken(r),
id: bone.GetValue(r, "id"),
id: chi.URLParam(r, "id"),
}
return req, nil
}
Expand Down
6 changes: 3 additions & 3 deletions auth/api/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ import (
"github.com/absmach/magistrala/auth/api/http/domains"
"github.com/absmach/magistrala/auth/api/http/keys"
"github.com/absmach/magistrala/logger"
"github.com/go-zoo/bone"
"github.com/go-chi/chi/v5"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

// MakeHandler returns a HTTP handler for API endpoints.
func MakeHandler(svc auth.Service, logger logger.Logger, instanceID string) http.Handler {
mux := bone.New()
mux := chi.NewRouter()

mux = keys.MakeHandler(svc, mux, logger)
mux = domains.MakeHandler(svc, mux, logger)

mux.GetFunc("/health", magistrala.Health("auth", instanceID))
mux.Get("/health", magistrala.Health("auth", instanceID))
mux.Handle("/metrics", promhttp.Handler())

return mux
Expand Down
Loading

0 comments on commit 004782d

Please sign in to comment.