MosaicπŸ“”

Server Development

How to set up and run the Mosaic server locally for development

Server Development

The Mosaic server is built with Rust using Actix Web and SQLx. This guide covers local development setup.

Prerequisites

  • Rust (latest stable, via rustup)
  • PostgreSQL 15+ with pgvector extension
  • FFmpeg (optional, for video processing)

Setup

1. Clone and enter the server directory

git clone https://github.com/crayonlu/Mosaic
cd Mosaic/server

2. Configure environment

cp .env.example .env
# Edit .env with your database credentials

3. Set up the database

Create a PostgreSQL database:

createdb mosaic
# or
psql -c "CREATE DATABASE mosaic;"

Enable the pgvector extension:

psql -d mosaic -c "CREATE EXTENSION IF NOT EXISTS vector;"

4. Run the server

cargo run

The server will:

  1. Read configuration from .env
  2. Connect to PostgreSQL
  3. Automatically run pending migrations
  4. Start listening on the configured port (default: 8080)

Project Structure

server/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ main.rs            # Entry point & initialization
β”‚   β”œβ”€β”€ config.rs          # Environment configuration
β”‚   β”œβ”€β”€ database.rs        # Database pool & migrations
β”‚   β”œβ”€β”€ error.rs           # Error types
β”‚   β”œβ”€β”€ middleware/        # Auth, CORS, logging
β”‚   β”œβ”€β”€ models/            # Data models
β”‚   β”œβ”€β”€ routes/            # API endpoints
β”‚   β”œβ”€β”€ services/          # Business logic
β”‚   β”œβ”€β”€ admin/             # Admin API & dashboard
β”‚   └── storage/           # File storage (local/S3)
β”œβ”€β”€ migrations/            # SQL migration files
β”œβ”€β”€ admin-ui/               # Admin dashboard frontend (React/Shadcn)
β”œβ”€β”€ Cargo.toml
└── .env.example

Key Dependencies

CratePurpose
actix-webHTTP server framework
sqlxPostgreSQL client with compile-time query checking
jsonwebtokenJWT authentication
bcryptPassword hashing
pgvectorVector embeddings for semantic search
opendalObject storage abstraction (S3/R2)
reqwestHTTP client for external AI API calls
imageImage processing (thumbnails)

Common Commands

cargo run              # Start the server
cargo check            # Check compilation (fast)
cargo build            # Build binary
cargo test             # Run tests
cargo clippy           # Lint checks

Database Migrations

Migrations are located in server/migrations/ and run automatically on startup. Each migration is a timestamped SQL file. To add a new migration:

touch server/migrations/$(date -u +"%Y%m%d%H%M%S")_your_description.sql

Write your SQL in the file. On the next server restart, it will be applied automatically.

Note: SQLx uses compile-time query verification. After changing migrations, run cargo sqlx prepare to update the offline query cache, otherwise the server may fail to compile if the database is not connected.

On this page