REST API: Ping & Health
Relevant source files
The following files were used as context for generating this wiki page:
The Ping & Health functionality within the rust-code-analysis-web server provides a simple mechanism for users and automated systems to verify the operational status of the analysis service. This endpoint is a core part of the REST API, designed to confirm that the server is up and responsive before initiating resource-heavy tasks like Metrics Computation or code parsing.
The implementation is lightweight, utilizing the Actix-web framework to handle incoming HTTP GET requests and returning a standardized success response.
Sources: rust-code-analysis-book/src/commands/rest.md:1-11, rust-code-analysis-web/README.md:1-5
API Architecture and Implementation
The health check system is integrated into the HttpServer factory. It is defined as a specific route within the Actix-web application configuration.
The Ping Endpoint
The /ping route is mapped to a handler function that returns a 200 OK status. While the implementation in the source code returns an empty body in the binary logic, the documentation specifies a JSON response for user interaction.
| Feature | Details |
|---|---|
| Endpoint Path | /ping |
| HTTP Method | GET |
| Handler Function | ping() |
| Response Content | 200 OK (Empty body in implementation; {"message": "pong"} in docs) |
Sources: rust-code-analysis-web/src/web/server.rs:205, rust-code-analysis-web/src/web/server.rs:252, rust-code-analysis-book/src/commands/rest.md:27-40
Request Lifecycle
When a client sends a GET request to the health endpoint, the Actix-web server processes it through the registered services defined in the run function.
The diagram shows the synchronous request flow for a standard health check.
Sources: rust-code-analysis-web/src/web/server.rs:205-207, rust-code-analysis-web/src/web/server.rs:252
Server Configuration and Health
The health of the API is also dependent on how the server is initialized. The rust-code-analysis-web tool allows configuration of the host, port, and parallel worker threads, which impacts the availability of the health endpoint under load.
Initialization Parameters
| Parameter | Flag | Default | Description |
|---|---|---|---|
| Host | --host | 127.0.0.1 | IP address for server binding. |
| Port | --port | 8080 | Port used for the REST service. |
| Jobs | -j | N/A | Number of parallel jobs/worker threads. |
Sources: rust-code-analysis-book/src/commands/rest.md:14-23, rust-code-analysis-web/src/web/server.rs:211-215
Implementation Detail
The server is started using the run function, which binds the application to the provided host and port.
pub async fn run(host: &str, port: u16, n_threads: usize) -> std::io::Result<()> {
HttpServer::new(move || {
App::new()
// ... other services ...
.service(web::resource("/ping").route(web::get().to(ping)))
})
.workers(n_threads)
.bind((host, port))?
.run()
.await
}
Sources: rust-code-analysis-web/src/web/server.rs:211-255
Verification and Testing
The project includes automated tests to ensure the /ping endpoint remains functional. These tests use actix_rt::test to initialize a mock service and verify the status code.
The flowchart illustrates the automated testing logic for the health check.
Sources: rust-code-analysis-web/src/web/server.rs:265-276
Testing Commands
To verify the health of the system during development, the following commands are used:
- Build Web Server:
cargo build -p rust-code-analysis-web - Run All Tests:
cargo test --workspace
Sources: rust-code-analysis-book/src/developers/README.md:32-41
Conclusion
The Ping & Health system in rust-code-analysis-web serves as the primary entry point for monitoring the analysis server. By providing a lightweight /ping endpoint, the system allows for robust health monitoring and ensures that integration layers can verify service availability before attempting complex code analysis operations.