A TypeScript/Node.js backend for a sophisticated AI image generation platform that integrates Stable Diffusion models with comprehensive user management, payment processing, Discord bot integration, and community features.
Bashable AI is a production-ready backend service that powers an AI image generation platform similar to Midjourney or DALL-E. It provides a complete ecosystem for users to generate, share, and monetize AI-generated images using various Stable Diffusion models. The platform includes user authentication, credit-based billing, community features, Discord integration, and comprehensive admin tools.
- AI Image Generation: Integration with multiple Stable Diffusion models (SFW/NSFW)
- Real-time Processing: WebSocket-based real-time image generation status updates
- Multi-model Support: Support for 9+ different Stable Diffusion checkpoints
- Image Upscaling: Advanced image enhancement capabilities
- Prompt Validation: AI-powered content filtering and moderation
- Multi-provider OAuth: Google, Discord, Microsoft, Apple, and local authentication
- Role-based Access Control: User, Moderator, and Admin roles with granular permissions
- Email & Mobile Verification: Comprehensive user verification system
- Profile Management: Avatar uploads, user profiles, and social features
- Credit System: Flexible credit-based billing with auto-recharge
- Multiple Payment Gateways: Stripe and Coinbase Commerce integration
- Subscription Management: Automated payment processing and webhook handling
- Tipping System: User-to-user tipping functionality
- Image Sharing: Public/private image sharing with social features
- Community Models: User-submitted model submission and approval system
- Discord Integration: Full Discord bot with moderation, polls, and sharing
- User Discovery: Explore and follow other creators
- Showcase System: Curated featured content
- API Access: RESTful API with API key management
- Content Moderation: Automated illegal content detection and filtering
- Analytics & Statistics: Comprehensive usage tracking and analytics
- Admin Dashboard: Full administrative interface for platform management
- Gradio Integration: Support for custom model deployment via Gradio
- Runtime: Node.js with TypeScript
- Framework: Express.js with custom middleware architecture
- Database: MongoDB with Mongoose ODM and Typegoose decorators
- Authentication: Passport.js with JWT tokens
- Validation: Zod schema validation
- Logging: Pino structured logging
- Stable Diffusion: Integration with multiple SD models
- Gradio: Custom model deployment framework
- Content Filtering: AI-powered prompt validation and moderation
- AWS S3: File storage and image hosting
- Payment Processing: Stripe and Coinbase Commerce APIs
- Email Services: Nodemailer with SMTP integration
- SMS Services: Twilio integration for mobile verification
- Discord.js: Full Discord bot with slash commands
- WebSockets: Server-Sent Events (SSE) for real-time updates
- File Upload: Multer with image processing via Sharp
- TypeScript: Full type safety with strict configuration
- ESLint & Prettier: Code quality and formatting
- Nodemon: Development hot-reloading
- Module Aliases: Clean import paths with path mapping
- Rate Limiting: IP-based request throttling
- Content Security: Multi-layer content filtering
- Input Validation: Comprehensive request validation
- Error Handling: Centralized error management
- Node.js 18+
- MongoDB 6+
- Python 3.8+ (for Gradio integration)
- Git
-
Clone the repository
git clone https://github.com/MH-PAVEL/bashable-backend.git cd bashable-backend -
Install dependencies
npm install # or yarn install -
Environment Configuration Create a
.envfile with the following required variables:# Server Configuration SERVER_URL=http://localhost:1337 CLIENT_URL=http://localhost:3000 # Database MONGO_URI=mongodb://localhost:27017/bashable # Authentication JWT_SECRET=your-jwt-secret API_SECRET=your-api-secret # OAuth Providers GOOGLE_CLIENT_ID=your-google-client-id GOOGLE_CLIENT_SECRET=your-google-client-secret DISCORD_CLIENT_ID=your-discord-client-id DISCORD_CLIENT_SECRET=your-discord-client-secret MICROSOFT_CLIENT_ID=your-microsoft-client-id MICROSOFT_CLIENT_SECRET=your-microsoft-client-secret APPLE_CLIENT_ID=your-apple-client-id APPLE_TEAM_ID=your-apple-team-id APPLE_KEY_ID=your-apple-key-id APPLE_KEY_FILE=path/to/apple/key.p8 # Discord Bot DISCORD_BOT_TOKEN=your-discord-bot-token DISCORD_GUILD_ID=your-discord-guild-id DISCORD_SFW_CHANNEL_ID=your-sfw-channel-id DISCORD_NSFW_CHANNEL_ID=your-nsfw-channel-id DISCORD_POLL_CHANNEL_ID=your-poll-channel-id DISCORD_RUN_BOT=true # AI Generation GENERATE_PROVIDER=your-ai-provider GENERATE_AUTH_KEY=your-ai-auth-key GENERATE_COST=0.2 GENERATE_SERVER_URL=your-ai-server-url OPENAI_API_KEY=your-openai-api-key # Payment Processing STRIPE_SECRET_KEY=your-stripe-secret-key STRIPE_WEBHOOK_SECRET=your-stripe-webhook-secret COINBASE_API_KEY=your-coinbase-api-key COINBASE_WEBHOOK_SECRET=your-coinbase-webhook-secret # File Storage AWS_S3_ACCESS_KEY_ID=your-s3-access-key AWS_S3_SECRET_ACCESS_KEY=your-s3-secret-key AWS_S3_BUCKET_NAME=your-s3-bucket AWS_S3_REGION=your-s3-region IMAGES_DIR=./static/images # Email & SMS VERIFY_EMAIL=true SMTP_HOST=your-smtp-host SMTP_PORT=587 SMTP_USER=your-smtp-user SMTP_PASS=your-smtp-password VERIFY_MOBILE=false TWILIO_ACCOUNT_SID=your-twilio-sid TWILIO_AUTH_TOKEN=your-twilio-token TWILIO_PHONE_NUMBER=your-twilio-phone # Admin ADMIN_PASSWORD=your-admin-password
-
Database Setup
# Start MongoDB (if using local instance) mongod # Run database migrations npm run migrate
-
Development Server
# Start development server with hot reload npm run dev # Or build and run production npm run build npm start
The server will start on http://localhost:1337
The platform provides a comprehensive REST API with the following main endpoints:
# Generate an image
POST /api/generate
{
"prompt": "A beautiful landscape with mountains",
"negative_prompt": "blurry, low quality",
"width": 768,
"height": 768,
"steps": 20,
"model": "dreamshaper_331BakedVae.safetensors"
}
# Get available models
GET /api/generate/checkpoints
# Estimate generation cost
POST /api/generate/cost# User registration
POST /api/users/signup
{
"username": "artist123",
"email": "artist@example.com",
"password": "securepassword"
}
# User authentication
POST /api/users/signin
{
"email": "artist@example.com",
"password": "securepassword"
}# Create API key
POST /api/users/api-keys
Authorization: Bearer <jwt-token>
# Use API key for generation
POST /api/endpoints/generate-image
X-API-Key: <your-api-key>
{
"prompt": "Your prompt here"
}/generate <prompt>- Generate an image directly in Discord/upscale <image>- Upscale an uploaded image/status- Check your account status and credits/link- Link your Discord account to your Bashable account
src/
โโโ app/ # Express application setup
โ โโโ index.ts # Main app configuration
โ โโโ routes.ts # Route definitions
โ โโโ middlewares.ts # Middleware setup
โ โโโ error.ts # Error handling
โโโ controllers/ # Business logic controllers
โ โโโ generate.ts # Image generation logic
โ โโโ users.ts # User management
โ โโโ admin.ts # Admin functionality
โ โโโ ...
โโโ models/ # MongoDB schemas (Typegoose)
โ โโโ user.ts # User model
โ โโโ generation.ts # Image generation model
โ โโโ ...
โโโ middlewares/ # Custom middleware
โ โโโ auth.ts # Authentication middleware
โ โโโ role.ts # Role-based access control
โ โโโ ...
โโโ bot/ # Discord bot implementation
โ โโโ commands/ # Slash commands
โ โโโ index.ts # Bot setup
โโโ core/ # Core utilities
โ โโโ database.ts # Database connection
โ โโโ env.ts # Environment validation
โ โโโ logger.ts # Logging setup
โโโ utils/ # Utility functions
- Repository Pattern: Clean separation between data access and business logic
- Middleware Chain: Modular middleware for authentication, validation, and error handling
- Event-Driven Architecture: Event system for decoupled communication
- Type-Safe APIs: Full TypeScript implementation with Zod validation
- Modular Routing: Hierarchical route structure with automatic registration
- Request Processing: Middleware chain handles auth, validation, and rate limiting
- Business Logic: Controllers process requests and interact with models
- AI Integration: External AI services handle image generation
- Response: Structured responses with proper error handling
- Real-time Updates: SSE connections provide live status updates
Problem: Users needed live updates on generation progress without polling. Solution: Implemented Server-Sent Events (SSE) for real-time status updates, reducing server load and improving user experience.
Problem: Supporting 5 different OAuth providers with consistent user experience. Solution: Created a unified authentication system using Passport.js with custom strategies, ensuring seamless user onboarding across all providers.
Problem: Preventing inappropriate content while maintaining generation speed. Solution: Implemented multi-layer content filtering with AI-powered prompt validation, user reputation systems, and automated moderation workflows.
Problem: Managing multiple payment gateways with different webhook formats and error handling. Solution: Built a unified payment abstraction layer with webhook validation, automatic retry logic, and comprehensive error handling for Stripe and Coinbase.
Problem: Synchronizing Discord interactions with the main platform while maintaining data consistency. Solution: Created a modular Discord bot with slash commands, real-time moderation tools, and seamless account linking functionality.
- Microservices Architecture: Break down monolithic structure into microservices
- Redis Caching: Implement Redis for session management and caching
- GraphQL API: Add GraphQL endpoint for more flexible data querying
- Docker Containerization: Full containerization with Docker Compose
- Kubernetes Deployment: Production-ready K8s manifests
- Real-time Chat: WebSocket-based chat system for user collaboration
- Advanced Analytics: Machine learning-powered usage analytics
- Mobile API: Optimized endpoints for mobile applications
- Webhook System: Extensible webhook system for third-party integrations
- A/B Testing Framework: Built-in experimentation platform
- Database Sharding: Horizontal scaling for MongoDB
- CDN Integration: Global content delivery network
- Load Balancing: Multi-instance deployment with load balancing
- Queue System: Redis-based job queue for background processing
- Monitoring & Alerting: Comprehensive observability stack
โญ Star this repository if you found it helpful! Your support helps us continue building amazing AI tools for creators worldwide.
Ready to build the future of AI-powered creativity? ๐