/
main.go
107 lines (91 loc) · 2.95 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"context"
"os"
"strings"
"github.com/aws/aws-lambda-go/events"
lambda "github.com/aws/aws-lambda-go/lambda"
"github.com/ptilotta/twitterGo/awsgo"
"github.com/ptilotta/twitterGo/bd"
"github.com/ptilotta/twitterGo/handlers"
"github.com/ptilotta/twitterGo/models"
"github.com/ptilotta/twitterGo/secretmanager"
)
func main() {
lambda.Start(EjecutoLambda)
}
func EjecutoLambda(ctx context.Context, request events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error) {
var res *events.APIGatewayProxyResponse
awsgo.InicializoAWS()
if !ValidoParametros() {
res = &events.APIGatewayProxyResponse{
StatusCode: 400,
Body: "Error en las variables de entorno. deben incluir 'SecretName', 'BucketName', 'UrlPrefix'",
Headers: map[string]string{
"Content-Type": "application/json",
},
}
return res, nil
}
SecretModel, err := secretmanager.GetSecret(os.Getenv("SecretName"))
if err != nil {
res = &events.APIGatewayProxyResponse{
StatusCode: 400,
Body: "Error en la lectura de Secret " + err.Error(),
Headers: map[string]string{
"Content-Type": "application/json",
},
}
return res, nil
}
path := strings.Replace(request.PathParameters["twittergo"], os.Getenv("UrlPrefix"), "", -1)
awsgo.Ctx = context.WithValue(awsgo.Ctx, models.Key("path"), path)
awsgo.Ctx = context.WithValue(awsgo.Ctx, models.Key("method"), request.HTTPMethod)
awsgo.Ctx = context.WithValue(awsgo.Ctx, models.Key("user"), SecretModel.Username)
awsgo.Ctx = context.WithValue(awsgo.Ctx, models.Key("password"), SecretModel.Password)
awsgo.Ctx = context.WithValue(awsgo.Ctx, models.Key("host"), SecretModel.Host)
awsgo.Ctx = context.WithValue(awsgo.Ctx, models.Key("database"), SecretModel.Database)
awsgo.Ctx = context.WithValue(awsgo.Ctx, models.Key("jwtSign"), SecretModel.JWTSign)
awsgo.Ctx = context.WithValue(awsgo.Ctx, models.Key("body"), request.Body)
awsgo.Ctx = context.WithValue(awsgo.Ctx, models.Key("bucketName"), os.Getenv("BucketName"))
// Chequeo Conexión a la BD o Conecto la BD
err = bd.ConectarBD(awsgo.Ctx)
if err != nil {
res = &events.APIGatewayProxyResponse{
StatusCode: 500,
Body: "Error conectando la BD " + err.Error(),
Headers: map[string]string{
"Content-Type": "application/json",
},
}
return res, nil
}
respAPI := handlers.Manejadores(awsgo.Ctx, request)
if respAPI.CustomResp == nil {
res = &events.APIGatewayProxyResponse{
StatusCode: respAPI.Status,
Body: respAPI.Message,
Headers: map[string]string{
"Content-Type": "application/json",
},
}
return res, nil
} else {
return respAPI.CustomResp, nil
}
}
func ValidoParametros() bool {
_, traeParametro := os.LookupEnv("SecretName")
if !traeParametro {
return traeParametro
}
_, traeParametro = os.LookupEnv("BucketName")
if !traeParametro {
return traeParametro
}
_, traeParametro = os.LookupEnv("UrlPrefix")
if !traeParametro {
return traeParametro
}
return traeParametro
}