mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 15:35:35 +01:00
Fix start app
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
|
use axum::extract::State;
|
||||||
use axum::response::IntoResponse;
|
use axum::response::IntoResponse;
|
||||||
use axum::routing::post;
|
use axum::routing::post;
|
||||||
use axum::{extract::Path, routing::get};
|
use axum::{extract::Path, routing::get};
|
||||||
|
|
||||||
use axum_prometheus::PrometheusMetricLayer;
|
use axum_prometheus::PrometheusMetricLayer;
|
||||||
use reqwest::StatusCode;
|
use reqwest::StatusCode;
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
net::SocketAddr,
|
net::SocketAddr,
|
||||||
@@ -23,7 +25,11 @@ use tracing::Level;
|
|||||||
use crate::bots_manager::{internal::start_bot, BOTS_DATA, BOTS_ROUTES, SERVER_PORT};
|
use crate::bots_manager::{internal::start_bot, BOTS_DATA, BOTS_ROUTES, SERVER_PORT};
|
||||||
|
|
||||||
pub async fn start_axum_server(stop_signal: Arc<AtomicBool>) {
|
pub async fn start_axum_server(stop_signal: Arc<AtomicBool>) {
|
||||||
async fn telegram_request(Path(token): Path<String>, input: String) -> impl IntoResponse {
|
async fn telegram_request(
|
||||||
|
State(start_bot_mutex): State<Arc<Mutex<()>>>,
|
||||||
|
Path(token): Path<String>,
|
||||||
|
input: String,
|
||||||
|
) -> impl IntoResponse {
|
||||||
let (_, r_tx) = match BOTS_ROUTES.get(&token).await {
|
let (_, r_tx) = match BOTS_ROUTES.get(&token).await {
|
||||||
Some(tx) => tx,
|
Some(tx) => tx,
|
||||||
None => {
|
None => {
|
||||||
@@ -33,11 +39,19 @@ pub async fn start_axum_server(stop_signal: Arc<AtomicBool>) {
|
|||||||
return StatusCode::NOT_FOUND;
|
return StatusCode::NOT_FOUND;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
'creator: {
|
||||||
|
let _guard = start_bot_mutex.lock().await;
|
||||||
|
|
||||||
|
if BOTS_ROUTES.contains_key(&token) {
|
||||||
|
break 'creator;
|
||||||
|
}
|
||||||
|
|
||||||
let start_result = start_bot(&bot_data.unwrap(), SERVER_PORT).await;
|
let start_result = start_bot(&bot_data.unwrap(), SERVER_PORT).await;
|
||||||
|
|
||||||
if !start_result {
|
if !start_result {
|
||||||
return StatusCode::SERVICE_UNAVAILABLE;
|
return StatusCode::SERVICE_UNAVAILABLE;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
BOTS_ROUTES.get(&token).await.unwrap()
|
BOTS_ROUTES.get(&token).await.unwrap()
|
||||||
}
|
}
|
||||||
@@ -79,9 +93,12 @@ pub async fn start_axum_server(stop_signal: Arc<AtomicBool>) {
|
|||||||
|
|
||||||
let (prometheus_layer, metric_handle) = PrometheusMetricLayer::pair();
|
let (prometheus_layer, metric_handle) = PrometheusMetricLayer::pair();
|
||||||
|
|
||||||
|
let start_bot_mutex = Arc::new(Mutex::new(()));
|
||||||
|
|
||||||
let app_router = axum::Router::new()
|
let app_router = axum::Router::new()
|
||||||
.route("/:token/", post(telegram_request))
|
.route("/:token/", post(telegram_request))
|
||||||
.layer(prometheus_layer);
|
.layer(prometheus_layer)
|
||||||
|
.with_state(start_bot_mutex);
|
||||||
|
|
||||||
let metric_router =
|
let metric_router =
|
||||||
axum::Router::new().route("/metrics", get(|| async move { metric_handle.render() }));
|
axum::Router::new().route("/metrics", get(|| async move { metric_handle.render() }));
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
use once_cell::sync::Lazy;
|
|
||||||
|
|
||||||
use teloxide::adaptors::throttle::Limits;
|
use teloxide::adaptors::throttle::Limits;
|
||||||
use teloxide::dispatching::Dispatcher;
|
use teloxide::dispatching::Dispatcher;
|
||||||
use teloxide::error_handlers::LoggingErrorHandler;
|
use teloxide::error_handlers::LoggingErrorHandler;
|
||||||
@@ -11,14 +9,12 @@ use teloxide::update_listeners::{StatefulListener, UpdateListener};
|
|||||||
use teloxide::{dptree, Bot};
|
use teloxide::{dptree, Bot};
|
||||||
|
|
||||||
use tokio::sync::mpsc::{self, UnboundedSender};
|
use tokio::sync::mpsc::{self, UnboundedSender};
|
||||||
use tokio::sync::Mutex;
|
|
||||||
use tokio_stream::wrappers::UnboundedReceiverStream;
|
use tokio_stream::wrappers::UnboundedReceiverStream;
|
||||||
|
|
||||||
use tracing::log;
|
use tracing::log;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
use std::convert::Infallible;
|
use std::convert::Infallible;
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use crate::bots_manager::BOTS_ROUTES;
|
use crate::bots_manager::BOTS_ROUTES;
|
||||||
use crate::config;
|
use crate::config;
|
||||||
@@ -50,11 +46,7 @@ pub fn get_listener() -> (
|
|||||||
(stop_token, stop_flag, tx, listener)
|
(stop_token, stop_flag, tx, listener)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub static START_BOT_LOCK: Lazy<Arc<Mutex<()>>> = Lazy::new(|| Arc::new(Mutex::new(())));
|
|
||||||
|
|
||||||
pub async fn start_bot(bot_data: &BotData, port: u16) -> bool {
|
pub async fn start_bot(bot_data: &BotData, port: u16) -> bool {
|
||||||
let _mutex_guard = START_BOT_LOCK.lock().await;
|
|
||||||
|
|
||||||
let bot = Bot::new(bot_data.token.clone())
|
let bot = Bot::new(bot_data.token.clone())
|
||||||
.set_api_url(config::CONFIG.telegram_bot_api.clone())
|
.set_api_url(config::CONFIG.telegram_bot_api.clone())
|
||||||
.throttle(Limits::default())
|
.throttle(Limits::default())
|
||||||
|
|||||||
Reference in New Issue
Block a user