# ๐Ÿ“Š API Service Project Layout ## ๐ŸŽฏ Project Overview Dashboard ### ๐Ÿ“ˆ Key Metrics - **Language**: Go 1.24.4 - **Framework**: Gin 1.10.1 - **Database**: PostgreSQL with GORM - **Authentication**: JWT + Keycloak - **Documentation**: Swagger/OpenAPI 3.0 - **Container**: Docker & Docker Compose ### ๐Ÿ—๏ธ Architecture Overview ``` api-service/ โ”œโ”€โ”€ cmd/ โ”‚ โ””โ”€โ”€ api/ โ”‚ โ””โ”€โ”€ main.go โ”œโ”€โ”€ internal/ โ”‚ โ”œโ”€โ”€ config/ # Config loader โ”‚ โ”œโ”€โ”€ server/ # HTTP server setup + routes binding โ”‚ โ”œโ”€โ”€ handler/ # HTTP handlers (controller layer) โ”‚ โ”œโ”€โ”€ service/ # Business logic โ”‚ โ”œโ”€โ”€ repository/ # DB access layer โ”‚ โ”œโ”€โ”€ model/ # App/domain models โ”‚ โ””โ”€โ”€ middleware/ # HTTP middlewares โ”œโ”€โ”€ pkg/ # Reusable packages independent of business logic โ”‚ โ”œโ”€โ”€ logger/ โ”‚ โ”œโ”€โ”€ utils/ โ”‚ โ””โ”€โ”€ validator/ โ”œโ”€โ”€ scripts/ # Deployment & build automation โ””โ”€โ”€ docs/ # Documentation (incl. swagger) ``` ## ๐Ÿš€ Development Environment Layout ### ๐Ÿ“‹ Prerequisites Setup ```bash # Required tools - Go 1.24.4+ - PostgreSQL 12+ - Docker & Docker Compose - Keycloak Server (optional) # Development tools - Air (hot reload) - Swag (Swagger docs) - Make (build automation) ``` ### ๐Ÿ”ง Configuration Files Layout ``` project-root/ โ”œโ”€โ”€ .env # Environment variables โ”œโ”€โ”€ .air.toml # Air configuration โ”œโ”€โ”€ Makefile # Build commands โ”œโ”€โ”€ docker-compose.yml # Docker services โ”œโ”€โ”€ Dockerfile # Container image โ””โ”€โ”€ .goreleaser.yml # Release configuration ``` ## ๐Ÿ“š API Documentation Layout ### ๐ŸŒ Base URL Structure ``` http://localhost:8080/api/v1 ``` ### ๐Ÿ“– Endpoint Categories #### ๐Ÿ” Health & Monitoring - `GET /api/v1/health` - Health check - `GET /api/v1/` - Hello world - `GET /api/v1/metrics` - Application metrics #### ๐Ÿ” Authentication Endpoints - `POST /api/v1/auth/login` - User login - `POST /api/v1/auth/register` - User registration - `POST /api/v1/auth/refresh` - Token refresh - `POST /api/v1/auth/logout` - User logout #### ๐Ÿ‘ค User Management - `GET /api/v1/users/profile` - Get user profile - `PUT /api/v1/users/profile` - Update user profile - `GET /api/v1/users` - List users (admin) - `GET /api/v1/users/:id` - Get user by ID #### ๐Ÿ“ฆ Product Management - `GET /api/v1/products` - List products - `POST /api/v1/products` - Create product - `GET /api/v1/products/:id` - Get product - `PUT /api/v1/products/:id` - Update product - `DELETE /api/v1/products/:id` - Delete product #### ๐Ÿ”„ Real-time Features - `GET /api/v1/websocket` - WebSocket connection - `GET /api/v1/webservice` - Web service endpoint ### ๐Ÿ“Š Swagger Documentation - **Interactive UI**: http://localhost:8080/swagger/index.html - **OpenAPI JSON**: http://localhost:8080/swagger/doc.json ## ๐Ÿณ Docker Deployment Layout ### ๐Ÿƒ Development Environment ```bash # Start all services make docker-run # Services included: - PostgreSQL database - API service with hot reload - pgAdmin (database management) ``` ### ๐Ÿš€ Production Environment ```bash # Build production image docker build -t api-service:prod . # Run production container docker run -d \ -p 8080:8080 \ --env-file .env \ --name api-service \ api-service:prod ``` ### ๐Ÿ“ฆ Docker Compose Services ```yaml services: postgres: image: postgres:15-alpine environment: POSTGRES_DB: api_service POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres ports: - "5432:5432" volumes: - postgres_data:/var/lib/postgresql/data api: build: . ports: - "8080:8080" depends_on: - postgres environment: - BLUEPRINT_DB_HOST=postgres volumes: - .:/app command: air -c .air.toml ``` ## ๐Ÿ› ๏ธ Development Workflow Layout ### ๐Ÿ”„ Daily Development Cycle ```bash # 1. Start development environment make watch # 2. Run tests make test # 3. Check code quality make lint # 4. Build for production make build ``` ### ๐Ÿงช Testing Strategy ``` tests/ โ”œโ”€โ”€ unit/ # Unit tests โ”œโ”€โ”€ integration/ # Integration tests โ”œโ”€โ”€ e2e/ # End-to-end tests โ””โ”€โ”€ fixtures/ # Test data ``` ### ๐Ÿ“Š Monitoring & Logging - **Health Check**: `/api/v1/health` - **Metrics**: Prometheus metrics - **Logging**: Structured JSON logs - **Tracing**: Distributed tracing support ## ๐Ÿ“ File Structure Detailed Layout ### ๐Ÿ“„ Configuration Files ``` .env.example # Environment template config/ โ”œโ”€โ”€ config.go # Configuration struct โ”œโ”€โ”€ config_test.go # Configuration tests โ””โ”€โ”€ validation.go # Config validation ``` ### ๐Ÿ—„๏ธ Database Layout ``` database/ โ”œโ”€โ”€ migrations/ # Database migrations โ”œโ”€โ”€ seeds/ # Seed data โ”œโ”€โ”€ models/ # GORM models โ””โ”€โ”€ repositories/ # Data access layer ``` ### ๐ŸŽฏ Handler Layout ``` handlers/ โ”œโ”€โ”€ auth.go # Authentication handlers โ”œโ”€โ”€ user.go # User management handlers โ”œโ”€โ”€ product.go # Product handlers โ”œโ”€โ”€ health.go # Health check handlers โ””โ”€โ”€ middleware/ # HTTP middleware โ”œโ”€โ”€ auth.go # JWT middleware โ”œโ”€โ”€ cors.go # CORS middleware โ””โ”€โ”€ logger.go # Logging middleware ``` ### ๐ŸŒ Route Layout ``` routes/ โ”œโ”€โ”€ v1/ โ”‚ โ”œโ”€โ”€ routes.go # Route definitions โ”‚ โ”œโ”€โ”€ auth_routes.go # Auth routes โ”‚ โ”œโ”€โ”€ user_routes.go # User routes โ”‚ โ””โ”€โ”€ product_routes.go # Product routes โ””โ”€โ”€ middleware.go # Route middleware ``` ## ๐Ÿš€ Quick Start Commands ### ๐Ÿƒ Development ```bash # Clone and setup git clone cd api-service cp .env.example .env make docker-run # Start development make watch ``` ### ๐Ÿงช Testing ```bash # Run all tests make all # Run specific test make test make itest ``` ### ๐Ÿš€ Production ```bash # Build and run make build ./main.exe ``` ## ๐Ÿ“Š Performance Monitoring Layout ### ๐Ÿ“ˆ Metrics Collection - **Request Duration**: Track API response times - **Error Rates**: Monitor error frequencies - **Database Performance**: Query execution times - **Memory Usage**: Application memory consumption ### ๐Ÿ” Health Checks - **Database Connectivity**: PostgreSQL connection status - **External Services**: Keycloak availability - **Resource Usage**: CPU and memory utilization ## ๐ŸŽฏ Next Steps 1. **Setup Environment**: Copy `.env.example` to `.env` 2. **Start Database**: Run `make docker-run` 3. **Start Development**: Run `make watch` 4. **Test API**: Visit `http://localhost:8080/swagger/index.html` 5. **Run Tests**: Execute `make all` ## ๐Ÿ“ž Support & Resources - **Documentation**: Check `/docs` folder - **API Testing**: Use Swagger UI at `/swagger/index.html` - **Database**: Access pgAdmin at `http://localhost:5050` - **Issues**: Create GitHub issues for bugs/features