Web Server Architecture
Relevant source files
The following files were used as context for generating this wiki page:
The rust-code-analysis-web tool provides a web service layer for the rust-code-analysis library, enabling source code analysis via a RESTful API. It is designed to allow developers to compute Supported Metrics, retrieve function spans, and perform comment removal over HTTP requests. The server is built using the Actix-web framework and supports concurrent request processing through a configurable worker pool.
Sources: rust-code-analysis-web/README.md, rust-code-analysis-book/src/commands/rest.md, rust-code-analysis-web/Cargo.toml:11-13
Core Architecture Components
The architecture follows a standard web server pattern where incoming HTTP requests are routed to specific handler functions. These handlers interact with the underlying rust-code-analysis core library to perform the requested operations.
Component Overview
| Component | Description |
|---|---|
| Actix HttpServer | The primary entry point that binds to a host/port and manages worker threads. |
| Request Handlers | Functions that process specific endpoints (e.g., metrics_json, ast_parser). |
| Payload Structures | Strongly-typed data structures for JSON deserialization (e.g., WebMetricsPayload). |
| Callback Trait | Interface between the web layer and the analysis library (e.g., WebMetricsCallback). |
| Language Guesser | Utility to detect the programming language based on file extensions and content. |
Sources: rust-code-analysis-web/src/web/server.rs:218-260, rust-code-analysis-web/src/web/metrics.rs:7-17
Server Execution Flow
The server initialization involves setting up the Actix-web App with resource definitions and guards based on content types.
API Endpoints and Data Flow
The server supports two primary interaction modes for its major endpoints: JSON-based and Plain/Octet-stream based.
Supported Endpoints
| Endpoint | Method | Input Format | Output Format | Description |
|---|---|---|---|---|
/ping | GET | N/A | Text | Checks server health. |
/ast | POST | JSON | JSON | Returns the Abstract Syntax Tree. |
/metrics | POST | JSON / Octet | JSON | Computes static code metrics. |
/comment | POST | JSON / Octet | JSON / Octet | Removes comments from code. |
/function | POST | JSON / Octet | JSON | Retrieves function start/end spans. |
Sources: rust-code-analysis-web/src/web/server.rs:222-259, rust-code-analysis-book/src/commands/rest.md
Metrics Processing Flow
When a request is sent to the /metrics endpoint, the server follows a sequence to guess the language, configure the analysis, and execute the library call.
Implementation Details
Configuration and Limits
The server enforces a default payload size limit of 4MB for both JSON and Octet-stream data to prevent resource exhaustion.
pub async fn run(host: &str, port: u16, n_threads: usize) -> std::io::Result<()> {
let max_size = 1024 * 1024 * 4;
HttpServer::new(move || {
App::new()
.app_data(web::JsonConfig::default().limit(max_size))
// ... routing
})
.workers(n_threads)
.bind((host, port))?
.run()
.await
}
Sources: rust-code-analysis-web/src/web/server.rs:218-223
Metric Callbacks
The server uses a specialized Callback implementation to bridge Actix-web types with the core analysis functions. The WebMetricsCallback specifically handles the logic for filtering "unit" metrics vs. detailed nested metrics.
impl Callback for WebMetricsCallback {
type Res = Value;
type Cfg = WebMetricsCfg;
fn call<T: ParserTrait>(cfg: Self::Cfg, parser: &T) -> Self::Res {
let spaces = metrics(parser, &cfg.path);
let spaces = if cfg.unit {
if let Some(mut spaces) = spaces {
spaces.spaces.clear(); // Only top-level
Some(spaces)
} else {
None
}
} else {
spaces
};
// ... serialization
}
}
Sources: rust-code-analysis-web/src/web/metrics.rs:56-74
Data Structures
Error Handling
Errors are returned as JSON objects containing the request ID and a descriptive error message, such as "The file extension doesn't correspond to a valid language".
Sources: rust-code-analysis-web/src/web/server.rs:17-21
Payload Example (JSON)
For the /metrics endpoint, the server expects a WebMetricsPayload:
id: Unique request identifier.file_name: Name used for language detection.code: The raw source code string.unit: Boolean to toggle top-level metrics only.
Sources: rust-code-analysis-web/src/web/metrics.rs:8-17
Conclusion
The Web Server Architecture serves as a high-performance wrapper around the rust-code-analysis library. By leveraging Actix-web's asynchronous worker model and providing dual-format (JSON/Plain) endpoints, it facilitates easy integration of static code analysis into remote workflows and distributed development environments.
Sources: rust-code-analysis-web/README.md, rust-code-analysis-web/src/web/server.rs:218-265