-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
97 lines (80 loc) · 2.27 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
package main
import (
"context"
"os"
"strings"
"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/gin-gonic/gin"
v1 "github.com/hunoz/maroon-api/api/v1"
"github.com/hunoz/maroon-api/authentication"
"github.com/hunoz/maroon-api/logging"
"github.com/sirupsen/logrus"
)
const (
BETA = "beta"
PROD = "prod"
)
var stage string
var ginLambda *ginadapter.GinLambdaV2
var ginRouter *gin.Engine
func Handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
// If no name is provided in the HTTP request body, throw an error
return ginLambda.ProxyWithContext(ctx, req)
}
func setGinMode() {
switch stageVariable := strings.ToLower(os.Getenv("STAGE")); stageVariable {
case PROD:
stage = stageVariable
gin.SetMode(gin.ReleaseMode)
default:
stage = BETA
}
}
func getRegionAndPoolId() (region string, poolId string) {
var cognitoRegion string
var cognitoPoolId string
if cognitoRegion = os.Getenv("COGNITO_REGION"); cognitoRegion == "" {
logrus.Fatal("'COGNITO_REGION' environment variable not set!")
os.Exit(1)
}
if cognitoPoolId = os.Getenv("COGNITO_POOL_ID"); cognitoPoolId == "" {
logrus.Fatal("'COGNITO_POOL_ID' environment variable not set!")
os.Exit(1)
}
return cognitoRegion, cognitoPoolId
}
func setupRoutes() {
cognitoRegion, cognitoPoolId := getRegionAndPoolId()
auth := authentication.NewAuth(&authentication.Config{
CognitoRegion: cognitoRegion,
CognitoUserPoolID: cognitoPoolId,
})
router := gin.New()
router.Use(logging.JSONLogMiddleware(stage))
router.Use(gin.Recovery())
router.Use(authentication.JWTMiddleware(*auth))
api := router.Group("/api")
v1Api := api.Group("/v1")
v1Api.GET("/console-url", v1.GetConsoleUrl)
v1Api.GET("/assume-role", v1.AssumeRole)
v1Api.GET("/self", v1.GetUserInfo)
ginRouter = router
}
func init() {
setGinMode()
logging.SetLogMode(stage)
logrus.Info("Setting up routes")
setupRoutes()
}
func main() {
if _, exists := os.LookupEnv("AWS_LAMBDA_FUNCTION_NAME"); exists {
logrus.Info("Running in lambda mode")
ginLambda = ginadapter.NewV2(ginRouter)
lambda.Start(Handler)
} else {
logrus.Info("Running in local mode")
ginRouter.Run("127.0.0.1:8080")
}
}