4 releases
0.1.3 | Aug 21, 2023 |
---|---|
0.1.2 | Jul 13, 2023 |
0.1.1 | Jul 12, 2023 |
0.1.0 | Jul 12, 2023 |
#13 in #endpoints
8KB
97 lines
Valensas Rocket - Rocket Client Library
This library provides a Rocket client with the capability of metric collection for endpoints. It allows you to create and configure a Rocket server instance to interact with web services.
Usage
To use this library, you need to create an instance of valensas_rocket::client::client_service::Client
and run spawn_rocket() method to launch the application.
For detailed information visit: https://docs.rs/valensas-rocket
Installation
Add the following to your Cargo.toml
file:
[dependencies]
valensas-rocket = "0.1.2"
lib.rs
:
Rocket Client Library
This library provides a Rocket client with metric collection for endpoints. It allows you to create and configure a Rocket server instance to interact with web services.
Example Usage
use valensas_actuator::metrics::{ArcRwLockPrometheus, PrometheusMetrics};
use valensas_rocket::client::client_params::ClientParams;
use valensas_rocket::client::client_service::Client;
use rocket::{get, routes, Route};
use std::sync::{Arc, RwLock};
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
#[tokio::main]
async fn main() {
// Create a vector of routes (endpoints)
let routes: Vec<Route> = routes![index];
// Optional: Create a Prometheus metrics object
// For further information about actuator library, visit: https://crates.io/crates/valensas-actuator
let prometheus = Arc::new(RwLock::new(PrometheusMetrics::new("your_namespace")));
let prometheus_fairing = ArcRwLockPrometheus::new(prometheus.clone());
// Create a new Rocket client
let client = Client::new(
ClientParams {
ip_addr: "127.0.0.1".to_string(),
port: "8000".to_string(),
},
routes,
Some(prometheus_fairing), // Pass the Prometheus object
)
// Optional: Set a managed state
//.set_manage(SomeState)
// Optional: Set a fairing
//.set_fairing(SomeFairing)
.spawn_rocket();
rocket::tokio::task::spawn(client.await)
.await
.unwrap()
.unwrap();
println!("Rocket server launched successfully!");
}
The example demonstrates how to create a Rocket client using the library. It sets up a single route ("/") with a handler function (index
). Optionally, it creates a Prometheus metrics object, sets a managed state (SomeState
), and attaches a fairing (SomeFairing
). The client is then launched using the spawn_rocket
function, and the Prometheus metrics can be accessed if available (For further information about metrics, visit: valensas-actuator). Finally, the client awaits completion and prints a success message.
Dependencies
~17–48MB
~836K SLoC