From 22d0cb7f79eef538ea9fd30e672a4ca28ba328ce Mon Sep 17 00:00:00 2001 From: Adam French Date: Thu, 20 Nov 2025 18:04:57 +0000 Subject: [PATCH] adding environment variables --- .gitignore | 1 + backend/main.go | 28 ++++++++++++++++++++++------ docker-compose.yml | 26 ++++++++++++++++++-------- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 8d3b4fb..005a66b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ certbot/ +.env diff --git a/backend/main.go b/backend/main.go index 985ba5a..8b03ff6 100644 --- a/backend/main.go +++ b/backend/main.go @@ -1,7 +1,9 @@ package main import ( + "fmt" "log" + "os" "gorm.io/driver/postgres" "gorm.io/gorm" @@ -13,7 +15,17 @@ import ( ) func connectToPostgreSQL() (*gorm.DB, error) { - dsn := "user=postgres password=password dbname=db host=localhost port=5432 sslmode=disable" + user := os.Getenv("POSTGRES_USER") + password := os.Getenv("POSTGRES_PASSWORD") + dbname := os.Getenv("POSTGRES_DB") + host := os.Getenv("POSTGRES_HOST") + port := os.Getenv("DB_PORT") + + dsn := fmt.Sprintf( + "user=%s password=%s dbname=%s host=%s port=%s sslmode=disable", + user, password, dbname, host, port, + ) + db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) if err != nil { return nil, err @@ -47,15 +59,19 @@ func main() { } r := gin.Default() - h := handlers.Handler{DB: db} + store := handlers.Store{DB: db} - r.GET("/posts", h.GetPosts) - r.POST("/posts", h.CreatePost) - r.PUT("/posts/:id", h.UpdatePost) + r.GET("/posts", store.GetPosts) + r.POST("/posts", store.CreatePost) + r.PUT("/posts/:id", store.UpdatePost) + + r.GET("/spotify", store.ListeningTo) + r.POST("/spotify", store.SendSong) r.GET("/", func(c *gin.Context) { c.JSON(200, gin.H{"message": "Hello World"}) }) - r.Run() + port := os.Getenv("PORT") + r.Run(fmt.Sprintf(":%s", port)) } diff --git a/docker-compose.yml b/docker-compose.yml index 939a094..b764ff0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,6 +3,8 @@ services: container_name: nginx restart: unless-stopped image: nginx + networks: + - app-network ports: - 80:80 - 443:443 @@ -19,6 +21,8 @@ services: - ./certbot/conf:/etc/letsencrypt - ./certbot/www:/var/www/certbot command: certonly --webroot -w /var/www/certbot --email adam.a.french@outlook.com -d adam-french.co.uk --agree-tos + networks: + - app-network backend: build: @@ -26,16 +30,22 @@ services: dockerfile: Dockerfile container_name: backend restart: unless-stopped - ports: - - 8080:8080 + # ports: + # - "${BACKEND_PORT}:8080" + depends_on: + - db + networks: + - app-network db: image: postgres:16 - container_name: db + container_name: "${POSTGRES_HOST}" restart: unless-stopped environment: - POSTGRES_USER: postgres - POSTGRES_PASSWORD: password - POSTGRES_DB: db - ports: - - 5432:5432 + POSTGRES_USER: "${POSTGRES_USER}" + POSTGRES_PASSWORD: "${POSTGRES_PASSWORD}" + POSTGRES_DB: "${POSTGRES_DB}" + # ports: + # - "${DB_PORT}:5432" + networks: + - app-network