diff --git a/.env.postgres b/.env.postgres index f393b2e..7a74b4b 100644 --- a/.env.postgres +++ b/.env.postgres @@ -4,3 +4,4 @@ POSTGRES_DB=postgres POSTGRES_HOST=postgres POSTGRES_PORT=5432 DATABASE_URL=postgresql://postgres:Welcome@localhost:5432/postgres +# if you run using docker run --name pgsql-dev -e POSTGRES_PASSWORD=Welcome -p 5432:5432 postgres:latest \ No newline at end of file diff --git a/.env_aws.template b/.env_aws.template new file mode 100644 index 0000000..1f04ed3 --- /dev/null +++ b/.env_aws.template @@ -0,0 +1,9 @@ +AWS_ACCESS_KEY_ID= +AWS_SECRET_ACCESS_KEY= +AWS_REGION= +ECS_VPC= +SUBNET1= +SUBNET2= +SUBNET3= +JWT_SECRET= +HOST_URL= \ No newline at end of file diff --git a/pkg/core/helpers.go b/pkg/core/helpers.go index 7eba9c3..edd8d4b 100644 --- a/pkg/core/helpers.go +++ b/pkg/core/helpers.go @@ -8,6 +8,7 @@ func generateName(UserName string, Image, suffix string) string { } return UserName + "-" + strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(Image, "/", "-"), ".", "-"), ":", "-") + "-" + suffix } -func generateNameFromImage(Image string) string { - return strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(Image, "/", "-"), ".", "-"), ":", "-") -} + +// func generateNameFromImage(Image string) string { +// return strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(Image, "/", "-"), ".", "-"), ":", "-") +// } diff --git a/pkg/core/service-ops.go b/pkg/core/service-ops.go index 57a0dea..6a6116e 100644 --- a/pkg/core/service-ops.go +++ b/pkg/core/service-ops.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "log" + "os" "strings" "time" @@ -87,12 +88,12 @@ func CreateService(svc *ecs.Client, elbSvc *elbv2.ELBV2, UserName string, Image NetworkConfiguration: &types.NetworkConfiguration{ AwsvpcConfiguration: &types.AwsVpcConfiguration{ Subnets: []string{ - // os.Getenv("SUBNET1"), - // os.Getenv("SUBNET2"), - // os.Getenv("SUBNET3"), - "subnet-03f664a0d4fe40293", - "subnet-01850c7c6f49dfb7f", - "subnet-0450df2a14564e3d5", + *aws.String(os.Getenv("SUBNET1")), + *aws.String(os.Getenv("SUBNET2")), + *aws.String(os.Getenv("SUBNET3")), + // "subnet-03f664a0d4fe40293", + // "subnet-01850c7c6f49dfb7f", + // "subnet-0450df2a14564e3d5", }, AssignPublicIp: types.AssignPublicIpEnabled, SecurityGroups: []string{"sg-0d1526e6316cb2abf"}, @@ -169,7 +170,6 @@ func DeleteService(elbSvc *elbv2.ELBV2, svc *ecs.Client, service *byocTypes.Serv return fmt.Errorf("unable to delete service: %v", err) } log.Printf("Service %v deleted from AWS", service.Name) - // image and service are of same name if service.LoadBalancerARN != "" { err = DeleteLoadBalancerARN(elbSvc, &service.LoadBalancerARN) if err != nil { diff --git a/pkg/handlers/health.go b/pkg/handlers/health.go new file mode 100644 index 0000000..b4bd233 --- /dev/null +++ b/pkg/handlers/health.go @@ -0,0 +1,47 @@ +package handlers + +import ( + "os" + + "github.com/g4ze/byoc/pkg/database/db" + "github.com/gin-gonic/gin" + "github.com/joho/godotenv" +) + +// health should check the health of the service +// database activity, tables +// aws keys and connection +// other related env variables +// and return errors accordingly +func Health(c *gin.Context) { + err := godotenv.Load() + if err != nil { + c.JSON(500, gin.H{"error": "Error loading .env file", "message": err.Error()}) + return + } + AWS_REGION := os.Getenv("AWS_REGION") + AWS_ACCESS_KEY_ID := os.Getenv("AWS_ACCESS_KEY_ID") + AWS_SECRET_ACCESS_KEY := os.Getenv("AWS_SECRET_ACCESS_KEY") + ECS_VPC := os.Getenv("ECS_VPC") + SUBNET1 := os.Getenv("SUBNET1") + SUBNET2 := os.Getenv("SUBNET2") + SUBNET3 := os.Getenv("SUBNET3") + JWT_SECRET := os.Getenv("JWT_SECRET") + if AWS_REGION == "" || AWS_ACCESS_KEY_ID == "" || AWS_SECRET_ACCESS_KEY == "" || ECS_VPC == "" || SUBNET1 == "" || SUBNET2 == "" || SUBNET3 == "" || JWT_SECRET == "" { + c.JSON(500, gin.H{"error": "Missing env variables"}) + } + + // databse pinging + client := db.NewClient() + if err := client.Prisma.Connect(); err != nil { + c.JSON(500, gin.H{"error": "Error connecting to database", "message": err.Error()}) + return + } + func() { + if err := client.Prisma.Disconnect(); err != nil { + c.JSON(500, gin.H{"error": "Error disconnecting from database", "message": err.Error()}) + } + }() + c.JSON(200, gin.H{"message": "Service is healthy"}) + +} diff --git a/pkg/routes/routes.go b/pkg/routes/routes.go index c82d7d3..ab2dc60 100644 --- a/pkg/routes/routes.go +++ b/pkg/routes/routes.go @@ -30,6 +30,7 @@ func Server() { r.POST("/create-user", handlers.Create_User) r.POST("/login", handlers.Login) r.GET("/get-lbdns", handlers.Get_LBDNS) + r.GET("/health", handlers.Health) // Create a new group for routes that require JWT middleware authRoutes.POST("/whoami", handlers.WhoAMI) authRoutes.POST("/make-cluster", handlers.Make_Cluster)