Skip to content

Commit

Permalink
Add a simple way to run both app and database using docker compose
Browse files Browse the repository at this point in the history
  • Loading branch information
davidianstyle committed Feb 27, 2024
1 parent a311871 commit 9ca8a5a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ MYSQL_PORT=[3306]
DB_USER=[root]
DB_PASSWORD=[password]
DB_NAME=[local_db]
DB_CONNECTION_NAME=localhost:$MYSQL_PORT
DB_CONNECTION_NAME=[mysql:$MYSQL_PORT]
DB_CONNECTION_STRING=$DB_USER:$DB_PASSWORD@tcp($DB_CONNECTION_NAME)/$DB_NAME?charset=utf8&parseTime=True&loc=Local
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21
FROM golang:latest

WORKDIR /usr/src/app

Expand Down
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,29 @@
RESTful API for interacting with David Chang programmatically

## Build & run locally (http://localhost:80)
### Configure

### Configure, build, and run using `docker compose`
`$ cp .env.example .env`
- Fill out all the details for your local database (replace examples in brackets):
- ENVIRONMENT=[development]
- MYSQL_PORT=[3306]
- DB_USER=[root]
- DB_PASSWORD=[password]
- DB_NAME=[local_db]
- DB_CONNECTION_NAME=[mysql:$MYSQL_PORT]
- DB_CONNECTION_STRING=$DB_USER:$DB_PASSWORD@tcp($DB_CONNECTION_NAME)/$DB_NAME?charset=utf8&parseTime=True&loc=Local
- Populate your local environment
`$ source .env`

### Configure, build, and run manually
`$ cp .env.example .env`
- Fill out all the details for your local database (replace examples in brackets):
- ENVIRONMENT=[development]
- MYSQL_PORT=[3306]
- DB_USER=[root]
- DB_PASSWORD=[password]
- DB_NAME=[local_db]
- DB_CONNECTION_NAME=localhost:$MYSQL_PORT
- DB_CONNECTION_NAME=[localhost:$MYSQL_PORT]
- DB_CONNECTION_STRING=$DB_USER:$DB_PASSWORD@tcp($DB_CONNECTION_NAME)/$DB_NAME?charset=utf8&parseTime=True&loc=Local
- Populate your local environment
`$ source .env`
Expand All @@ -20,9 +34,9 @@ RESTful API for interacting with David Chang programmatically
`$ docker exec -it local_mysql mysql -u $DB_USER -p`
(enter password when prompted $DB_PASSWORD)
`CREATE DATABASE [$DB_NAME]` (replace [$DB_NAME] with password you set up)
### Build
#### Build
`$ docker build -t dev-davidchang-api .`
### Run
#### Run
`$ docker run -p 80:8080 -it --rm --name dev-dc-api dev-davidchang-api`

## API Design (https://api.davidchang.dev)
Expand Down
33 changes: 33 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
version: '3'

services:
api:
build:
context: .
ports:
- "80:8080" # Map host's port 80 to container's port 8080
networks:
- api-network
depends_on:
mysql:
condition: service_healthy
environment:
DB_CONNECTION_STRING: "$DB_USER:$DB_PASSWORD@tcp($DB_CONNECTION_NAME)/$DB_NAME?charset=utf8&parseTime=True&loc=UTC"

mysql:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_DATABASE: ${DB_NAME}
ports:
- "3306:3306"
networks:
- api-network
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
timeout: 10s
retries: 10

networks:
api-network:
driver: bridge
26 changes: 20 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"os"
"strings"
"time"

"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
Expand Down Expand Up @@ -71,20 +72,33 @@ func setupDB() error {
connectionString = fmt.Sprintf("%s:%s@unix(/cloudsql/%s)/%s?charset=utf8&parseTime=True&loc=Local",
dbUser, dbPassword, dbConnectionName, dbName)
}

// Open a database connection
database, err := gorm.Open(mysql.Open(connectionString), &gorm.Config{})
var err error
for i := 0; i < 10; i++ { // Attempt to connect 10 times with a delay between attempts
db, err = gorm.Open(mysql.Open(connectionString), &gorm.Config{
// Set the time zone
NowFunc: func() time.Time {
return time.Now().UTC()
},
})
if err == nil {
break
}

log.Printf("Error connecting to the database: %v. Retrying in 5 seconds...", err)
time.Sleep(5 * time.Second)
}

if err != nil {
return fmt.Errorf("failed to connect to database: %v", err)
return fmt.Errorf("failed to connect to the database after multiple attempts: %v", err)
}

// Auto Migrate the resumé model, Resume
if err := database.AutoMigrate(&Resume{}); err != nil {
if err := db.AutoMigrate(&Resume{}); err != nil {
return fmt.Errorf("failed to migrate database: %v", err)
}

// Assign the database instance to the global variable
db = database

return nil
}

Expand Down

0 comments on commit 9ca8a5a

Please sign in to comment.