A Go microservice for managing user address directory with full CRUD operations and search functionality.
- ✅ Create, Read, Update, Delete user addresses
- ✅ Search addresses by name, phone, email, city, or keyword
- ✅ RESTful API with JSON responses
- ✅ In-memory database (similar to H2)
- ✅ Health check endpoints via
/actuator - ✅ Code coverage with 80%+ threshold
- ✅ Docker containerization
- ✅ Helm chart for Kubernetes deployment
- ✅ Azure DevOps CI/CD pipeline
- Go 1.21+
- Gorilla Mux (HTTP router)
- go-playground/validator (validation)
- testify (testing)
- In-memory storage (thread-safe)
- Go 1.21+
# Clone the repository
git clone <repository-url>
cd go-addressbook-api
# Download dependencies
go mod download
# Build the application
go build -o addressbook-api ./cmd/server
# Run the application
./addressbook-apiThe application will start at http://localhost:8080
# Build
make build
# Run
make run
# Run tests
make test
# Run tests with coverage
make coverage
# Check coverage threshold (80%)
make coverage-check# Build Docker image
docker build -t addressbook-api .
# Run container
docker run -p 8080:8080 addressbook-api| Method | Endpoint | Description |
|---|---|---|
| POST | /api/addresses |
Create a new address |
| GET | /api/addresses |
Get all addresses |
| GET | /api/addresses/{id} |
Get address by ID |
| PUT | /api/addresses/{id} |
Update address |
| DELETE | /api/addresses/{id} |
Delete address |
| GET | /api/addresses/search?q={keyword} |
Search addresses |
| GET | /api/addresses/search/name?name={name} |
Search by name |
| GET | /api/addresses/search/city?city={city} |
Search by city |
Access the interactive API documentation at:
- Swagger UI: http://localhost:8080/swagger/index.html
- OpenAPI JSON: http://localhost:8080/swagger/doc.json
To regenerate Swagger docs after making changes to annotations:
swag init -g cmd/server/main.go -o docscurl -X POST http://localhost:8080/api/addresses \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"phone": "1234567890",
"email": "john@example.com",
"street": "123 Main St",
"city": "New York",
"state": "NY",
"zipCode": "10001",
"country": "USA"
}'curl http://localhost:8080/api/addressescurl http://localhost:8080/api/addresses/1curl -X PUT http://localhost:8080/api/addresses/1 \
-H "Content-Type: application/json" \
-d '{
"name": "John Smith",
"phone": "9876543210",
"email": "john.smith@example.com",
"street": "456 Oak Ave",
"city": "Los Angeles",
"state": "CA",
"zipCode": "90001",
"country": "USA"
}'curl -X DELETE http://localhost:8080/api/addresses/1# Search across all fields (name, phone, email, address, city, etc.)
curl "http://localhost:8080/api/addresses/search?q=John"curl "http://localhost:8080/api/addresses/search/name?name=John"curl "http://localhost:8080/api/addresses/search/city?city=New%20York"curl http://localhost:8080/actuator/healthAccess health endpoints at:
# Run all tests
go test ./... -v
# Run tests with coverage
go test ./... -coverprofile=coverage.out -covermode=atomic
# View coverage report
go tool cover -func=coverage.out
# Generate HTML coverage report
go tool cover -html=coverage.out -o coverage.htmlgo-addressbook-api/
├── cmd/
│ └── server/
│ └── main.go # Application entry point
├── internal/
│ ├── config/
│ │ └── config.go # Configuration management
│ ├── handlers/
│ │ ├── address_handler.go # HTTP handlers for address endpoints
│ │ ├── health_handler.go # Health check handlers
│ │ └── handlers_test.go # Handler unit tests
│ ├── middleware/
│ │ └── middleware.go # Logging, error handling middleware
│ ├── models/
│ │ └── address.go # Address model and validation
│ ├── repository/
│ │ ├── address_repo.go # In-memory address repository
│ │ └── address_repo_test.go # Repository unit tests
│ └── service/
│ ├── address_service.go # Business logic layer
│ └── address_service_test.go # Service unit tests
├── tests/
│ ├── integration/
│ │ └── api_integration_test.go # Integration tests
│ └── e2e/
│ └── api_e2e_test.go # End-to-end tests
├── helm/
│ └── addressbook/ # Helm chart files
├── Dockerfile
├── azure-pipelines.yml # CI/CD pipeline
├── Makefile
├── go.mod
├── go.sum
└── README.md
To build with a specific version:
# Go build
go build -ldflags="-s -w -X main.version=1.0.0" -o addressbook-api ./cmd/server
# Docker
docker build --build-arg VERSION=1.0.0 -t addressbook-api:1.0.0 .The Azure DevOps pipeline (azure-pipelines.yml) handles:
- Build and test with code coverage
- Docker image build and push to ACR
- Deploy to Azure Container Instances
The GitHub Actions workflow (.github/workflows/ci-cd.yml) provides:
- Build & Test: Compiles code, runs tests with coverage
- Docker: Builds and pushes image to GitHub Container Registry (ghcr.io)
- Security: Runs Gosec security scanner
- Lint: Runs golangci-lint
- Deploy: Helm deployment to Kubernetes (configurable)
# Install
helm install addressbook ./helm/addressbook
# Upgrade
helm upgrade addressbook ./helm/addressbook
# Uninstall
helm uninstall addressbookEnvironment variables:
| Variable | Description | Default |
|---|---|---|
SERVER_PORT |
Application port | 8080 |
SERVER_HOST |
Server host binding | (empty = all interfaces) |
Apache 2.0