|
|
||
|---|---|---|
| .. | ||
| docs | ||
| migrations | ||
| src | ||
| test | ||
| .env.example | ||
| jest.config.js | ||
| nest-cli.json | ||
| package-lock.json | ||
| package.json | ||
| README.md | ||
| tsconfig.build.json | ||
| tsconfig.json | ||
Keys for All API
Backend API server for the Keys for All licensing system.
Features
- 🔑 Secure key generation and validation
- 🔒 API key authentication
- ⚡ Redis-based rate limiting
- 📊 Analytics and monitoring
- 🎁 Community pool management
- 🔄 Key sharing functionality
Quick Start
Prerequisites
- Node.js 18+
- PostgreSQL 14+
- Redis 7+
Installation
# Install dependencies
npm install
# Set up environment variables
cp .env.example .env
# Edit .env with your configuration
# Run database migrations
npm run migrate
# Start development server
npm run dev
Environment Variables
Create a .env file with:
# Server
NODE_ENV=development
PORT=3000
# Database
DATABASE_URL=postgresql://username:password@localhost:5432/keysforall
# Redis
REDIS_URL=redis://localhost:6379
# Security
JWT_SECRET=your-secret-key
API_KEY_SALT=your-salt
# Rate Limiting
RATE_LIMIT_WINDOW=60000
RATE_LIMIT_VALIDATE=100
RATE_LIMIT_GENERATE=10
# Features
FEATURE_COMMUNITY_POOL=true
FEATURE_KEY_SHARING=true
API Endpoints
Key Management
POST /v1/keys/generate- Generate new keysPOST /v1/keys/validate- Validate a keyPOST /v1/keys/activate- Activate a keyGET /v1/keys/:key/status- Get key status
Key Sharing
POST /v1/keys/share- Create share linkPOST /v1/keys/claim- Claim shared key
Community Pool
POST /v1/community/donate- Donate keysPOST /v1/community/request- Request keyGET /v1/community/status- Pool statistics
Purchase Verification
POST /v1/purchase/verify- Verify StoreKit receipt
Analytics
GET /v1/analytics/app/:id- App statistics
Authentication
All requests require an API key header:
X-API-Key: your-api-key
Development
# Run tests
npm test
# Run tests in watch mode
npm run test:watch
# Lint code
npm run lint
# Start development server with auto-reload
npm run dev
Production Deployment
# Build for production
npm run build
# Start production server
npm start
Docker
# Build image
docker build -t keysforall-api .
# Run container
docker run -p 3000:3000 --env-file .env keysforall-api
Project Structure
src/
├── config/ # Configuration management
├── controllers/ # Request handlers
├── middleware/ # Express middleware
├── models/ # Database models
├── routes/ # API routes
├── services/ # Business logic
├── utils/ # Helper functions
└── server.js # Entry point
Security
- API key authentication
- Rate limiting per endpoint
- Input validation with Joi
- SQL injection prevention
- XSS protection with Helmet
Monitoring
The API exposes metrics at /metrics for Prometheus scraping.
Key metrics:
- Request rate and latency
- Key validation success/failure
- Database query performance
- Redis cache hit rate
Contributing
- Fork the repository
- Create your feature branch
- Write tests for new functionality
- Ensure all tests pass
- Submit a pull request
License
MIT