Dockerized Go API application with DB migrations, GitOps and Kubernetes Kustomize based CD
Dockerized Go API application with DB migrations, GitOps and Kubernetes Kustomize based CD
We build a production-ready containerized RESTful API server application using following packages and tools.
net/http: The most idiomatic way to write web API applications in Go.slog.| Environment | Go Image Type | Go Image Size | Postgres Image Type | Postgres Image Size |
|---|---|---|---|---|
| Development | golang:1.27-alpine | ~ 800 MB | postgres:18-alpine | ~ 300MB |
| Production | distroless/static-debian13:nonroot | ~ 30 MB |
type Handler struct {
validator *validator.Validate
bookRepo IBookRepo
}
func (h *Handler) Register(r chi.Router) {
r.Get("/", h.list)
r.With(m.Validate[Form](h.validator)).Post("/", h.create)
r.Get("/{id}", h.read)
r.With(m.Validate[Form](h.validator)).Put("/{id}", h.update)
r.Delete("/{id}", h.delete)
}
// ...| Name | HTTP Method | Route |
|---|---|---|
| List Books | GET | /v1/books |
| Create Book | POST | /v1/books |
| Read Book | GET | /v1/books/{id} |
| Update Book | PUT | /v1/books/{id} |
| Delete Book | DELETE | /v1/books/{id} |
| Health | GET | /livez |
POST/PUT){
"title": "Harry Potter and the Deathly Hallows",
"description": "It is the seventh and final novel in the Harry Potter series",
"image_url": "https://upload.wikimedia.org/wikipedia/en/a/a9/Harry_Potter_and_the_Deathly_Hallows.jpg",
"published_date": "2007-07-21",
"status": "verified"
}GET/POST/PUT){
"id": "01bbbbbb-bbbb-7bbb-8bbb-bbbbbbbbbbbb",
"created_at": "2027-01-01T00:00:00.123456Z",
"updated_at": "2027-01-01T00:00:00.123456Z",
"published_date": "2007-07-21",
"title": "Harry Potter and the Deathly Hallows",
"description": "It is the seventh and final novel in the Harry Potter series",
"image_url": "https://upload.wikimedia.org/wikipedia/en/a/a9/Harry_Potter_and_the_Deathly_Hallows.jpg"
"status": "verified",
}[!note] The list endpoint returns an array of above response JSON.
{
"errors": {
"image_url": "Must be a valid URL",
"status": "Must be one of pending, verified",
"title": "This field is required",
"published_date": "Must be a valid date"
}
}To keep this simple, we use only a single database table named books.
| Column Name | Datatype | Not Null | Primary Key |
|---|---|---|---|
| created_at | TIMESTAMPTZ | ✅ | |
| updated_at | TIMESTAMPTZ | ✅ | |
| id | UUID | ✅ | ✅ |
| published_date | DATE | ✅ | |
| status | SMALLINT | ✅ | |
| title | TEXT | ✅ | |
| description | TEXT | ||
| image_url | TEXT |
[!important]
- For high-traffic systems with very large PostgreSQL tables that containing millions/billions of rows, arranging fixed-width columns by decreasing alignment requirements can reduce tuple alignment padding; potentially minimize row/ storage size. This technique is called "Column Tetris".
- For this optimization, order fixed-width table columns by decreasing alignment requirements.
- 8-byte alignment types:
bigint,bigserial,double precision/float8,timestamp,timestamptz,time,interval- 4-byte alignment types:
integer,serial,real/float4,uuid,date- 2-byte alignment types:
smallint,smallserial- 1-byte alignment types:
boolean- Variable-width types (at last):
numeric,text,character varying/varchar,bytea- However, it's ok to follow a more readable column format, when your table schema changes frequently.
………└── k8s
│
├── bootstrap
│ ├── argocd
│ └── argocd-config
│ ├── clusters
│ ├── projects
│ └── applications
│
├── platform
│ ├── metrics-server
│ ├── gateway-api
│ ├── istio-ambient
│ └── cloudnative-pg
│
├── components
│ └── myapp-db
├── services
│ ├── base
│ │ └── myapp
│ └── overlays
│ ├── dev
│ ├── prod
│ └── stage
│
└── gateways[!tip] Sample Kind Dev Cluster
kind create cluster --name dev kubectl apply -k k8s/bootstrap/argocd kubectl apply -k k8s/bootstrap/argocd-config kubectl apply -k k8s/platform/istio-ambient kubectl apply -k k8s/platform/gateway-api kubectl apply -k k8s/gateways kubectl port-forward svc/shared-gateway-dev-istio -n istio-ingress 8081:8081 # Shared Dev Gateway curl -X GET 'localhost:8081/myapp/v1/books' --header 'Accept: application/json' kubectl port-forward svc/argocd-server -n argocd 8080:443 # ArgoCD Dashboard(admin/password)
No open issues yet, or sync has not completed.