adding environment variables

This commit is contained in:
2025-11-20 18:04:57 +00:00
parent 9aabff9752
commit 22d0cb7f79
3 changed files with 41 additions and 14 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
certbot/
.env

View File

@@ -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))
}

View File

@@ -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