b438fd8dd4e957869b224e62ca66c8a5d04afbb3
…
…
…
…
…
…
🚀 API Service Guide
📖 Introduction
This API service is designed to provide a robust backend for managing products, orders, and user authentication. It integrates with BPJS for health insurance services and offers a comprehensive set of features for developers.
🌟 Features
- User authentication with JWT
- CRUD operations for products and orders
- Integration with BPJS services
- Swagger documentation for easy API testing
- Docker support for easy deployment
📋 Quick Start (5 Menit)
1. Setup Environment
git clone <repository-url>
cd api-service
cp .env.example .env
2. Jalankan Database & API
# Opsi A: Docker (Rekomendasi)
make docker-run
# Opsi B: Manual
go mod download
go run cmd/api/main.go
3. Akses API
- API: http://localhost:8080/api/v1
- Swagger: http://localhost:8080/swagger/index.html
- Health Check: http://localhost:8080/api/v1/health
🏗️ Struktur Project
api-service/
├── cmd/api/main.go # Entry point
├── internal/ # Core business logic
│ ├── handlers/ # HTTP controllers
│ ├── models/ # Data structures
│ ├── middleware/ # Auth & error handling
│ └── routes/ # API routes
├── tools/ # CLI generators
└── docs/ # API documentation
🔐 Authentication
Login (Dapatkan Token)
curl -X POST http://localhost:8080/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"password"}'
Gunakan Token
curl -X GET http://localhost:8080/api/v1/products \
-H "Authorization: Bearer <your-token>"
Demo Users
| Username | Password | Role |
|---|---|---|
| admin | password | admin |
| user | password | user |
🛠️ Generate Handler Baru (30 Detik)
Cara Cepat
# Windows
tools/generate.bat product get post put delete
# Linux/Mac
./tools/generate.sh product get post put delete
# Atau langsung dengan Go
go run tools/general/generate-handler.go orders get post
go run tools/general/generate-handler.go orders/product get post
go run tools/general/generate-handler.go orders/order get post put delete dynamic search stats
go run tools/bpjs/generate-bpjs-handler.go reference/peserta get
# 1. Test generate satu service
go run tools/bpjs/generate-handler.go services-config-bpjs.yaml vclaim
# 2. Test generate semua service
go run tools/bpjs/generate-handler.go services-config-bpjs.yaml
Method Tersedia
get- Ambil datapost- Buat data baruput- Update datadelete- Hapus data
Hasil Generate
- Handler:
internal/handlers/product/product.go - Models:
internal/models/product/product.go - Routes: Otomatis ditambahkan ke
/api/v1/products
📊 API Endpoints
Public (Tanpa Auth)
POST /api/v1/auth/login- LoginPOST /api/v1/auth/register- RegisterGET /api/v1/health- Health check
Protected (Dengan Auth)
GET /api/v1/auth/me- Profile userGET /api/v1/products- List productsPOST /api/v1/products- Create productGET /api/v1/products/:id- Detail productPUT /api/v1/products/:id- Update productDELETE /api/v1/products/:id- Delete product
🧪 Development Workflow
Perintah Penting
make watch # Jalankan dengan hot reload
make test # Unit tests
make itest # Integration tests
make all # Semua tests
make build # Build production
Update Swagger
swag init -g cmd/api/main.go --parseDependency --parseInternal
🔧 Environment Variables
Database
BLUEPRINT_DB_HOST=localhost
BLUEPRINT_DB_PORT=5432
BLUEPRINT_DB_USERNAME=postgres
BLUEPRINT_DB_PASSWORD=postgres
BLUEPRINT_DB_DATABASE=api_service
JWT Secret
JWT_SECRET=your-secret-key-change-this-in-production
🐳 Docker Commands
Development
make docker-run # Jalankan semua services
docker-compose down # Stop services
Production
docker build -t api-service:prod .
docker run -d -p 8080:8080 --env-file .env api-service:prod
🎯 Tips Cepat
1. Generate CRUD Lengkap
go run tools/generate-handler.go user get post put delete
2. Test API dengan Swagger
- Buka http://localhost:8080/swagger/index.html
- Login untuk dapat token
- Klik "Authorize" dan masukkan token
- Test semua endpoint
3. Debug Database
- pgAdmin: http://localhost:5050
- Database: api_service
- User/Pass: postgres/postgres
🚨 Troubleshooting
Generate Handler Gagal
- Pastikan di root project
- Cek file
internal/routes/v1/routes.goada - Pastikan permission write
Database Connection Error
- Cek PostgreSQL running
- Verifikasi .env configuration
- Gunakan
make docker-rununtuk setup otomatis
Token Invalid
- Login ulang untuk dapat token baru
- Token expire dalam 1 jam
- Pastikan format:
Bearer <token>
📱 Contoh Integrasi Frontend
JavaScript/Axios
// Login
const login = await axios.post('/api/v1/auth/login', {
username: 'admin',
password: 'password'
});
// Set token
axios.defaults.headers.common['Authorization'] =
`Bearer ${login.data.access_token}`;
// Get data
const products = await axios.get('/api/v1/products');
🎉 Next Steps
- ✅ Setup environment selesai
- ✅ Generate handler pertama
- ✅ Test dengan Swagger
- 🔄 Tambahkan business logic
- 🔄 Deploy ke production
Total waktu setup: 5 menit | Generate CRUD: 30 detik | Testing: Langsung di Swagger
Description
Languages
Go
91.1%
HTML
8.6%
Makefile
0.1%