A lightweight RESTful API written in Go (Golang) that demonstrates a clean architecture for CRUD operations using PostgreSQL. The project is containerized using Docker and includes live reload support via Air.
- Language: Go 1.25+
- Database: PostgreSQL
- Hot Reload: Air
- Containerization: Docker & Docker Compose
- Environment Management: .env file (loaded automatically)
CRUD PROJECT ├── cmd/api/ # Application entry point │ ├── main.go # App configuration and startup │ ├── handler.go # HTTP handlers for CRUD endpoints │ └── route.go # Route setup │ ├── internal/ # Internal packages (not exposed externally) │ ├── database/ # Database connection logic │ │ └── db.go │ ├── env/ # Environment variable helpers │ ├── store/ # Data models and queries │ │ ├── note.go │ │ └── error.go │ └── migrations/ # SQL migrations (if any) │ ├── tmp/ # Temporary build or runtime files │ ├── .air.toml # Air configuration (for live reload) ├── .dockerignore # Ignore rules for Docker ├── .env # Environment configuration ├── .gitignore ├── docker-compose.yml # Docker Compose setup ├── Dockerfile # Docker build instructions ├── go.mod # Go module definition ├── go.sum # Go module checksums ├── Makefile # Helper commands (run, build, etc.) └── README.md # Project documentation git clone https://github.com/dmc0001/crud-project.git cd crud-projectEdit .env:
PORT=:8080 DB_DSN=postgres://postgres:password@localhost:5432/note_db?sslmode=disableairVisit: http://localhost:8080/notes
docker compose up --buildThis will start both the API and the PostgreSQL service.
| Method | Endpoint | Description |
|---|---|---|
| GET | /notes | Get latest notes |
| GET | /note?id=1 | Get a note by ID |
| POST | /create | Create a new note |
| PUT | /update?id=1 | Update an existing note |
| DELETE | /delete?id=1 | Delete a note by ID |
{ "title": "My first note", "content": "Hello from Go!" }{ "message": "Note with id 1 has been created", "id": 1 }Dockerfile builds the Go API image:
FROM golang:1.25-alpine WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . RUN go build -o crud cmd/api/main.go EXPOSE 8080 CMD ["./crud"]docker-compose.yml spins up API + DB:
version: "3.9" services: db: image: postgres:16 environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: password POSTGRES_DB: note_db ports: - "5432:5432" volumes: - postgres_data:/var/lib/postgresql/data api: build: . ports: - "8080:8080" env_file: .env depends_on: - db volumes: postgres_data:run: go run ./cmd/api build: go build -o bin/api ./cmd/api docker-up: docker compose up --build docker-down: docker compose downYou can use Postman or curl:
curl -X GET http://localhost:8080/notes curl -X POST http://localhost:8080/create -d '{"title":"Test","content":"Example"}' -H 'Content-Type: application/json'- Add request validation middleware
- Integrate sqlc for fully type-safe idiomatic Go code from SQL.
- Implement JWT authentication
- Add automated tests (unit + integration)
Haitham Attab