]> git.frustrated-labs.net Git - squad-rotation-bot.git/commitdiff
feat: containerize bot
authorAlexander Goussas <[email protected]>
Thu, 30 Oct 2025 02:41:08 +0000 (21:41 -0500)
committerAlexander Goussas <[email protected]>
Fri, 31 Oct 2025 03:04:39 +0000 (22:04 -0500)
Dockerfile [new file with mode: 0644]
docker-compose.yaml
main.go

diff --git a/Dockerfile b/Dockerfile
new file mode 100644 (file)
index 0000000..140806c
--- /dev/null
@@ -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"]
+
index b9e44b6b70c3aff67c7f0d9bf37816cb7309a1b9..c97c85ed5db33063396f5c1c6b35fb28be993019 100644 (file)
@@ -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 ae3d4b86e0bf4a28fb1b603df4624fc9add00046..80f5d3563c4f8392ad8e8fe6e8e8cfdcbd98ca2a 100644 (file)
--- 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)
 }