From: Alexander Goussas Date: Thu, 30 Oct 2025 02:41:08 +0000 (-0500) Subject: feat: containerize bot X-Git-Url: http://git.frustrated-labs.net/?a=commitdiff_plain;h=325510eaf72a05ff7a4d6ecb031f6fc5f11b235c;p=squad-rotation-bot.git feat: containerize bot --- diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..140806c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM golang:1.25 + +WORKDIR /usr/src/app + +COPY go.mod go.sum ./ +RUN go mod download + +COPY . . +RUN go build -v -o /usr/local/bin/app . + +CMD ["app"] + diff --git a/docker-compose.yaml b/docker-compose.yaml index b9e44b6..c97c85e 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,4 +1,16 @@ services: + bot: + image: "aloussase69/squad-rotation-bot" + build: + context: . + environment: + WEB_HOOK_URL: "${WEB_HOOK_URL:-https://example.com}" + DATABASE_URL: "${DB_URL:-postgresql://postgres:postgres@db:5432/postgres?application_name=squad_rotation_bot}" + ports: + - "8080:8080" + depends_on: + - db + db: image: "postgres:17" restart: unless-stopped diff --git a/main.go b/main.go index ae3d4b8..80f5d35 100644 --- a/main.go +++ b/main.go @@ -3,40 +3,62 @@ package main import ( "context" "log" + "net/http" + "time" "github.com/aloussase/squad-rotation-bot/config" "github.com/aloussase/squad-rotation-bot/services" "github.com/jackc/pgx/v5" ) +func connectDB(dbUrl string) *pgx.Conn { + attempts := 0 + + var conn *pgx.Conn + var err error + + for attempts < 3 { + conn, err = pgx.Connect(context.Background(), dbUrl) + if err == nil { + break + } + if attempts == 2 { + log.Fatalf("There was an error while trying to connect to the database: %s", err) + } + time.Sleep(3 * time.Second) + } + + return conn +} + func main() { config, err := config.ReadConfig() if err != nil { log.Fatalf("There was an error while reading the config: %s", err) } - conn, err := pgx.Connect(context.Background(), config.DatabaseUrl) - if err != nil { - log.Fatalf("There was an error while trying to connect to the database: %s", err) - } - + conn := connectDB(config.DatabaseUrl) defer conn.Close(context.Background()) memberService := services.Create(conn) rotationService := services.CreateRotationService(conn) messagingService := services.CreateMessagingService(config) - members, err := memberService.ListMembers() - if err != nil { - log.Fatalf("There was an error while trying to list members: %s", err) - } + http.HandleFunc("/api/v1/rotation/trigger", func(w http.ResponseWriter, r *http.Request) { + members, err := memberService.ListMembers() + if err != nil { + log.Fatalf("There was an error while trying to list members: %s", err) + } - chosenOne, err := rotationService.ChooseNextInRotation(members) - if err != nil { - log.Fatalf("There was an error while trying to choose next in rotation: %s", err) - } + chosenOne, err := rotationService.ChooseNextInRotation(members) + if err != nil { + log.Fatalf("There was an error while trying to choose next in rotation: %s", err) + } - if messagingService.SendRotationNotification(chosenOne) != nil { - log.Fatalf("There was an error while trying to send a rotation: %s", err) - } + if messagingService.SendRotationNotification(chosenOne) != nil { + log.Fatalf("There was an error while trying to send a rotation: %s", err) + } + }) + + http.ListenAndServe(":8080", nil) }