Skip to content
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
3 changes: 2 additions & 1 deletion createFeatureFlag/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module createFeatureFlag
go 1.20

require (
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20240806210615-ae45f3d1311a
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20250121125844-dc297346c8ce
github.com/aws/aws-lambda-go v1.41.0
github.com/aws/aws-sdk-go v1.44.284
github.com/go-playground/validator/v10 v10.14.1
Expand All @@ -14,6 +14,7 @@ require (
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
Expand Down
8 changes: 6 additions & 2 deletions createFeatureFlag/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20240806210615-ae45f3d1311a h1:4hBZDCacd+X+JCRE2x3pHRiU1RGWzkj239WCL7OgY/8=
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20240806210615-ae45f3d1311a/go.mod h1:KIXxGKS5Hn3yFpu0nGfhYFrNF7yChl2yjGk8ZbRCso8=
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20250105174329-d640dca1d9d2 h1:nqE0aZPsY4qkv5YfZ3g/nyNYn32Mrz45ypXDpbxb5no=
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20250105174329-d640dca1d9d2/go.mod h1:5PjUN7WNRsmIsAFRipvthv2FR5EmDXPUKdpst0ejrDo=
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20250121125844-dc297346c8ce h1:yvIuR8iGTJGyckU7DZv5LklDxGHYkib/joUAjRilozI=
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20250121125844-dc297346c8ce/go.mod h1:5PjUN7WNRsmIsAFRipvthv2FR5EmDXPUKdpst0ejrDo=
github.com/aws/aws-lambda-go v1.41.0 h1:l/5fyVb6Ud9uYd411xdHZzSf2n86TakxzpvIoz7l+3Y=
github.com/aws/aws-lambda-go v1.41.0/go.mod h1:jwFe2KmMsHmffA1X2R09hH6lFzJQxzI8qK17ewzbQMM=
github.com/aws/aws-sdk-go v1.44.284 h1:Oc5Kubi43/VCkerlt3ZU3KpBju6BpNkoG3s7E8vj/O8=
Expand All @@ -16,6 +18,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.14.1 h1:9c50NUPC30zyuKprjL3vNZ0m5oG+jU0zvx4AqHGnv4k=
github.com/go-playground/validator/v10 v10.14.1/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
Expand Down
47 changes: 36 additions & 11 deletions createFeatureFlag/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"time"

"github.com/Real-Dev-Squad/feature-flag-backend/database"
"github.com/Real-Dev-Squad/feature-flag-backend/jwt"
middleware "github.com/Real-Dev-Squad/feature-flag-backend/middlewares"
"github.com/Real-Dev-Squad/feature-flag-backend/models"
"github.com/Real-Dev-Squad/feature-flag-backend/utils"
"github.com/aws/aws-lambda-go/events"
Expand All @@ -23,7 +25,7 @@ func init() {
validate = validator.New()
}

func createFeatureFlag(db *dynamodb.DynamoDB, createFeatureFlagRequest utils.CreateFeatureFlagRequest) error {
func createFeatureFlag(db *dynamodb.DynamoDB, createFeatureFlagRequest utils.CreateFeatureFlagRequest) (models.FeatureFlag, error) {
featureFlag := models.FeatureFlag{
Id: uuid.New().String(),
Name: createFeatureFlagRequest.FlagName,
Expand All @@ -38,7 +40,7 @@ func createFeatureFlag(db *dynamodb.DynamoDB, createFeatureFlagRequest utils.Cre
item, err := database.MarshalMap(featureFlag)
if err != nil {
log.Printf("Error marshalling object to DynamoDB AttributeValue: \n %v", err)
return err
return models.FeatureFlag{}, err
}

input := &dynamodb.PutItemInput{
Expand All @@ -49,9 +51,9 @@ func createFeatureFlag(db *dynamodb.DynamoDB, createFeatureFlagRequest utils.Cre
_, err = db.PutItem(input)
if err != nil {
log.Printf("Error putting item to Dynamodb: \n %v", err)
return err
return models.FeatureFlag{}, err
}
return nil
return featureFlag, nil
}

func handler(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
Expand All @@ -60,8 +62,18 @@ func handler(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse,
db := database.CreateDynamoDB()

utils.CheckRequestAllowed(db, utils.ConcurrencyDisablingLambda)

err := json.Unmarshal([]byte(req.Body), &createFeatureFlagRequest)

corsResponse, err, passed := middleware.HandleCORS(req)
if !passed {
return corsResponse, err
}

jwtResponse, _, err := jwt.JWTMiddleware()(req)
if err != nil || jwtResponse.StatusCode != http.StatusOK {
return jwtResponse, err
}

err = json.Unmarshal([]byte(req.Body), &createFeatureFlagRequest)
if err != nil {
log.Printf("Error unmarshal request body: \n %v", err)
return utils.ClientError(http.StatusUnprocessableEntity, "Error unmarshalling request body")
Expand All @@ -74,19 +86,32 @@ func handler(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse,
}, nil
}

err = createFeatureFlag(db, createFeatureFlagRequest)
featureFlag, err := createFeatureFlag(db, createFeatureFlagRequest)
if err != nil {
log.Printf("Error while creating feature flag: \n %v ", err)
return utils.ServerError(err)
}

origin := req.Headers["Origin"]
corsHeaders := middleware.GetCORSHeaders(origin)

response := events.APIGatewayProxyResponse{
StatusCode: http.StatusCreated,
Headers: map[string]string{
"Content-Type": "application/json",
},
Body: "Created feature flag successfully",
Headers: corsHeaders,
}

responseBody, err := json.Marshal(map[string]interface{}{
"message": "Created feature flag successfully",
"data": featureFlag,
})

if err != nil {
log.Printf("Error marshalling response body: %v", err)
return utils.ServerError(err)
}

response.Body = string(responseBody)

return response, nil
}

Expand Down
3 changes: 2 additions & 1 deletion createUserFeatureFlag/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module createUserFeatureFlag
go 1.20

require (
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20240806210615-ae45f3d1311a
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20250121125844-dc297346c8ce
github.com/aws/aws-lambda-go v1.41.0
github.com/aws/aws-sdk-go v1.44.284
github.com/go-playground/validator/v10 v10.14.1
Expand All @@ -13,6 +13,7 @@ require (
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
Expand Down
8 changes: 6 additions & 2 deletions createUserFeatureFlag/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20240806210615-ae45f3d1311a h1:4hBZDCacd+X+JCRE2x3pHRiU1RGWzkj239WCL7OgY/8=
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20240806210615-ae45f3d1311a/go.mod h1:KIXxGKS5Hn3yFpu0nGfhYFrNF7yChl2yjGk8ZbRCso8=
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20250105174329-d640dca1d9d2 h1:nqE0aZPsY4qkv5YfZ3g/nyNYn32Mrz45ypXDpbxb5no=
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20250105174329-d640dca1d9d2/go.mod h1:5PjUN7WNRsmIsAFRipvthv2FR5EmDXPUKdpst0ejrDo=
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20250121125844-dc297346c8ce h1:yvIuR8iGTJGyckU7DZv5LklDxGHYkib/joUAjRilozI=
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20250121125844-dc297346c8ce/go.mod h1:5PjUN7WNRsmIsAFRipvthv2FR5EmDXPUKdpst0ejrDo=
github.com/aws/aws-lambda-go v1.41.0 h1:l/5fyVb6Ud9uYd411xdHZzSf2n86TakxzpvIoz7l+3Y=
github.com/aws/aws-lambda-go v1.41.0/go.mod h1:jwFe2KmMsHmffA1X2R09hH6lFzJQxzI8qK17ewzbQMM=
github.com/aws/aws-sdk-go v1.44.284 h1:Oc5Kubi43/VCkerlt3ZU3KpBju6BpNkoG3s7E8vj/O8=
Expand All @@ -16,6 +18,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.14.1 h1:9c50NUPC30zyuKprjL3vNZ0m5oG+jU0zvx4AqHGnv4k=
github.com/go-playground/validator/v10 v10.14.1/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
Expand Down
18 changes: 17 additions & 1 deletion createUserFeatureFlag/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"time"

"github.com/Real-Dev-Squad/feature-flag-backend/database"
"github.com/Real-Dev-Squad/feature-flag-backend/jwt"
middleware "github.com/Real-Dev-Squad/feature-flag-backend/middlewares"
"github.com/Real-Dev-Squad/feature-flag-backend/models"
"github.com/Real-Dev-Squad/feature-flag-backend/utils"
"github.com/aws/aws-lambda-go/events"
Expand Down Expand Up @@ -54,8 +56,18 @@ func handler(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse,
userId := req.PathParameters["userId"]
flagId := req.PathParameters["flagId"]

corsResponse, err, passed := middleware.HandleCORS(req)
if !passed {
return corsResponse, err
}

jwtResponse, _, err := jwt.JWTMiddleware()(req)
if err != nil || jwtResponse.StatusCode != http.StatusOK {
return jwtResponse, err
}

var requestBody utils.CreateFeatureFlagUserMappingRequest
err := json.Unmarshal([]byte(req.Body), &requestBody)
err = json.Unmarshal([]byte(req.Body), &requestBody)
if err != nil {
log.Printf("Error unmarshal request body: \n %v", err)
return utils.ClientError(http.StatusUnprocessableEntity, "Error unmarshal request body")
Expand Down Expand Up @@ -103,9 +115,13 @@ func handler(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse,
return utils.ServerError(err)
}

origin := req.Headers["Origin"]
corsHeaders := middleware.GetCORSHeaders(origin)

response := events.APIGatewayProxyResponse{
Body: string(resultJson),
StatusCode: http.StatusOK,
Headers: corsHeaders,
}

return response, nil
Expand Down
3 changes: 2 additions & 1 deletion getAllFeatureFlags/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ module getAllFeatureFlags
go 1.20

require (
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20240806210615-ae45f3d1311a
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20250121125844-dc297346c8ce
github.com/aws/aws-lambda-go v1.41.0
github.com/aws/aws-sdk-go v1.44.284
)

require (
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
)
8 changes: 6 additions & 2 deletions getAllFeatureFlags/go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20240806210615-ae45f3d1311a h1:4hBZDCacd+X+JCRE2x3pHRiU1RGWzkj239WCL7OgY/8=
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20240806210615-ae45f3d1311a/go.mod h1:KIXxGKS5Hn3yFpu0nGfhYFrNF7yChl2yjGk8ZbRCso8=
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20250105174329-d640dca1d9d2 h1:nqE0aZPsY4qkv5YfZ3g/nyNYn32Mrz45ypXDpbxb5no=
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20250105174329-d640dca1d9d2/go.mod h1:5PjUN7WNRsmIsAFRipvthv2FR5EmDXPUKdpst0ejrDo=
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20250121125844-dc297346c8ce h1:yvIuR8iGTJGyckU7DZv5LklDxGHYkib/joUAjRilozI=
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20250121125844-dc297346c8ce/go.mod h1:5PjUN7WNRsmIsAFRipvthv2FR5EmDXPUKdpst0ejrDo=
github.com/aws/aws-lambda-go v1.41.0 h1:l/5fyVb6Ud9uYd411xdHZzSf2n86TakxzpvIoz7l+3Y=
github.com/aws/aws-lambda-go v1.41.0/go.mod h1:jwFe2KmMsHmffA1X2R09hH6lFzJQxzI8qK17ewzbQMM=
github.com/aws/aws-sdk-go v1.44.284 h1:Oc5Kubi43/VCkerlt3ZU3KpBju6BpNkoG3s7E8vj/O8=
github.com/aws/aws-sdk-go v1.44.284/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
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/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
Expand Down
20 changes: 17 additions & 3 deletions getAllFeatureFlags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"net/http"

"github.com/Real-Dev-Squad/feature-flag-backend/database"
"github.com/Real-Dev-Squad/feature-flag-backend/jwt"
middleware "github.com/Real-Dev-Squad/feature-flag-backend/middlewares"
"github.com/Real-Dev-Squad/feature-flag-backend/utils"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
Expand Down Expand Up @@ -45,9 +47,18 @@ func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
db := database.CreateDynamoDB()

utils.CheckRequestAllowed(db, utils.ConcurrencyDisablingLambda)

featureFlagsResponse, err := getAllFeatureFlags(db)

corsResponse, err, passed := middleware.HandleCORS(request)
if !passed {
return corsResponse, err
}

response, _, err := jwt.JWTMiddleware()(request)
if err != nil || response.StatusCode != http.StatusOK {
return response, err
}

featureFlagsResponse, err := getAllFeatureFlags(db)
if err != nil {
return utils.ServerError(err)
}
Expand All @@ -62,11 +73,14 @@ func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
return utils.ServerError(err)
}

origin := request.Headers["Origin"]
corsHeaders := middleware.GetCORSHeaders(origin)

return events.APIGatewayProxyResponse{
Body: string(jsonResult),
StatusCode: http.StatusOK,
Headers: corsHeaders,
}, nil

}

func main() {
Expand Down
3 changes: 2 additions & 1 deletion getFeatureFlagById/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ module getFeatureFlagById
go 1.20

require (
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20231006175112-8daf955a8572
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20250121125844-dc297346c8ce
github.com/aws/aws-lambda-go v1.41.0
)

require (
github.com/aws/aws-sdk-go v1.44.284 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
)
8 changes: 6 additions & 2 deletions getFeatureFlagById/go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20231006175112-8daf955a8572 h1:Hyc2jIH2tjWTafRMjPeznbRHpbw/1Q8UmRdHVoRrzE8=
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20231006175112-8daf955a8572/go.mod h1:KIXxGKS5Hn3yFpu0nGfhYFrNF7yChl2yjGk8ZbRCso8=
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20250105174329-d640dca1d9d2 h1:nqE0aZPsY4qkv5YfZ3g/nyNYn32Mrz45ypXDpbxb5no=
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20250105174329-d640dca1d9d2/go.mod h1:5PjUN7WNRsmIsAFRipvthv2FR5EmDXPUKdpst0ejrDo=
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20250121125844-dc297346c8ce h1:yvIuR8iGTJGyckU7DZv5LklDxGHYkib/joUAjRilozI=
github.com/Real-Dev-Squad/feature-flag-backend v0.0.0-20250121125844-dc297346c8ce/go.mod h1:5PjUN7WNRsmIsAFRipvthv2FR5EmDXPUKdpst0ejrDo=
github.com/aws/aws-lambda-go v1.41.0 h1:l/5fyVb6Ud9uYd411xdHZzSf2n86TakxzpvIoz7l+3Y=
github.com/aws/aws-lambda-go v1.41.0/go.mod h1:jwFe2KmMsHmffA1X2R09hH6lFzJQxzI8qK17ewzbQMM=
github.com/aws/aws-sdk-go v1.44.284 h1:Oc5Kubi43/VCkerlt3ZU3KpBju6BpNkoG3s7E8vj/O8=
github.com/aws/aws-sdk-go v1.44.284/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
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/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
Expand Down
47 changes: 35 additions & 12 deletions getFeatureFlagById/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,60 @@ import (
"net/http"

"github.com/Real-Dev-Squad/feature-flag-backend/database"
"github.com/Real-Dev-Squad/feature-flag-backend/jwt"
middleware "github.com/Real-Dev-Squad/feature-flag-backend/middlewares"
"github.com/Real-Dev-Squad/feature-flag-backend/utils"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)

func handler(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
id, _ := req.PathParameters["flagId"]
featureFlag, err := database.ProcessGetFeatureFlagByHashKey(utils.Id, id)
corsResponse, err, passed := middleware.HandleCORS(req)

if !passed {
return corsResponse, err
}

response, _, err := jwt.JWTMiddleware()(req)
if err != nil || response.StatusCode != http.StatusOK {
return response, err
}

featureFlagId, ok := req.PathParameters["flagId"]
if !ok {
log.Println("flagId is required")
clientErrorResponse, _ := utils.ClientError(http.StatusBadRequest, "flagId is required")
return clientErrorResponse, nil
}

featureFlag, err := database.ProcessGetFeatureFlagByHashKey(utils.Id, featureFlagId)
if err != nil {
return utils.ServerError(err)
log.Printf("Database error: %v", err)
serverErrorResponse, _ := utils.ServerError(err)
return serverErrorResponse, nil
}

if featureFlag == nil {
log.Println("Feature Flag not found")
return utils.ClientError(http.StatusNotFound, "Feature flag not found")

clientErrorResponse, _ := utils.ClientError(http.StatusNotFound, "Feature flag not found")
return clientErrorResponse, nil
}
log.Println(featureFlag, " is the feature flag")

jsonResponse, err := json.Marshal(featureFlag)
if err != nil {
log.Printf("Error converting FeatureFlag to JSON \n %v", err)
return utils.ServerError(err)
log.Printf("Error converting FeatureFlag to JSON: %v", err)
serverErrorResponse, _ := utils.ServerError(err)
return serverErrorResponse, nil
}

response := events.APIGatewayProxyResponse{
origin := req.Headers["Origin"]
corsHeaders := middleware.GetCORSHeaders(origin)

response = events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
Headers: map[string]string{
"Content-Type": "application/json",
},
Body: string(jsonResponse),
Headers: corsHeaders,
Body: string(jsonResponse),
}
return response, nil
}
Expand Down
Loading
Loading