/go-sample-rest-api

This is a Go-based RESTful API with Azure Blob Storage integration, utilizing Gorilla Mux for routing and providing comprehensive tools for secure and scalable web application development.

Primary LanguageGo

go-sample-rest-api

Project Overview

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.

Prerequisites

Dependencies

Here are the primary libraries and tools used:

Installation

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

Database Migration Tool

Install golang-migrate on macOS using Homebrew:

brew install golang-migrate

Usage with Makefile

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

Debugging

Set up Delve for sophisticated debugging:

go install github.com/go-delve/delve/cmd/dlv@latest
make debug        # Start the application in debug mode

API and Metrics Documentation

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.

Deployment Instructions

Deploy using Kubernetes and Kustomize:

  1. Create Namespace:

    kubectl create namespace ozgen
    kubectl create namespace logging
    kubectl create namespace monitoring
  2. Deploy Resources:

    kustomize build k8s | kubectl apply -f -
  3. 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