Skip to content

Commit

Permalink
refactor errors and config
Browse files Browse the repository at this point in the history
  • Loading branch information
Neifi committed Jun 21, 2023
1 parent 3d0a712 commit d920665
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ ARG DISTROLESS_IMAGE=gcr.io/distroless/static
# STEP 1 build executable binary
############################
FROM ${BUILDER_IMAGE} as builder
VOLUME /cfg/
EXPOSE 8080
RUN update-ca-certificates
WORKDIR /go/bin
COPY . .
VOLUME ./
RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -ldflags='-w -s -extldflags "-static"' -a -o /go/bin/run cmd/main.go

############################
Expand Down
7 changes: 4 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ type Config struct {
func InitConfig() (*Config, error) {
var cfg = &Config{}

cfgPath := os.Getenv("BLOOCK_CONFIG_PATH")

var cfgPath string
if cfgPath = os.Getenv("BLOOCK_CONFIG_PATH"); cfgPath == "" {
cfgPath = "./"
}
viper.AddConfigPath(cfgPath)
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(cfgPath)
viper.AutomaticEnv()
err := viper.ReadInConfig()
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions internal/platform/rest/handler/api_error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package handler

import "net/http"

type APIError struct {
Status int `json:"status"`
Message string `json:"message"`
}

func NewAPIError(status int, message string) *APIError {
return &APIError{Status: status, Message: message}
}
func NewBadRequestAPIError(message string) *APIError {
return &APIError{Status: http.StatusBadRequest, Message: message}
}
func NewInternalServerAPIError(message string) *APIError {
return &APIError{Status: http.StatusInternalServerError, Message: message}
}
10 changes: 5 additions & 5 deletions internal/platform/rest/handler/post_create_certification.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ func PostCreateCertification(certification create.Certification) gin.HandlerFunc
if err != nil {
var request CertificationJSONRequest
if err := ctx.BindJSON(&request); err != nil {
ctx.JSON(http.StatusInternalServerError, err.Error())
ctx.JSON(http.StatusInternalServerError, NewInternalServerAPIError(err.Error()))
return
}
jsonBytes, err := json.Marshal(request.data)
if err != nil {
ctx.JSON(http.StatusInternalServerError, err.Error())
ctx.JSON(http.StatusInternalServerError, NewInternalServerAPIError(err.Error()))
return
}
files = append(files, jsonBytes)
if err != nil {
ctx.JSON(http.StatusInternalServerError, err.Error())
ctx.JSON(http.StatusInternalServerError, NewInternalServerAPIError(err.Error()))
return
}

Expand All @@ -43,7 +43,7 @@ func PostCreateCertification(certification create.Certification) gin.HandlerFunc

file, err := io.ReadAll(p)
if err != nil {
ctx.JSON(http.StatusInternalServerError, err.Error())
ctx.JSON(http.StatusInternalServerError, NewInternalServerAPIError(err.Error()))
return
}

Expand All @@ -53,7 +53,7 @@ func PostCreateCertification(certification create.Certification) gin.HandlerFunc

certificationResponse, err := certification.Certify(ctx, files)
if err != nil {
ctx.JSON(http.StatusInternalServerError, err.Error())
ctx.JSON(http.StatusInternalServerError, NewInternalServerAPIError(err.Error()))
return
}

Expand Down
10 changes: 5 additions & 5 deletions internal/platform/rest/handler/post_receive_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func PostReceiveWebhook(certification update.CertificationAnchor, secretKey stri

var webhookRequest WebhookRequest
if err := ctx.BindJSON(&webhookRequest); err != nil {
ctx.JSON(http.StatusBadRequest, "invalid json body")
NewBadRequestAPIError("invalid json body")
return
}
bloockSignature := ctx.GetHeader("Bloock-Signature")
Expand All @@ -23,24 +23,24 @@ func PostReceiveWebhook(certification update.CertificationAnchor, secretKey stri
bodyBytes, err := json.Marshal(webhookRequest)
if err != nil {

ctx.JSON(http.StatusInternalServerError, err.Error())
ctx.JSON(http.StatusInternalServerError, NewInternalServerAPIError(err.Error()))
return
}
ok, err := webhookClient.VerifyWebhookSignature(bodyBytes, bloockSignature, secretKey, enforceTolerance)
if err != nil {
ctx.JSON(http.StatusInternalServerError, err.Error())
ctx.JSON(http.StatusInternalServerError, NewInternalServerAPIError(err.Error()))
return
}
if !ok {
ctx.JSON(http.StatusBadRequest, "invalid signature")
ctx.JSON(http.StatusBadRequest, NewBadRequestAPIError("invalid signature"))
return
}

if err := certification.UpdateAnchor(ctx, request.UpdateCertificationAnchorRequest{
AnchorId: webhookRequest.Data.Network.AnchorId,
Payload: webhookRequest,
}); err != nil {
ctx.JSON(http.StatusInternalServerError, err.Error())
ctx.JSON(http.StatusInternalServerError, NewInternalServerAPIError(err.Error()))
return
}

Expand Down

0 comments on commit d920665

Please sign in to comment.