Go to file
Mohamad.Elsena fe5184e18c demo001
2025-05-05 17:01:20 +02:00
.gitea/workflows build yml 2025-01-03 12:44:39 +01:00
config demo001 2025-05-05 17:01:20 +02:00
frontend demo001 2025-05-05 17:01:20 +02:00
src demo001 2025-05-05 17:01:20 +02:00
tests demo001 2025-05-05 17:01:20 +02:00
.env demo001 2025-05-05 17:01:20 +02:00
.gitignore init to use html/css/js 2025-05-05 13:25:23 +02:00
Cargo.lock demo001 2025-05-05 17:01:20 +02:00
Cargo.toml demo001 2025-05-05 17:01:20 +02:00
design.html demo001 2025-05-05 17:01:20 +02:00
Dockerfile demo001 2025-05-05 17:01:20 +02:00
form_data.db demo001 2025-05-05 17:01:20 +02:00
README.md demo001 2025-05-05 17:01:20 +02:00

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 username
  • INITIAL_ADMIN_PASSWORD: Initial admin password

Optional Environment Variables

  • ALLOWED_ORIGIN: CORS allowed origin
  • RUST_LOG: Log level (default: info)
  • SENTRY_DSN: Sentry DSN for error tracking
  • JWT_SECRET: JWT secret key
  • JWT_EXPIRATION: JWT expiration time in seconds

Development

  1. Clone the repository
  2. Install dependencies:
    cargo build
    
  3. Set up environment variables:
    cp .env.example .env
    # Edit .env with your configuration
    
  4. Run the development server:
    cargo run
    

Production Deployment

Docker

  1. Build the Docker image:

    docker build -t formies-backend .
    
  2. 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

  1. 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
    
  2. 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