Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
g4ze committed Jun 26, 2024
1 parent 9adfc30 commit 045792b
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 10 deletions.
1 change: 1 addition & 0 deletions .env.postgres
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions .env_aws.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_REGION=
ECS_VPC=
SUBNET1=
SUBNET2=
SUBNET3=
JWT_SECRET=
HOST_URL=
7 changes: 4 additions & 3 deletions pkg/core/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "/", "-"), ".", "-"), ":", "-")
// }
14 changes: 7 additions & 7 deletions pkg/core/service-ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"os"
"strings"
"time"

Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -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 {
Expand Down
47 changes: 47 additions & 0 deletions pkg/handlers/health.go
Original file line number Diff line number Diff line change
@@ -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"})

}
1 change: 1 addition & 0 deletions pkg/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 045792b

Please sign in to comment.