Skip to content

Commit

Permalink
Merge pull request #10 from fiap-8soat-tc-one/hotfix/backend-url
Browse files Browse the repository at this point in the history
fix: ajusts gateway
  • Loading branch information
jcmdsbr authored Dec 16, 2024
2 parents 049d7e7 + 8d3dee7 commit 9d33ec3
Showing 1 changed file with 2 additions and 36 deletions.
38 changes: 2 additions & 36 deletions src/funcs/generate_token/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"encoding/json"
"io"
"log"
"net/http"
Expand All @@ -10,30 +9,12 @@ import (

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/go-playground/validator/v10"
)

func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
var postValidateTokenUrl = os.Getenv("BACKEND_URL")
requestBody := struct {
Document string `json:"document"`
}{}
err := json.Unmarshal([]byte(request.Body), &requestBody)
if err != nil {
log.Printf("Error parsing request body: %v", err)
return events.APIGatewayProxyResponse{
Body: "Error parsing request body",
StatusCode: 500,
}, nil
}

if requestBody.Document == "" {
return events.APIGatewayProxyResponse{
Body: "Please provide a document parameter",
StatusCode: 400,
}, nil
}

log.Printf("request-body: %v", request.Body)
log.Printf("request-body-fmt: %v", strings.NewReader(request.Body))
resp, err := http.Post(postValidateTokenUrl, "application/json", strings.NewReader(request.Body))
if err != nil {
log.Printf("Error getting access token: %v", err)
Expand All @@ -55,21 +36,6 @@ func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
}
defer resp.Body.Close()

// Validate the response before returning it
accessTokenResponse := struct {
AccessToken string `json:"access_token" validate:"required"`
Profile string `json:"profile" validate:"required"`
}{}
err = json.Unmarshal(body, &accessTokenResponse)
if err != nil || validator.New().Struct(accessTokenResponse) != nil {
log.Printf("Response: %s", body)
log.Printf("Error parsing access token: %v", err)
return events.APIGatewayProxyResponse{
Body: "Error parsing access token",
StatusCode: 500,
}, nil
}

return events.APIGatewayProxyResponse{
Body: string(body),
StatusCode: 200,
Expand Down

0 comments on commit 9d33ec3

Please sign in to comment.