3.3 KiB
3.3 KiB
Formies Backend
A production-ready Rust backend for the Formies application.
Features
- RESTful API endpoints
- SQLite database with connection pooling
- JWT-based authentication
- Rate limiting
- Structured logging
- Error tracking with Sentry
- Health check endpoint
- CORS support
- Configuration management
- Metrics endpoint
Prerequisites
- Rust 1.70 or later
- SQLite 3
- Make (optional, for using Makefile commands)
Configuration
The application can be configured using environment variables or a configuration file. The following environment variables are supported:
Required Environment Variables
DATABASE_URL
: SQLite database URL (default: form_data.db)BIND_ADDRESS
: Server bind address (default: 127.0.0.1:8080)INITIAL_ADMIN_USERNAME
: Initial admin usernameINITIAL_ADMIN_PASSWORD
: Initial admin password
Optional Environment Variables
ALLOWED_ORIGIN
: CORS allowed originRUST_LOG
: Log level (default: info)SENTRY_DSN
: Sentry DSN for error trackingJWT_SECRET
: JWT secret keyJWT_EXPIRATION
: JWT expiration time in seconds
Development
- Clone the repository
- Install dependencies:
cargo build
- Set up environment variables:
cp .env.example .env # Edit .env with your configuration
- Run the development server:
cargo run
Production Deployment
Docker
-
Build the Docker image:
docker build -t formies-backend .
-
Run the container:
docker run -d \ --name formies-backend \ -p 8080:8080 \ -v $(pwd)/data:/app/data \ -e DATABASE_URL=/app/data/form_data.db \ -e BIND_ADDRESS=0.0.0.0:8080 \ -e INITIAL_ADMIN_USERNAME=admin \ -e INITIAL_ADMIN_PASSWORD=your-secure-password \ -e ALLOWED_ORIGIN=https://your-frontend-domain.com \ -e SENTRY_DSN=your-sentry-dsn \ formies-backend
Systemd Service
-
Create a systemd service file at
/etc/systemd/system/formies-backend.service
:[Unit] Description=Formies Backend Service After=network.target [Service] Type=simple User=formies WorkingDirectory=/opt/formies-backend ExecStart=/opt/formies-backend/formies-be Restart=always Environment=DATABASE_URL=/opt/formies-backend/data/form_data.db Environment=BIND_ADDRESS=0.0.0.0:8080 Environment=INITIAL_ADMIN_USERNAME=admin Environment=INITIAL_ADMIN_PASSWORD=your-secure-password Environment=ALLOWED_ORIGIN=https://your-frontend-domain.com Environment=SENTRY_DSN=your-sentry-dsn [Install] WantedBy=multi-user.target
-
Enable and start the service:
sudo systemctl enable formies-backend sudo systemctl start formies-backend
Monitoring
Health Check
The application exposes a health check endpoint at /api/health
:
curl http://localhost:8080/api/health
Metrics
Metrics are available at /metrics
when enabled in the configuration.
Logging
Logs are written to the configured log file and can be viewed using:
tail -f logs/app.log
Security
- All API endpoints are rate-limited
- CORS is configured to only allow specified origins
- JWT tokens are used for authentication
- Passwords are hashed using bcrypt
- SQLite database is protected with proper file permissions
License
MIT