# ๐Ÿš€ API Service - Clean Architecture > **Layanan API modern dengan arsitektur bersih untuk manajemen produk, pesanan, dan otentikasi pengguna** ## ๐Ÿ“‘ Daftar Isi - [โœจ Fitur Utama](#-fitur-utama) - [๐Ÿ—๏ธ Arsitektur](#%EF%B8%8F-arsitektur) - [โšก Quick Start](#-quick-start) - [๐Ÿ” Autentikasi](#-autentikasi) - [๐Ÿ“Š API Endpoints](#-api-endpoints) - [๐Ÿ› ๏ธ Development](#%EF%B8%8F-development) - [๐Ÿš€ Deployment](#-deployment) - [๐Ÿ“š Dokumentasi](#-dokumentasi) *** ## โœจ Fitur Utama ### Core Features - **๐Ÿ”’ JWT Authentication** - Sistem autentikasi yang aman - **๐Ÿ“ฆ CRUD Operations** - Operasi lengkap untuk produk dan pesanan - **๐Ÿฅ BPJS Integration** - Integrasi dengan layanan kesehatan BPJS - **๐Ÿฉบ FHIR/SATUSEHAT** - Dukungan standar kesehatan internasional - **๐Ÿ“– API Documentation** - Swagger/OpenAPI yang interaktif ### Developer Experience - **๐Ÿ”ฅ Hot Reload** - Development dengan auto-restart - **๐Ÿณ Docker Ready** - Deployment yang mudah - **โšก Code Generator** - Buat handler dan model otomatis - **๐Ÿงช Testing Suite** - Unit dan integration tests - **๐Ÿ“Š Health Monitoring** - Monitoring kesehatan aplikasi *** ## ๐Ÿ—๏ธ Arsitektur ### Clean Architecture Layers ``` โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Presentation Layer โ”‚ โ† handlers/, routes/ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ Application Layer โ”‚ โ† middleware/, validators/ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ Domain Layer โ”‚ โ† models/, interfaces/ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ Infrastructure Layer โ”‚ โ† database/, external APIs โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` ### Struktur Project ``` api-service/ โ”œโ”€โ”€ ๐Ÿ“ cmd/ โ”‚ โ””โ”€โ”€ api/main.go # ๐Ÿšช Entry point aplikasi โ”œโ”€โ”€ ๐Ÿ“ internal/ # ๐Ÿ  Core business logic โ”‚ โ”œโ”€โ”€ handlers/ # ๐ŸŽฎ HTTP controllers (Presentation) โ”‚ โ”œโ”€โ”€ middleware/ # ๐Ÿ›ก๏ธ Auth & validation (Application) โ”‚ โ”œโ”€โ”€ models/ # ๐Ÿ“Š Data structures (Domain) โ”‚ โ”œโ”€โ”€ routes/ # ๐Ÿ›ฃ๏ธ API routing (Presentation) โ”‚ โ”œโ”€โ”€ services/ # ๐Ÿ’ผ Business logic (Application) โ”‚ โ””โ”€โ”€ repository/ # ๐Ÿ’พ Data access (Infrastructure) โ”œโ”€โ”€ ๐Ÿ“ pkg/ # ๐Ÿ’ผ Package โ”‚ โ”œโ”€โ”€ logger/ # ๐ŸŽฏ General generators โ”‚ โ””โ”€โ”€ data/ # Hasil Log yang tersimpan โ”‚ โ”œโ”€โ”€ utils/ # ๐Ÿฅ BPJS specific tools โ”‚ โ””โ”€โ”€ validator/ # ๐Ÿฉบ SATUSEHAT tools โ”œโ”€โ”€ ๐Ÿ“ tools/ # ๐Ÿ”ง Development tools โ”‚ โ”œโ”€โ”€ general/ # ๐ŸŽฏ General generators โ”‚ โ”œโ”€โ”€ bpjs/ # ๐Ÿฅ BPJS specific tools โ”‚ โ””โ”€โ”€ satusehat/ # ๐Ÿฉบ SATUSEHAT tools โ”œโ”€โ”€ ๐Ÿ“ docs/ # ๐Ÿ“š Documentation โ”œโ”€โ”€ ๐Ÿ“ configs/ # โš™๏ธ Configuration files โ””โ”€โ”€ ๐Ÿ“ scripts/ # ๐Ÿ“œ Automation scripts ``` *** ## โšก Quick Start ### 1๏ธโƒฃ Setup Environment (2 menit) ```bash # Clone repository git clone cd api-service # Setup environment cp .env.example .env ``` ### 2๏ธโƒฃ Pilih Method Setup **๐Ÿณ Docker (Recommended)** ```bash make docker-run ``` **๐Ÿ”ง Manual Setup** ```bash # Install dependencies go mod download # Run database migrations make migrate-up # Start server go run cmd/api/main.go ``` ### Update Swagger ```bash swag init -g cmd/api/main.go --parseDependency --parseInternal # Alternative swag init -g cmd/api/main.go -o docs/ ### 3๏ธโƒฃ Verify Installation | Service | URL | Status | | :-- | :-- | :-- | | **API** | http://localhost:8080/api/v1 | โœ… | | **Swagger** | http://localhost:8080/swagger/index.html | ๐Ÿ“– | | **Health Check** | http://localhost:8080/api/v1/health | ๐Ÿ’š | *** ## ๐Ÿ” Autentikasi ### Login \& Mendapatkan Token ```bash curl -X POST http://localhost:8080/api/v1/auth/login \ -H "Content-Type: application/json" \ -d '{ "username": "admin", "password": "password" }' ``` **Response:** ```json { "access_token": "eyJhbGciOiJIUzI1NiIs...", "expires_in": 3600, "user": { "id": "123", "username": "admin", "role": "admin" } } ``` ### Menggunakan Token ```bash curl -X GET http://localhost:8080/api/v1/products \ -H "Authorization: Bearer " ``` ### Demo Accounts | Username | Password | Role | Akses | | :-- | :-- | :-- | :-- | | `admin` | `password` | Admin | Semua endpoint | | `user` | `password` | User | Read-only | *** ## ๐Ÿ“Š API Endpoints ### ๐ŸŒ Public Endpoints | Method | Endpoint | Deskripsi | | :-- | :-- | :-- | | `POST` | `/api/v1/auth/login` | Login pengguna | | `POST` | `/api/v1/auth/register` | Registrasi pengguna baru | | `GET` | `/api/v1/health` | Status kesehatan API | ### ๐Ÿ”’ Protected Endpoints #### User Management | Method | Endpoint | Deskripsi | | :-- | :-- | :-- | | `GET` | `/api/v1/auth/me` | Profile pengguna | | `PUT` | `/api/v1/auth/me` | Update profile | #### Product Management | Method | Endpoint | Deskripsi | | :-- | :-- | :-- | | `GET` | `/api/v1/products` | List semua produk | | `POST` | `/api/v1/products` | Buat produk baru | | `GET` | `/api/v1/products/:id` | Detail produk | | `PUT` | `/api/v1/products/:id` | Update produk | | `DELETE` | `/api/v1/products/:id` | Hapus produk | #### Dynamic Query (Advanced) | Method | Endpoint | Deskripsi | | :-- | :-- | :-- | | `GET` | `/api/v1/retribusi/dynamic` | Query dengan filter dinamis | **Contoh Query:** ```bash # Filter berdasarkan jenis GET /api/v1/retribusi/dynamic?filter[Jenis][_eq]=RETRIBUSI PELAYANAN KESEHATAN # Kombinasi filter GET /api/v1/retribusi/dynamic?filter[status][_eq]=active&filter[Tarif][_gt]=100000 # Pagination dan sorting GET /api/v1/retribusi/dynamic?sort=-date_created&limit=10&offset=20 ``` ### ๐Ÿฅ BPJS Integration | Method | Endpoint | Deskripsi | | :-- | :-- | :-- | | `GET` | `/api/v1/bpjs/peserta/:no` | Data peserta BPJS | | `GET` | `/api/v1/bpjs/rujukan/:no` | Data rujukan | ### ๐Ÿฉบ SATUSEHAT Integration | Method | Endpoint | Deskripsi | | :-- | :-- | :-- | | `GET` | `/api/v1/satusehat/patient/:id` | Data pasien | | `POST` | `/api/v1/satusehat/encounter` | Buat encounter baru | *** ## ๐Ÿ› ๏ธ Development ### Code Generation (30 detik) **๐ŸŽฏ Generate CRUD Lengkap** ```bash # Generate handler untuk entity baru go run tools/general/generate-handler.go product get post put delete # Generate dengan fitur advanced go run tools/general/generate-handler.go orders get post put delete dynamic search stats ``` **๐Ÿฅ Generate BPJS Handler** ```bash # Single service go run tools/bpjs/generate-bpjs-handler.go tools/bpjs/reference/peserta get # Semua service dari config go run tools/bpjs/generate-handler.go tools/bpjs/services-config-bpjs.yaml ``` **๐Ÿฉบ Generate SATUSEHAT Handler** ```bash go run tools/satusehat/generate-satusehat-handler.go tools/satusehat/services-config-satusehat.yaml patient ``` ### Development Commands ```bash # ๐Ÿ”ฅ Development dengan hot reload make watch # ๐Ÿงช Testing make test # Unit tests make itest # Integration tests make test-all # Semua tests # ๐Ÿ“– Update dokumentasi make docs # Generate Swagger docs # ๐Ÿ” Code quality make lint # Linting make format # Format code ``` ### Environment Configuration **๐Ÿ“ .env File:** ```bash # Database BLUEPRINT_DB_HOST=localhost BLUEPRINT_DB_PORT=5432 BLUEPRINT_DB_USERNAME=postgres BLUEPRINT_DB_PASSWORD=postgres BLUEPRINT_DB_DATABASE=api_service # JWT JWT_SECRET=your-super-secret-key-change-in-production # External APIs BPJS_BASE_URL=https://api.bpjs-kesehatan.go.id SATUSEHAT_BASE_URL=https://api.satusehat.kemkes.go.id # Application APP_ENV=development APP_PORT=8080 LOG_LEVEL=debug ``` *** ## ๐Ÿš€ Deployment ### ๐Ÿณ Docker Deployment **Development:** ```bash # Start semua services make docker-run # Stop services make docker-down # Rebuild dan restart make docker-rebuild ``` **Production:** ```bash # Build production image docker build -t api-service:prod . # Run production container docker run -d \ --name api-service \ -p 8080:8080 \ --env-file .env.prod \ api-service:prod ``` ### ๐Ÿ”ง Manual Deployment ```bash # Build aplikasi make build # Run migrations ./scripts/migrate.sh up # Start server ./bin/api-service ``` *** ## ๐Ÿ“š Dokumentasi ### ๐Ÿ“– Interactive API Documentation Kunjungi **Swagger UI** di: http://localhost:8080/swagger/index.html **Cara menggunakan:** 1. ๐Ÿ”‘ Login melalui `/auth/login` endpoint 2. ๐Ÿ“‹ Copy token dari response 3. ๐Ÿ”“ Klik tombol "Authorize" di Swagger 4. ๐Ÿ“ Masukkan: `Bearer ` 5. โœ… Test semua endpoint yang tersedia ### ๐Ÿงช Testing Examples **JavaScript/Axios:** ```javascript // Login dan set token const auth = await axios.post('/api/v1/auth/login', { username: 'admin', password: 'password' }); axios.defaults.headers.common['Authorization'] = `Bearer ${auth.data.access_token}`; // Fetch data const products = await axios.get('/api/v1/products'); console.log(products.data); ``` **cURL Examples:** ```bash # Login TOKEN=$(curl -s -X POST http://localhost:8080/api/v1/auth/login \ -H "Content-Type: application/json" \ -d '{"username":"admin","password":"password"}' | jq -r '.access_token') # Use token curl -H "Authorization: Bearer $TOKEN" \ http://localhost:8080/api/v1/products ``` ### ๐Ÿ” Health Monitoring ```bash # Basic health check curl http://localhost:8080/api/v1/health # Detailed system info curl http://localhost:8080/api/v1/health/detailed ``` **Response:** ```json { "status": "healthy", "timestamp": "2025-09-10T05:39:00Z", "services": { "database": "connected", "bpjs_api": "accessible", "satusehat_api": "accessible" }, "version": "1.0.0" } ``` *** ## ๐Ÿšจ Troubleshooting ### Masalah Umum **โŒ Database Connection Error** ```bash # Cek status PostgreSQL make db-status # Reset database make db-reset # Check logs make logs-db ``` **โŒ Generate Handler Gagal** - โœ… Pastikan berada di root project - โœ… Cek permission write di folder `internal/` - โœ… Verifikasi file `internal/routes/v1/routes.go` exists **โŒ Token Invalid/Expired** - ๐Ÿ”„ Login ulang untuk mendapatkan token baru - โฐ Token expire dalam 1 jam (configurable) - ๐Ÿ“ Format harus: `Bearer ` **โŒ Import Error saat Generate** - ๐Ÿงน Jalankan: `go mod tidy` - ๐Ÿ”„ Restart development server - ๐Ÿ“ Cek format import di generated files ### Debug Mode ```bash # Enable debug logging export LOG_LEVEL=debug # Run dengan verbose output make run-debug # Monitor performance make monitor ``` *** ## ๐ŸŽฏ Next Steps ### ๐Ÿ“‹ Development Roadmap - [ ] โœ… **Setup environment selesai** - [ ] โœ… **Generate handler pertama** - [ ] โœ… **Test dengan Swagger** - [ ] ๐Ÿ”„ **Implementasi business logic** - [ ] ๐Ÿ”„ **Tambahkan unit tests** - [ ] ๐Ÿ”„ **Setup CI/CD pipeline** - [ ] ๐Ÿ”„ **Deploy ke production** ### ๐Ÿš€ Advanced Features - **๐Ÿ“Š Monitoring \& Observability** - **๐Ÿ”’ Enhanced Security (Rate limiting, CORS)** - **๐Ÿ“ˆ Performance Optimization** - **๐ŸŒ Multi-language Support** - **๐Ÿ“ฑ Mobile SDK Integration** *** **โšก Total setup time: 5 menit | ๐Ÿ”ง Generate CRUD: 30 detik | ๐Ÿงช Testing: Langsung via Swagger** > **๐Ÿ’ก Pro Tip:** Gunakan `make help` untuk melihat semua command yang tersedia ***