tesla-charge-controller/charge-controller-supervisor/src/web.rs

32 lines
765 B
Rust
Raw Normal View History

2024-12-28 18:54:21 +11:00
use rocket::{get, routes};
pub fn rocket() -> rocket::Rocket<rocket::Build> {
rocket::build().mount("/", routes![metrics,])
}
#[get("/metrics")]
fn metrics() -> Result<String, ServerError> {
Ok(
prometheus::TextEncoder::new()
.encode_to_string(&prometheus::default_registry().gather())?,
)
}
enum ServerError {
Prometheus,
}
impl From<prometheus::Error> for ServerError {
fn from(_: prometheus::Error) -> Self {
Self::Prometheus
}
}
impl<'a> rocket::response::Responder<'a, 'a> for ServerError {
fn respond_to(self, _: &'a rocket::Request<'_>) -> rocket::response::Result<'a> {
Err(match self {
ServerError::Prometheus => rocket::http::Status::InternalServerError,
})
}
}