-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
80 lines (63 loc) · 2.16 KB
/
Copy pathmain.go
File metadata and controls
80 lines (63 loc) · 2.16 KB
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
package main
import (
"context"
"encoding/json"
"log"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
ginadapter "github.com/awslabs/aws-lambda-go-api-proxy/gin"
"github.com/datphamcode295/go-lambda-pulumi/internal/adapters/handler"
"github.com/datphamcode295/go-lambda-pulumi/internal/adapters/repository"
"github.com/datphamcode295/go-lambda-pulumi/internal/config"
"github.com/datphamcode295/go-lambda-pulumi/internal/core/domain"
"github.com/datphamcode295/go-lambda-pulumi/internal/core/services"
"github.com/datphamcode295/go-lambda-pulumi/internal/logger"
util "github.com/datphamcode295/go-lambda-pulumi/internal/utils"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
"github.com/jinzhu/gorm"
_ "github.com/lib/pq"
)
var (
patientService *services.PatientService
ginLambda *ginadapter.GinLambdaV2
)
func Handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
// parse json request for debugging
reqJson, err := json.Marshal(req)
if err != nil {
log.Println("Error marshalling request", err)
}
log.Println("Request received", string(reqJson))
return ginLambda.ProxyWithContext(ctx, req)
}
func init() {
cfg := config.NewConfig()
db, err := gorm.Open("postgres", cfg.DatabaseURL)
if err != nil {
panic(err)
}
logger.SetupLogger()
// Create or modify the database tables based on the model structs found in the imported package
db.AutoMigrate(&domain.User{}, &domain.Patient{}, &domain.Transaction{})
store := repository.NewDB(db)
patientService = services.NewPatientService(cfg, store, store)
InitRoutes()
}
func InitRoutes() {
router := gin.Default()
// Register custom validator
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
v.RegisterValidation("ddmmyyyy", util.ValidateDDMMYYYY)
}
pprof.Register(router)
v1 := router.Group("/app")
patientHandler := handler.NewPatientHandler(*patientService)
v1.POST("/patients/pay-transaction", patientHandler.PayTransaction)
ginLambda = ginadapter.NewV2(router)
}
func main() {
lambda.Start(Handler)
}