Skip to content

Commit

Permalink
MG-28 - Align Bootstrap with SuperMQ (#31)
Browse files Browse the repository at this point in the history
* refactor: aligh bootstrap with new supermq architecture

Signed-off-by: Felix Gateru <[email protected]>

* feat: add sdk and update api docs

Signed-off-by: Felix Gateru <[email protected]>

* refactor: rename env variables

Signed-off-by: Felix Gateru <[email protected]>

* style: add empty line to config files and bootstrap docker compose file

Signed-off-by: Felix Gateru <[email protected]>

* refactor: add supermq sdk to magistrala sdk

Signed-off-by: Felix Gateru <[email protected]>

* refactor: extend supermq sdk in magistrala sdk

Signed-off-by: Felix Gateru <[email protected]>

* reafctor: update responses

Signed-off-by: Felix Gateru <[email protected]>

* ci: update api docs dir in swagger-ui deployment

Signed-off-by: Felix Gateru <[email protected]>

---------

Signed-off-by: Felix Gateru <[email protected]>
  • Loading branch information
felixgateru authored Jan 10, 2025
1 parent 3f0bb25 commit ec71a5e
Show file tree
Hide file tree
Showing 52 changed files with 7,865 additions and 298 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/swagger-ui.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
id: swagger-ui-action
uses: blokovi/swagger-ui-action@main
with:
dir: "./api/openapi"
dir: "./apidocs/openapi"
pattern: "*.yml"
debug: "true"

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ USER_REPO ?= $(shell git remote get-url origin | sed -e 's/.*\/\([^/]*\)\/\([^/]
empty:=
space:= $(empty) $(empty)
# Docker compose project name should follow this guidelines: https://docs.docker.com/compose/reference/#use--p-to-specify-a-project-name
DOCKER_PROJECT ?= test #$(shell echo $(subst $(space),,$(USER_REPO)) | tr -c -s '[:alnum:][=-=]' '_' | tr '[:upper:]' '[:lower:]')
DOCKER_PROJECT ?= $(shell echo $(subst $(space),,$(USER_REPO)) | tr -c -s '[:alnum:][=-=]' '_' | tr '[:upper:]' '[:lower:]')
DOCKER_COMPOSE_COMMANDS_SUPPORTED := up down config
DEFAULT_DOCKER_COMPOSE_COMMAND := up
GRPC_MTLS_CERT_FILES_EXISTS = 0
Expand Down
75 changes: 75 additions & 0 deletions api/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package api

import (
"context"
"encoding/json"
"net/http"

"github.com/absmach/magistrala/bootstrap"
api "github.com/absmach/supermq/api/http"
apiutil "github.com/absmach/supermq/api/http/util"
"github.com/absmach/supermq/pkg/errors"
)

// EncodeError encodes an error response.
func EncodeError(ctx context.Context, err error, w http.ResponseWriter) {
var wrapper error
if errors.Contains(err, apiutil.ErrValidation) {
wrapper, err = errors.Unwrap(err)
}

w.Header().Set("Content-Type", api.ContentType)

status, nerr := toStatus(err)
if nerr != nil {
err = unwrap(err)
w.WriteHeader(status)
encodeErrorMessage(err, wrapper, w)
return
}

if wrapper != nil {
err = errors.Wrap(wrapper, err)
}
api.EncodeError(ctx, err, w)
}

func toStatus(err error) (int, error) {
switch {
case errors.Contains(err, bootstrap.ErrExternalKey),
errors.Contains(err, bootstrap.ErrExternalKeySecure):
return http.StatusForbidden, err

case errors.Contains(err, bootstrap.ErrBootstrapState),
errors.Contains(err, bootstrap.ErrAddBootstrap):
return http.StatusBadRequest, err

case errors.Contains(err, bootstrap.ErrBootstrap):
return http.StatusNotFound, err

default:
return 0, nil
}
}

func encodeErrorMessage(err, wrapper error, w http.ResponseWriter) {
if wrapper != nil {
err = errors.Wrap(wrapper, err)
}
if errorVal, ok := err.(errors.Error); ok {
if err := json.NewEncoder(w).Encode(errorVal); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
}
}

func unwrap(err error) error {
wrapper, err := errors.Unwrap(err)
if wrapper != nil {
return wrapper
}
return err
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit ec71a5e

Please sign in to comment.