Skip to content

Rewrite it in Rust.#13

Merged
apognu merged 1 commit into
mainfrom
feat/rerite-it-in-rust
May 6, 2026
Merged

Rewrite it in Rust.#13
apognu merged 1 commit into
mainfrom
feat/rerite-it-in-rust

Conversation

@apognu

@apognu apognu commented Apr 2, 2026

Copy link
Copy Markdown
Contributor
image

Summary by CodeRabbit

  • New Features

    • Implemented Rust-based API service with authentication, health checks, and detection endpoints
  • Chores

    • Migrated CI/CD pipelines to Rust-focused workflows with cargo-based checks and builds
    • Updated Docker build to use Rust multistage compilation with optimized runtime image
    • Added code formatter configuration
    • Updated ignore patterns for build artifacts and model files
    • Removed legacy Python test suite and dependencies

@apognu apognu self-assigned this Apr 2, 2026
@apognu apognu force-pushed the feat/rerite-it-in-rust branch 6 times, most recently from 04ec794 to 0533fe3 Compare April 2, 2026 13:22
@apognu apognu force-pushed the feat/rerite-it-in-rust branch from 0533fe3 to 8130087 Compare April 2, 2026 13:26

@Pascal-Delange Pascal-Delange left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll grant you this one, rusty crab !

Comment thread Makefile Outdated
git clone https://huggingface.co/onnx-community/gliner_small-v2.1 model
cd model
git lfs pull
cd ..

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beautiful

@apognu apognu force-pushed the feat/rerite-it-in-rust branch 2 times, most recently from 5cae9ea to 89ccd6f Compare April 15, 2026 15:42
@apognu apognu force-pushed the feat/rerite-it-in-rust branch from 89ccd6f to f03f4e4 Compare May 6, 2026 07:07
@apognu apognu marked this pull request as ready for review May 6, 2026 08:00
@apognu apognu merged commit 241c479 into main May 6, 2026
6 of 7 checks passed
@coderabbitai coderabbitai Bot added the L label May 6, 2026
@coderabbitai

coderabbitai Bot commented May 6, 2026

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1235cd4b-c468-47eb-81b7-afbefe50a5ba

📥 Commits

Reviewing files that changed from the base of the PR and between 1a6705e and f03f4e4.

⛔ Files ignored due to path filters (3)
  • Cargo.lock is excluded by !**/*.lock
  • poetry.cpu.lock is excluded by !**/*.lock
  • poetry.gpu.lock is excluded by !**/*.lock
📒 Files selected for processing (20)
  • .dockerignore
  • .github/workflows/build.yml
  • .github/workflows/docker.yml
  • .github/workflows/production.yml
  • .github/workflows/staging.yml
  • .gitignore
  • .rustfmt.toml
  • Cargo.toml
  • Dockerfile
  • Makefile
  • README.md
  • logger.py
  • main.py
  • pyproject.cpu.toml
  • pyproject.gpu.toml
  • src/auth.rs
  • src/inference.rs
  • src/main.rs
  • src/trace.rs
  • test_main.py

📝 Walkthrough

Walkthrough

This PR migrates the NER service from Python (FastAPI/Poetry) to Rust (Axum), replacing the entire API implementation, adding structured tracing-based logging, Rust-focused CI/CD workflows, and Rust multistage Docker builds.

Changes

Python-to-Rust Service Migration

Layer / File(s) Summary
Project Foundation & Manifests
Cargo.toml, .rustfmt.toml, README.md
Rust project manifest with Axum, GLiNER, and logging dependencies; Rust formatter configuration with 200-character line width; README title placeholder updated.
CI/CD Workflows
.github/workflows/build.yml, .github/workflows/docker.yml, .github/workflows/staging.yml, .github/workflows/production.yml
Replaced Python/Poetry test workflow with Rust-based cargo check/lint/build matrix; updated docker workflow to use matrix strategy instead of inputs; staging and production workflows now depend on docker job for image builds.
Container Build & Ignore Files
Dockerfile, .dockerignore, .gitignore, Makefile
Switched from Python distroless runtime to Rust multistage build with base (model cloning), builder (cargo build), and distroless runtime; ignore patterns updated to exclude /model and /target; download target added for HuggingFace model fetching.
Core API & Request Handling
src/main.rs
Implemented Axum async web service with AppState (API key), routes for health check and /detect endpoint, JSON request/response serialization, error handling (empty results and inference failures return HTTP 500).
Authentication & Middleware
src/auth.rs, src/trace.rs
Bearer token extractor that validates against AppState.api_key; structured tracing initialization with JSON formatting and request-level API logger recording timestamp, IP, method, path, status, and latency.
Model & Inference
src/inference.rs
Lazy-initialized GLiNER model with optional CUDA support; label set from GLINER_LABELS environment variable; inference function wrapping model.infer() calls with anyhow error context.
Removed Python Code
main.py, logger.py, test_main.py, pyproject.cpu.toml, pyproject.gpu.toml
Deleted entire FastAPI application, JSON logging middleware, HTTP tests, and Poetry project configurations.

Sequence Diagram

sequenceDiagram
    participant Client
    participant Axum as Axum Router
    participant Auth as Auth Extractor
    participant Trace as API Logger
    participant Inference as Inference Engine
    participant Model as GLiNER Model

    Client->>Axum: POST /detect + Bearer Token
    Axum->>Trace: Log request (timestamp, method, path)
    Trace->>Auth: Forward request with headers
    Auth->>Auth: Extract Bearer token from Authorization header
    alt Token Valid
        Auth->>Inference: Return Auth, pass to handler
        Inference->>Model: Call infer(text)
        Model->>Model: Lazy-load GLiNER + CUDA if enabled
        Model-->>Inference: Return Vec<Vec<Span>>
        Inference->>Inference: Map spans to Output { kind, text }
        Inference-->>Axum: (StatusCode::OK, JSON array)
    else Token Missing/Invalid
        Auth-->>Axum: Reject (HTTP 401)
    end
    Axum->>Trace: Log response (status, latency, size)
    Trace-->>Client: HTTP response with result
Loading

Estimated Code Review Effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Suggested Labels

rust, migration, refactor, ci/cd, docker


From Python's halls we bid farewell,
Where FastAPI did gently dwell;
Now Axum's fire burns true and bright,
With Rust's own speed and zero-copy might—
A humble service, reborn in flame, 🦀⚡

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/rerite-it-in-rust

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants