event rcv

This commit is contained in:
quantulr
2024-01-18 14:21:54 +08:00
parent fda538adbb
commit 826b670b27
3 changed files with 13 additions and 5 deletions

1
Cargo.lock generated
View File

@ -327,6 +327,7 @@ version = "0.1.0"
dependencies = [
"axum",
"reqwest",
"serde",
"tokio",
"tower",
]

View File

@ -10,3 +10,4 @@ axum = "0.7.4"
reqwest = { version = "0.11.23", features = [] }
tokio = { version = "1.35.1", features = ["full"] }
tower = "0.4.13"
serde = { version = "1.0.195", features = ["derive"] }

View File

@ -1,10 +1,16 @@
use axum::{routing::get, Router};
use axum::{routing::{get, post}, Router, Json};
use axum::http::StatusCode;
use serde::Deserialize;
async fn event_rcv_handle() -> Result<String, (StatusCode, String)> {
tokio::spawn(async {
#[derive(Deserialize)]
struct EventRecv {
content: String,
}
async fn event_rcv_handle(Json(form_data): Json<EventRecv>) -> Result<String, (StatusCode, String)> {
tokio::spawn(async move {
println!("收到告警,向飞书推送消息");
reqwest::get("https://www.feishu.cn/flow/api/trigger-webhook/d96aa14944ed0595d831e9d68834b47b?content=收到告警信息").await
reqwest::get(format!("https://www.feishu.cn/flow/api/trigger-webhook/d96aa14944ed0595d831e9d68834b47b?content={}", &form_data.content)).await
});
Ok(String::from("success"))
}
@ -14,7 +20,7 @@ async fn main() {
// build our application with a single route
let app = Router::new()
.route("/", get(|| async { "Hello, World!" }))
.route("/eventRcv", get(event_rcv_handle));
.route("/eventRcv", post(event_rcv_handle));
// run our app with hyper, listening globally on port 3000
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();