A REST API server written in Rust with Axum, Tokio and SQLite (through sqlx). It serves CRUD and search endpoints for a single "items" resource, with user accounts and JWT login, file uploads, a WebSocket event stream, an in-memory response cache, rate limiting, and health and metrics endpoints.
git clone https://github.com/Abdalla-Eldoumani/rust-http-server.git
cd rust-http-server
cargo build --release
mkdir -p data
./target/release/serverThe server listens on 127.0.0.1:3000 by default. cargo run --release --bin server works too. Logs go to stdout in a readable format; set LOG_FORMAT=json for JSON and RUST_LOG to change the level.
The default config creates data/app.db on first run; the data/ folder must exist. If the database cannot be opened, the server logs a warning and keeps running on an in-memory store, with accounts, files and jobs turned off. GET / reports which parts are enabled.
The workspace has two crates. core_lib holds everything: configuration, the database layer and migrations (including an FTS5 table for search), handlers, middleware, auth, files, jobs, cache, metrics and WebSocket handling. http_server is the server binary, which loads the config, opens the database, wires the services into the shared AppState and starts Axum.
Each request passes through, in order: security headers, request logging, request validation, metrics, rate limiting (when enabled), the response cache, optional JWT parsing, then CORS. /health/fast sits outside that stack and returns a fixed {"status":"ok"}.
| Area | Routes |
|---|---|
| Items | GET, POST /api/items; GET, PUT, PATCH, DELETE /api/items/{id}; the same under /api/v1/items and /api/v2/items (v2 has its own list, get, create and update handlers) |
| Search and export | GET /api/items/search (q, tags, date ranges, sort_by, sort_order, fuzzy, limit, offset); GET /api/items/export?format=json|csv|yaml |
| Accounts | POST /auth/register, /auth/login, /auth/refresh, /auth/logout; GET /auth/me, /auth/users/{id} |
| Files | POST /api/files/upload (multipart); GET /api/files, /api/files/{id}/info, /api/files/{id}/serve, /api/files/{id}/download; DELETE /api/files/{id}; POST /api/files/{id}/associate; GET /api/files/item/{id} |
| Jobs | POST, GET /api/jobs; GET /api/jobs/stats, /api/jobs/{id}, /api/jobs/{id}/status; DELETE /api/jobs/{id}/cancel; POST /api/jobs/{id}/retry, /api/jobs/cleanup, /api/jobs/bulk-import, /api/jobs/bulk-export |
| Cache | GET /api/cache/stats, /api/cache/health; POST /api/cache/clear, /api/cache/invalidate |
| Health | GET /health, /health/{component}, /ready, /live, /health/fast |
| Metrics | GET /api/stats, /api/metrics, /api/system/metrics, /api/performance/metrics, /api/system/alerts, /api/health/history |
| Other | GET /ws (WebSocket), GET /dashboard, /test, /websocket-test (HTML pages), POST /api/form, HEAD /api/head, OPTIONS /api/options |
Register with username, email, password and password_confirmation; log in with username_or_email set to the username (email login is not implemented) and password. Passwords are hashed with Argon2. Access tokens last one hour and refresh tokens seven days. The WebSocket pushes item and job events and, while anyone is connected, a metrics snapshot every five seconds.
docs/ has longer notes on the API, authentication, configuration, deployment and WebSocket messages.
Settings load in this order, later sources winning: built-in defaults, then config.toml in the working directory (or the file named by CONFIG_FILE), then environment variables of the form APP_<SECTION>_<KEY>. config-optimized.toml is a variant with rate limiting off and minimal logging, for load tests.
The binary applies server.host, server.port, database.url, the rate_limit, cors and logging sections, and files.upload_dir and files.temp_dir (which it creates). The other keys are read and validated but not used: file limits, cache size, token lifetimes and pool size are fixed in code.
Because _ separates both the section and the key, only single-word keys can be set from the environment. APP_SERVER_PORT=8080 and APP_DATABASE_URL=... work; APP_RATE_LIMIT_ENABLE=false is ignored.
The JWT signing key comes from the JWT_SECRET environment variable (at least 32 characters). Without it the server uses a default key written in the source, so set it for anything beyond local use. auth.jwt_secret in the config file is only checked for being non-empty.
cargo test --workspace --no-fail-fastThere are about 200 tests: unit tests in core_lib/src and integration tests in core_lib/tests. Most pass, but 8 to 10 fail depending on the run, mostly file-storage and concurrent database tests. The shell scripts in scripts/ (test_api.sh, test_features.sh and others) call a server already running on localhost:3000 with curl.
- Item, job and cache endpoints need no token. Roles (
user,admin) are stored and put in tokens, but no route is restricted to admins. GET /auth/users/{id}returns any user's profile, email included, without a token.- The six job types are simulated: a worker waits, then stores a fixed result.
- Request bodies are capped at 1 MB, so larger uploads fail even though the upload setting says 10 MB.
- No published benchmarks. The release profile uses fat LTO and one codegen unit, so a release build takes a few minutes.
MIT. See LICENSE.