System Overview
Zingat follows a modular architecture with clear separation of concerns:The modular split allows independent iteration of web, business logic, and data layers.
Database Schema
Clips Table
API Keys Table
The
clips table stores all paste content with metadata. The hits field tracks view counts and is updated asynchronously via batched operations for performance.Key Design Decisions
1. Batch Database Operations
Instead of logging each view directly into the database, Zingat defers these operations to separate threads. This design:- Reduces Database Contention: Multiple hits are batched into single database commits
- Improves Performance: Minimizes database write operations
- Scales Better: Handles high traffic more efficiently
2. Async Architecture
Zingat uses Tokio for async processing, enabling:- Non-blocking I/O: Database operations don’t block the web server
- High Concurrency: Handles thousands of simultaneous connections
- Efficient Resource Usage: Minimal overhead for I/O-bound operations
3. Password Security
Passwords are hashed using Argon2 before storage:4. Shortcode Generation
Clip shortcodes are generated as unique identifiers for URL sharing. The system ensures uniqueness through database constraints and collision handling. s5. Project Structure
Zingat follows a layered architecture with clear separation: Data Layer (src/lib/data/)
mod.rs: Database connection and configurationmodel.rs: Database models and entitiesquery.rs: Database queries and operations
src/lib/domain/)
clip/: Core business logic for clipsfield/: Newtype wrappers for type safety (ClipId, Content, Expires, Hits, Password, Posted, Shortcode, Title)mod.rs: Main Clip struct and ClipError enum
maintenance.rs: Background maintenance taskstime.rs: Time-related utilities
src/lib/service/)
action.rs: Business logic actionsask.rs: Request/response DTOs for API operations
src/lib/web/)
api.rs: REST API endpoints and authenticationctx.rs: Template context structuresform.rs: Form handling and validationhitcounter.rs: Asynchronous hit counting systemhttp.rs: Web UI routes and handlersrenderer.rs: Template rendering engine
The newtype pattern is used extensively for type safety, preventing accidental mixing of different string types (e.g., ClipId vs Shortcode).
Performance Optimizations
Database Connection Pooling
SQLx connection pooling ensures efficient database resource usage:Right-size
max_connections for your database; too high can degrade performance via context switching.Memory Management
Rust’s ownership model prevents common issues:- No memory leaks
- No buffer overflows
- Thread safety guaranteed at compile time
Hit Counter Batching
Hit counting uses an asynchronous batching system:- Hits are deferred to separate threads
- Multiple hits are batched into single database commits
- Significantly improves performance and reduces database contention
- Implemented via
hitcounter.rsmodule with crossbeam channels
Security Architecture
Input Validation
All inputs are rigorously validated:Error Handling
Zingat uses comprehensive error types: ClipError Variants:NotFound: Clip doesn’t existAlreadyExists: Shortcode collisionExpired: Clip has passed expiration dateInvalidPassword: Wrong password providedInvalidTitle: Title validation failedEmptyContent: Content cannot be emptyInvalidDate: Date parsing error
NotFound: Resource not found (404)Server: Internal server error (500)User: Client error (401)KeyError: API key issues (400)
Threat model at a glance
Threat model at a glance
- Abuse: brute force and high-frequency scraping
- Leakage: unintended access to protected pastes
- Integrity: tampering with stored content
Mitigations mapped
Mitigations mapped
- Rate limiting for abuse
- Hashed secrets for password protection
- Prepared statements for injection prevention
Rate Limiting
IP-based rate limiting prevents abuse:SQL Injection Prevention
SQLx uses prepared statements, preventing SQL injection:Deployment Architecture
Containerization
Docker support for easy deployment:Cloud Deployment
Support for multiple platforms:- Render: render.yaml configuration
- Railway: railway.toml configuration
- Docker: Dockerfile included
Monitoring and Logging
Structured Logging
JSON-formatted logs for easy parsing:Health Checks
Endpoint for monitoring application health:Future Enhancements
Planned architectural improvements:- Redis Integration: For better caching and session management
- CDN Support: For static asset delivery
- Microservices: Split into smaller, focused services
- GraphQL API: Alternative to REST API