Skip to content

Commit

Permalink
polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
the2pizza committed Oct 18, 2024
1 parent 21acb93 commit 02012c4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ clap = { version = "4.0", features = ["derive"] }
time = "0.3"



4 changes: 3 additions & 1 deletion src/config2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ pub struct AppConfig {
pub api_mode: bool,
#[serde(default = "default_enabled")]
pub metrics_mode: bool,
pub api_token: String,
pub api_webhook_token: Option<String>,
#[serde(default = "default_enabled")]
pub api_webhook_enabled: bool,
}

#[derive(Clone, Debug, Deserialize, Default)]
Expand Down
11 changes: 9 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use ::clickhouse::Client;
use actix_web::middleware::Logger;
use actix_web::web::to;
use actix_web::web::Data;
use actix_web::{App, HttpServer};
use clap::Parser;
Expand Down Expand Up @@ -29,6 +30,7 @@ use crate::cpuusage::cpu_metrics;
use crate::loadavg::loadavg_metrics;
use crate::memory::mem_metrics;
use crate::utils::{current_timestamp, human_readable_date, level_from_settings};
use crate::web::not_found;

#[derive(Parser)]
#[command(version = "0.0.1", about = "Pony - montiroing tool for Xray/Wireguard")]
Expand Down Expand Up @@ -114,14 +116,19 @@ async fn main() -> std::io::Result<()> {
let settings_arc = Arc::new(settings.clone());

HttpServer::new(move || {
App::new()
let mut app = App::new()
.app_data(Data::new(ch_client.clone()))
.app_data(Data::new(settings_arc.clone()))
.wrap(Logger::default())
.service(web::hello)
.service(web::status_ch)
.service(web::status)
.service(webhook::webhook_handler)
.default_service(to(not_found));

if settings.app.api_webhook_enabled {
app = app.service(webhook::webhook_handler);
}
app
})
.bind(("0.0.0.0", 5005))?
.run()
Expand Down
4 changes: 4 additions & 0 deletions src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ struct Params {
cluster: String,
}

pub async fn not_found() -> HttpResponse {
HttpResponse::NotFound().body("404 - Not Found")
}

#[get("/")]
pub async fn hello() -> impl Responder {
HttpResponse::Ok().body("Hello, Fuckin World!")
Expand Down
6 changes: 5 additions & 1 deletion src/webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ async fn webhook_handler(
payload: web::Json<WebhookPayload>,
settings: Data<Arc<Settings>>,
) -> impl Responder {
let verified = validate(auth.token(), &settings.app.api_token);
let verified = match settings.app.api_webhook_token.clone() {
Some(token) => validate(auth.token(), &token),
None => false,
};

if verified {
let settings_ref = Arc::as_ref(settings.get_ref()).clone();
if payload.status.trim() == "failed" {
Expand Down

0 comments on commit 02012c4

Please sign in to comment.