This repository demonstrates the implementation of a RESTful API in Go with integration to Azure Blob Storage for handling image uploads and downloads. It also includes Prometheus metrics to monitor the API performance and health. The API leverages popular Go libraries for web routing, environment management, database interactions, and more, following best practices to provide a robust backend service.
- Go Version: Requires Go 1.22.0 or higher.
Here are the primary libraries and tools used:
- Gorilla Mux - For HTTP routing.
- godotenv - For environment variable management.
- lib/pq - PostgreSQL driver.
- Go Playground Validator - For data validation.
- Go JWT - For authentication.
- golang-migrate - For database migrations.
- Azure Storage Blob Go - For managing Azure Blob Storage.
- Prometheus Go Client - For exposing custom metrics collected from the API.
Install all dependencies at once:
go get -u github.com/gorilla/mux github.com/joho/godotenv github.com/lib/pq github.com/go-playground/validator/v10 github.com/golang-jwt/jwt/v5 github.com/DATA-DOG/go-sqlmock github.com/stretchr/testify github.com/google/uuid github.com/sirupsen/logrus github.com/Azure/azure-storage-blob-go/azblob github.com/prometheus/client_golang/prometheus github.com/prometheus/client_golang/prometheus/promhttp
Install golang-migrate on macOS using Homebrew:
brew install golang-migrate
Common tasks can be executed using the Makefile:
make build # Build the application
make test # Run tests
make run # Run the application
make migration # Create a database migration
make migrate-up # Apply migrations
make migrate-down # Revert migrations
make swagger # Generate Swagger documentation
Set up Delve for sophisticated debugging:
go install github.com/go-delve/delve/cmd/dlv@latest
make debug # Start the application in debug mode
Swagger UI is accessible at: Swagger API Documentation
Swagger is served using swaggo/http-swagger
integrated into the Gorilla Mux setup.
Prometheus metrics are exposed at: Prometheus Metrics Endpoint
This endpoint is used by Prometheus to collect metrics about the application's performance and health, leveraging the Prometheus Go client.
Deploy using Kubernetes and Kustomize:
-
Create Namespace:
kubectl create namespace ozgen kubectl create namespace logging kubectl create namespace monitoring
-
Deploy Resources:
kustomize build k8s | kubectl apply -f -
-
Initialize Database:
kustomize build k8s/ozgen/postgres | kubectl apply -f - -n ozgen kubectl port-forward svc/my-postgres 5432:5432 -n ozgen make migration up
Check pod status and logs using:
kubectl get pods -n ozgen
kubectl logs <pod-name> -n ozgen