adding environment variables
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
certbot/
|
certbot/
|
||||||
|
.env
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
"gorm.io/driver/postgres"
|
"gorm.io/driver/postgres"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -13,7 +15,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func connectToPostgreSQL() (*gorm.DB, error) {
|
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{})
|
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -47,15 +59,19 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
h := handlers.Handler{DB: db}
|
store := handlers.Store{DB: db}
|
||||||
|
|
||||||
r.GET("/posts", h.GetPosts)
|
r.GET("/posts", store.GetPosts)
|
||||||
r.POST("/posts", h.CreatePost)
|
r.POST("/posts", store.CreatePost)
|
||||||
r.PUT("/posts/:id", h.UpdatePost)
|
r.PUT("/posts/:id", store.UpdatePost)
|
||||||
|
|
||||||
|
r.GET("/spotify", store.ListeningTo)
|
||||||
|
r.POST("/spotify", store.SendSong)
|
||||||
|
|
||||||
r.GET("/", func(c *gin.Context) {
|
r.GET("/", func(c *gin.Context) {
|
||||||
c.JSON(200, gin.H{"message": "Hello World"})
|
c.JSON(200, gin.H{"message": "Hello World"})
|
||||||
})
|
})
|
||||||
|
|
||||||
r.Run()
|
port := os.Getenv("PORT")
|
||||||
|
r.Run(fmt.Sprintf(":%s", port))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ services:
|
|||||||
container_name: nginx
|
container_name: nginx
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
image: nginx
|
image: nginx
|
||||||
|
networks:
|
||||||
|
- app-network
|
||||||
ports:
|
ports:
|
||||||
- 80:80
|
- 80:80
|
||||||
- 443:443
|
- 443:443
|
||||||
@@ -19,6 +21,8 @@ services:
|
|||||||
- ./certbot/conf:/etc/letsencrypt
|
- ./certbot/conf:/etc/letsencrypt
|
||||||
- ./certbot/www:/var/www/certbot
|
- ./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
|
command: certonly --webroot -w /var/www/certbot --email adam.a.french@outlook.com -d adam-french.co.uk --agree-tos
|
||||||
|
networks:
|
||||||
|
- app-network
|
||||||
|
|
||||||
backend:
|
backend:
|
||||||
build:
|
build:
|
||||||
@@ -26,16 +30,22 @@ services:
|
|||||||
dockerfile: Dockerfile
|
dockerfile: Dockerfile
|
||||||
container_name: backend
|
container_name: backend
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
ports:
|
# ports:
|
||||||
- 8080:8080
|
# - "${BACKEND_PORT}:8080"
|
||||||
|
depends_on:
|
||||||
|
- db
|
||||||
|
networks:
|
||||||
|
- app-network
|
||||||
|
|
||||||
db:
|
db:
|
||||||
image: postgres:16
|
image: postgres:16
|
||||||
container_name: db
|
container_name: "${POSTGRES_HOST}"
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_USER: postgres
|
POSTGRES_USER: "${POSTGRES_USER}"
|
||||||
POSTGRES_PASSWORD: password
|
POSTGRES_PASSWORD: "${POSTGRES_PASSWORD}"
|
||||||
POSTGRES_DB: db
|
POSTGRES_DB: "${POSTGRES_DB}"
|
||||||
ports:
|
# ports:
|
||||||
- 5432:5432
|
# - "${DB_PORT}:5432"
|
||||||
|
networks:
|
||||||
|
- app-network
|
||||||
|
|||||||
Reference in New Issue
Block a user