Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementação para setup com docker compose #7

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM golang:1.21

RUN go install github.com/cespare/reflex@latest

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY . .

COPY entrypoint.sh /app/

RUN chmod +x /app/entrypoint.sh

ENTRYPOINT ["/app/entrypoint.sh"]
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,43 @@ Este projeto é uma aplicação Go que integra com o Google Calendar e tem permi
```
Na primeira vez que você rodar o projeto, você deverá acessar o link será gerado no console para autorizar o seu aplicativo na sua conta Google.

## Instruções para com Docker

Para iniciar a aplicação, execute o comando:

```bash
docker-compose up -d
```

Para parar e remover contêineres, redes, volumes e imagens usadas pelo docker compose, execute o comando:

```bash
docker-compose down --rmi all
```

Para limpar caches e configurações locais, você pode remover os arquivos de configuração e imagens desnecessárias:

```bash
docker system prune -a --volumes
```

## Como Usar

Após iniciar a aplicação, você poderá acessar a interface web no endereço configurado para a aplicação (exemplo: http://localhost:8080).
Para acessar a aplicação:

```bash
http://localhost:8080
```

Para acessar o Jaeger:

```bash
http://localhost:16686/
```

Para acessar o pgAdmin:

```bash
http://localhost:5050/
```

2 changes: 0 additions & 2 deletions cmd/migrate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ func main() {

db := database.GetDB()

db.Exec("CREATE DATABASE faladev;")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Talvez podemos manter essa linha usando um workaround para o postgres para criar o banco somente se ele não existir, pra melhorar a DevEx.

SELECT 'CREATE DATABASE mydb'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'mydb')\gexec

Fonte: https://stackoverflow.com/questions/18389124/simulate-create-database-if-not-exists-for-postgresql

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tenlisboa a ideia é subir tudo pelo docker-compose. O docker compose já cria a base.


db.Exec("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";")

errStudents := db.AutoMigrate(&models.Student{})
Expand Down
62 changes: 62 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
version: '3.9'

services:
postgres:
image: postgres:13
container_name: postgres
environment:
POSTGRES_USER: userpostgres
POSTGRES_PASSWORD: passwordpostgres
POSTGRES_DB: faladev
ports:
- "5432:5432"
volumes:
- postgres-data:/var/lib/postgresql/data

pgadmin:
image: dpage/pgadmin4
container_name: pgadmin
environment:
PGADMIN_DEFAULT_EMAIL: admin@admin.com
PGADMIN_DEFAULT_PASSWORD: admin
ports:
- "5050:80"
volumes:
- ./servers.json:/pgadmin4/servers.json

jaeger:
image: jaegertracing/all-in-one:1.31
container_name: jaeger
ports:
- "5775:5775/udp"
- "6831:6831/udp"
- "6832:6832/udp"
- "5778:5778"
- "16686:16686"
- "14268:14268"
- "14250:14250"
- "9411:9411"

app:
build:
context: .
dockerfile: Dockerfile
container_name: faladev
environment:
DB_HOST: postgres
DB_PORT: 5432
DB_USER: userpostgres
DB_PASSWORD: passwordpostgres
DB_NAME: faladev
DATABASE_URL: postgres://userpostgres:passwordpostgres@postgres:5432/faladev?sslmode=disable
OTEL_EXPORTER_JAEGER_ENDPOINT: http://jaeger:14268/api/traces
ports:
- "8080:8080"
volumes:
- .:/app
depends_on:
- postgres
- jaeger

volumes:
postgres-data:
11 changes: 11 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

set -e

echo "Running database migrations..."

go run cmd/migrate/main.go

echo "Starting the application with reflex..."

exec reflex -r '\.go$' -s -- sh -c "go run cmd/api/main.go"
15 changes: 15 additions & 0 deletions servers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Servers": {
"1": {
"Name": "Postgres Docker",
"Group": "Servers",
"Host": "postgres",
"Port": 5432,
"MaintenanceDB": "faladev",
"Username": "userpostgres",
"Password": "passwordpostgres",
"SSLMode": "prefer",
"ConnectionTimeout": 10
}
}
}
Loading