Add env example, swagger, and Dockerfile for seeder api
This commit is contained in:
parent
2bfda1fc5e
commit
52e2706d3e
5 changed files with 139 additions and 1 deletions
2
seeder-api/.env.example
Normal file
2
seeder-api/.env.example
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
PORT=8080
|
||||||
|
DATABASE_URL=postgres://user:password@localhost:5432/saveinmed?sslmode=disable
|
||||||
24
seeder-api/Dockerfile
Normal file
24
seeder-api/Dockerfile
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
# syntax=docker/dockerfile:1.7
|
||||||
|
FROM golang:1.22-alpine AS builder
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
RUN apk add --no-cache ca-certificates
|
||||||
|
|
||||||
|
COPY go.mod go.sum ./
|
||||||
|
RUN go mod download
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
|
||||||
|
go build -trimpath -ldflags="-s -w" -o seeder-api ./main.go
|
||||||
|
|
||||||
|
FROM gcr.io/distroless/static-debian12:nonroot
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
COPY --from=builder /app/seeder-api /app/seeder-api
|
||||||
|
|
||||||
|
USER nonroot:nonroot
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
ENTRYPOINT ["/app/seeder-api"]
|
||||||
|
|
@ -47,7 +47,7 @@ Use este modo para desenvolvimento diário. Ele cria as seguintes credenciais de
|
||||||
- Go 1.22+
|
- Go 1.22+
|
||||||
|
|
||||||
### 1. Configurar
|
### 1. Configurar
|
||||||
Certifique-se que o `.env` ou variáveis de ambiente estão corretas:
|
Use o arquivo `.env.example` como base e configure as variáveis:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
export DATABASE_URL=postgres://user:password@localhost:5432/saveinmed?sslmode=disable
|
export DATABASE_URL=postgres://user:password@localhost:5432/saveinmed?sslmode=disable
|
||||||
|
|
@ -73,6 +73,26 @@ _Saída esperada:_
|
||||||
curl -X POST "http://localhost:8216/seed"
|
curl -X POST "http://localhost:8216/seed"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## 📜 Swagger/OpenAPI
|
||||||
|
O documento OpenAPI está disponível em:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl http://localhost:8216/swagger.json
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🐳 Docker (imagem otimizada)
|
||||||
|
Build:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker build -t saveinmed-seeder-api .
|
||||||
|
```
|
||||||
|
|
||||||
|
Run:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run --rm -p 8216:8080 --env-file .env saveinmed-seeder-api
|
||||||
|
```
|
||||||
|
|
||||||
## ⚡ Fluxo Recomendado de Trabalho
|
## ⚡ Fluxo Recomendado de Trabalho
|
||||||
1. Pare o backend principal (`backend-go`) para liberar conexões com o banco.
|
1. Pare o backend principal (`backend-go`) para liberar conexões com o banco.
|
||||||
2. Rode o seeder: `go run main.go` em um terminal.
|
2. Rode o seeder: `go run main.go` em um terminal.
|
||||||
|
|
|
||||||
78
seeder-api/docs/openapi.json
Normal file
78
seeder-api/docs/openapi.json
Normal file
|
|
@ -0,0 +1,78 @@
|
||||||
|
{
|
||||||
|
"openapi": "3.0.3",
|
||||||
|
"info": {
|
||||||
|
"title": "SaveInMed Seeder API",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "API utilitária para popular o banco de dados com dados de teste."
|
||||||
|
},
|
||||||
|
"servers": [
|
||||||
|
{
|
||||||
|
"url": "http://localhost:8080",
|
||||||
|
"description": "Ambiente local"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"paths": {
|
||||||
|
"/seed": {
|
||||||
|
"post": {
|
||||||
|
"summary": "Executa o seed do banco",
|
||||||
|
"description": "Limpa e recria as tabelas, gerando dados de teste.",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"name": "mode",
|
||||||
|
"in": "query",
|
||||||
|
"required": false,
|
||||||
|
"description": "Modo de seed: `lean` para dados de dev ou `full` para carga.",
|
||||||
|
"schema": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": ["lean", "full"],
|
||||||
|
"default": "full"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "Seed executado com sucesso",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"examples": {
|
||||||
|
"lean": {
|
||||||
|
"value": "Lean seed completed. 4 Pharmacies. Users: 13. Pass: 123456 (admin: admin123)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"405": {
|
||||||
|
"description": "Método não permitido"
|
||||||
|
},
|
||||||
|
"500": {
|
||||||
|
"description": "Erro ao executar o seed"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"/swagger.json": {
|
||||||
|
"get": {
|
||||||
|
"summary": "Retorna o Swagger/OpenAPI da Seeder API",
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "Documento OpenAPI",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"type": "object"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"405": {
|
||||||
|
"description": "Método não permitido"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
_ "embed"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -9,6 +10,9 @@ import (
|
||||||
"github.com/saveinmed/seeder-api/pkg/seeder"
|
"github.com/saveinmed/seeder-api/pkg/seeder"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed docs/openapi.json
|
||||||
|
var swaggerJSON string
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
godotenv.Load()
|
godotenv.Load()
|
||||||
port := os.Getenv("PORT")
|
port := os.Getenv("PORT")
|
||||||
|
|
@ -40,6 +44,16 @@ func main() {
|
||||||
w.Write([]byte(result))
|
w.Write([]byte(result))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
http.HandleFunc("/swagger.json", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodGet {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.Write([]byte(swaggerJSON))
|
||||||
|
})
|
||||||
|
|
||||||
log.Printf("Seeder API listening on port %s", port)
|
log.Printf("Seeder API listening on port %s", port)
|
||||||
if err := http.ListenAndServe(":"+port, nil); err != nil {
|
if err := http.ListenAndServe(":"+port, nil); err != nil {
|
||||||
log.Fatalf("Server error: %v", err)
|
log.Fatalf("Server error: %v", err)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue