* Made todo added responses ephemeral if /settings ephemeral is on
* Enabled systemd watchdog
* Move metrics to rocket
This commit is contained in:
jude
2024-06-04 18:24:24 +01:00
parent 064efd4386
commit de4ecf8dd6
15 changed files with 83 additions and 110 deletions

1
src/web/fairings/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod metrics;

View File

@ -2,9 +2,10 @@ mod consts;
#[macro_use]
mod macros;
mod catchers;
mod fairings;
mod guards;
mod metrics;
mod routes;
pub mod string {
use std::{fmt::Display, str::FromStr};
@ -79,7 +80,7 @@ use sqlx::{MySql, Pool};
use crate::web::{
consts::{CNC_GUILD, DISCORD_OAUTH_AUTHORIZE, DISCORD_OAUTH_TOKEN, SUBSCRIPTION_ROLES},
metrics::MetricProducer,
fairings::metrics::MetricProducer,
};
type Database = MySql;
@ -149,6 +150,7 @@ pub async fn initialize(
routes::report::report_error,
routes::return_to_same_site,
routes::terms,
routes::metrics,
],
)
.mount(

View File

@ -2,11 +2,14 @@ pub mod dashboard;
pub mod login;
pub mod report;
use std::collections::HashMap;
use std::{collections::HashMap, net::IpAddr};
use log::warn;
use rocket::{get, request::FlashMessage, serde::json::Value as JsonValue};
use rocket_dyn_templates::Template;
use crate::metrics::REGISTRY;
pub type JsonResult = Result<JsonValue, JsonValue>;
#[get("/")]
@ -107,3 +110,19 @@ pub async fn help_iemanager() -> Template {
let map: HashMap<&str, String> = HashMap::new();
Template::render("support/iemanager", &map)
}
#[get("/metrics")]
pub async fn metrics(client_ip: IpAddr) -> String {
if !client_ip.is_loopback() {
String::new()
} else {
let encoder = prometheus::TextEncoder::new();
let res_custom = encoder.encode_to_string(&REGISTRY.gather());
res_custom.unwrap_or_else(|e| {
warn!("Error encoding metrics: {:?}", e);
String::new()
})
}
}