Skip to content

Commit

Permalink
feat: some tweaks for prod differences
Browse files Browse the repository at this point in the history
  • Loading branch information
noobj committed Oct 7, 2022
1 parent 2930e24 commit c909b87
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 5 deletions.
2 changes: 2 additions & 0 deletions cmd/ahorro/login/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (events
Value: token,
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteNoneMode,
Expires: time.Now().Add(time.Second * time.Duration(accessTokenExpireTime)),
Path: "/",
}
Expand All @@ -105,6 +106,7 @@ func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (events
Value: refreshToken,
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteNoneMode,
Expires: time.Now().Add(time.Second * time.Duration(refreshTokenExpireTime)),
Path: "/auth",
}
Expand Down
1 change: 1 addition & 0 deletions cmd/ahorro/refresh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func Handler(ctx context.Context, request events.APIGatewayV2HTTPRequest) (event
HttpOnly: true,
Secure: true,
Expires: time.Now().Add(time.Second * time.Duration(accessTokenExpireTime)),
SameSite: http.SameSiteNoneMode,
Path: "/",
}
helper.SetCookie(cookieWithAccessTkn, &resp)
Expand Down
8 changes: 7 additions & 1 deletion cmd/ahorro/sync/callback/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ func Handler(ctx context.Context, request events.APIGatewayV2HTTPRequest) (event
return internalErrorhandler()
}

return helper.PushSyncRequest(user.Id.Hex())
res, _ := helper.PushSyncRequest(user.Id.Hex())

if res.StatusCode != 200 {
return res, nil
}

return helper.GenerateRedirectResponse[events.APIGatewayProxyResponse](env.FrontendUrl)
}

func main() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/ahorro/sync/checker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func Handler(ctx context.Context, request events.APIGatewayV2HTTPRequest) (event
TableName: aws.String(env.DynamoTaskTable),
}

for i := 0; i < 10; i++ {
for i := 0; i < 20; i++ {
item, err := svc.GetItem(input)

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/ahorro/sync/receiver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ func Handler(ctx context.Context, request events.APIGatewayV2HTTPRequest) (event

if err != nil {
log.Printf("Unable to create Drive service: %v", err)
return helper.GenerateApiResponse[events.APIGatewayProxyResponse](authURL)
return helper.GenerateErrorResponse[events.APIGatewayProxyResponse](301, authURL)
}

fileId := file.Files[0].Id

if _, err = service.Files.Get(fileId).Do(); err != nil {
log.Printf("Unable to create Drive service: %v", err)
return helper.GenerateApiResponse[events.APIGatewayProxyResponse](authURL)
return helper.GenerateErrorResponse[events.APIGatewayProxyResponse](301, authURL)
}

return helper.PushSyncRequest(user.Id.Hex())
Expand Down
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type Specification struct {
SwimNotifyChannelId string `required:"true" split_words:"true"`
DynamoRandTable string `required:"true" split_words:"true"`
DynamoTaskTable string `required:"true" split_words:"true"`
BackendUrl string `required:"true" split_words:"true"`
FrontendUrl string `required:"true" split_words:"true"`
}

var specInstance *Specification
Expand Down
5 changes: 4 additions & 1 deletion internal/helpers/helper/oauth2.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package helper

import (
"fmt"

"github.com/noobj/go-serverless-services/internal/config"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
Expand All @@ -11,12 +13,13 @@ func GenerateOauthConfig() *oauth2.Config {
env := config.GetInstance()
googleClientId := env.GoogleClientId
googleClientSecret := env.GoogleClientSecret
backendUrl := env.BackendUrl

return &oauth2.Config{
ClientID: googleClientId,
ClientSecret: googleClientSecret,
Endpoint: google.Endpoint,
Scopes: []string{drive.DriveReadonlyScope},
RedirectURL: "https://ahorrojs.io/sync/callback",
RedirectURL: fmt.Sprintf("%s/sync/callback", backendUrl),
}
}
21 changes: 21 additions & 0 deletions internal/helpers/helper/request_and_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@ import (
"github.com/noobj/go-serverless-services/internal/types"
)

func GenerateRedirectResponse[T types.ApiResponse](urlForRedirect string) (T, error) {
var res T

switch t := any(&res).(type) {
case *events.APIGatewayProxyResponse:
t.StatusCode = 301
t.IsBase64Encoded = false
t.Headers = map[string]string{
"Location": urlForRedirect,
}
case *events.APIGatewayV2HTTPResponse:
t.StatusCode = 301
t.IsBase64Encoded = false
t.Headers = map[string]string{
"Location": urlForRedirect,
}
}

return res, nil
}

func GenerateApiResponse[T types.ApiResponse](resultForBody interface{}) (T, error) {
var buf bytes.Buffer
body, err := json.Marshal(resultForBody)
Expand Down
1 change: 1 addition & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ functions:
- bin/sync_callback
sync_handler:
handler: bin/sync_handler
timeout: 30
events:
- sqs:
arn:
Expand Down

0 comments on commit c909b87

Please sign in to comment.