Skip to content

Commit

Permalink
🛠️ Reordered the compose.yml file and moved ports to env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
lhbelfanti committed Sep 5, 2024
1 parent 7fe8a1a commit 4ec0cb4
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 26 deletions.
12 changes: 9 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
DB_NAME=<Database name>
DB_USER=<Database username>
DB_PASS=<Database password>
# App settings
APP_EXPOSED_PORT=4100 # Host port
APP_INTERNAL_PORT=4000 # Container port

# Database
DB_NAME=ahbcc
DB_USER=ahbcc_user
DB_PASS=ahbcc_password
DB_PORT=5432
3 changes: 1 addition & 2 deletions Dockerfile_app
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ RUN go mod download
COPY cmd/ ./cmd
COPY internal/ ./internal
COPY migrations/ ./migrations
COPY .env ./

# Build the application and output the binary as 'ahbcc'
RUN CGO_ENABLED=0 GOOS=linux go build -o /ahbcc ./cmd/api

# Expose port
EXPOSE 8090
EXPOSE ${API_PORT}

# Run application
CMD [ "/ahbcc" ]
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile_migrations
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ RUN apk update && apk add --no-cache curl tzdata \
ENV TZ=America/Argentina/Buenos_Aires

# Make HTTP request to /migrations/run/v1 endpoint
CMD ["sh", "-c", "curl -X POST http://app:8090/migrations/run/v1"]
CMD ["sh", "-c", "curl -X POST http://ahbcc:${API_PORT}/migrations/run/v1"]

6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,15 @@ To connect to the database create a `.env` file in the root of the project or re

This file should contain the following environment variables:
```
# App settings
APP_EXPOSED_PORT=<AHBCC Host Port>
APP_INTERNAL_PORT=<AHBCC Container Port>
# Database
DB_NAME=<Database name>
DB_USER=<Database username>
DB_PASS=<Database password>
DB_PORT=<Database port>
```

Replace the `< ... >` by the correct value. For example: `DB_NAME=<Database name>` --> `DB_NAME=ahbcc`.
7 changes: 5 additions & 2 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"fmt"
"log"
"log/slog"
"net/http"
"os"

"ahbcc/cmd/api/migrations"
"ahbcc/cmd/api/ping"
Expand Down Expand Up @@ -36,8 +38,9 @@ func main() {
router.HandleFunc("POST /tweets/v1", tweets.InsertHandlerV1(insertTweets))

/* --- Server --- */
slog.Info("AHBCC server is ready to receive request on port :8090")
err := http.ListenAndServe(":8090", router)
port := fmt.Sprintf(":%s", os.Getenv("API_PORT"))
slog.Info(fmt.Sprintf("AHBCC server is ready to receive request on port %s", port))
err := http.ListenAndServe(port, router)
if err != nil {
log.Fatalf("Could not start server: %s\n", err.Error())
}
Expand Down
42 changes: 26 additions & 16 deletions docker-compose.yml → compose.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
services:
app:
corpus_creator:
build:
context: .
dockerfile: Dockerfile_app
container_name: app
container_name: ahbcc
ports:
- "${APP_EXPOSED_PORT}:${APP_INTERNAL_PORT}"
environment:
API_PORT: ${APP_INTERNAL_PORT:-4001}
POSTGRES_DB_NAME: ${DB_NAME}
POSTGRES_DB_USER: ${DB_USER}
POSTGRES_DB_PASS: ${DB_PASS}
ports:
- 8080:8090
restart: on-failure
POSTGRES_DB_PORT: ${DB_PORT}
env_file:
- .env
volumes:
- .:/app
depends_on:
postgres_db:
condition: service_healthy
networks:
- network
- corpus_creator
restart: on-failure
healthcheck:
test: ["CMD-SHELL", "sh -c 'curl -sSf http://localhost:8090/ping/v1 || exit 1'"]
test: ["CMD-SHELL", "sh -c 'curl -sSf http://localhost:${APP_INTERNAL_PORT}/ping/v1 || exit 1'"]
interval: 5s
timeout: 10s
retries: 5
Expand All @@ -30,36 +34,42 @@ services:
context: .
dockerfile: Dockerfile_migrations
container_name: migrations
restart: "no"
environment:
API_PORT: ${APP_INTERNAL_PORT:-4001}
env_file:
- .env
depends_on:
app:
corpus_creator:
condition: service_healthy
networks:
- network
- corpus_creator
restart: "no"

postgres_db:
image: postgres:latest
container_name: postgres
ports:
- 1234:${DB_PORT:-5432}
environment:
POSTGRES_DB: ${DB_NAME}
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASS}
TZ: "America/Argentina/Buenos_Aires"
ports:
- 1234:5432
env_file:
- .env
volumes:
- database:/var/lib/postgresql/data
- postgres_data:/var/lib/postgresql/data
networks:
- network
- corpus_creator
healthcheck:
test: ["CMD-SHELL", "sh -c 'pg_isready -U \"$DB_USER\" -d \"$DB_NAME\"'"]
interval: 10s
timeout: 10s
retries: 5

volumes:
database:
postgres_data:

networks:
network:
corpus_creator:
driver: bridge
5 changes: 3 additions & 2 deletions internal/database/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ var (
pgOnce sync.Once
)

const databaseURL string = "postgresql://%s:%s@postgres_db:5432/%s?sslmode=disable"
const databaseURL string = "postgresql://%s:%s@postgres_db:%s/%s?sslmode=disable"

func resolveDatabaseURL() string {
dbUser := os.Getenv("POSTGRES_DB_USER")
dbPass := os.Getenv("POSTGRES_DB_PASS")
dbName := os.Getenv("POSTGRES_DB_NAME")
dbPort := os.Getenv("POSTGRES_DB_PORT")

return fmt.Sprintf(databaseURL, dbUser, dbPass, dbName)
return fmt.Sprintf(databaseURL, dbUser, dbPass, dbPort, dbName)
}

0 comments on commit 4ec0cb4

Please sign in to comment.