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::routing::post;
|
||||
use axum::{extract::Path, routing::get};
|
||||
|
||||
use axum_prometheus::PrometheusMetricLayer;
|
||||
use reqwest::StatusCode;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use std::{
|
||||
net::SocketAddr,
|
||||
@@ -23,7 +25,11 @@ use tracing::Level;
|
||||
use crate::bots_manager::{internal::start_bot, BOTS_DATA, BOTS_ROUTES, SERVER_PORT};
|
||||
|
||||
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 {
|
||||
Some(tx) => tx,
|
||||
None => {
|
||||
@@ -33,10 +39,18 @@ pub async fn start_axum_server(stop_signal: Arc<AtomicBool>) {
|
||||
return StatusCode::NOT_FOUND;
|
||||
}
|
||||
|
||||
let start_result = start_bot(&bot_data.unwrap(), SERVER_PORT).await;
|
||||
'creator: {
|
||||
let _guard = start_bot_mutex.lock().await;
|
||||
|
||||
if !start_result {
|
||||
return StatusCode::SERVICE_UNAVAILABLE;
|
||||
if BOTS_ROUTES.contains_key(&token) {
|
||||
break 'creator;
|
||||
}
|
||||
|
||||
let start_result = start_bot(&bot_data.unwrap(), SERVER_PORT).await;
|
||||
|
||||
if !start_result {
|
||||
return StatusCode::SERVICE_UNAVAILABLE;
|
||||
}
|
||||
}
|
||||
|
||||
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 start_bot_mutex = Arc::new(Mutex::new(()));
|
||||
|
||||
let app_router = axum::Router::new()
|
||||
.route("/:token/", post(telegram_request))
|
||||
.layer(prometheus_layer);
|
||||
.layer(prometheus_layer)
|
||||
.with_state(start_bot_mutex);
|
||||
|
||||
let metric_router =
|
||||
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::dispatching::Dispatcher;
|
||||
use teloxide::error_handlers::LoggingErrorHandler;
|
||||
@@ -11,14 +9,12 @@ use teloxide::update_listeners::{StatefulListener, UpdateListener};
|
||||
use teloxide::{dptree, Bot};
|
||||
|
||||
use tokio::sync::mpsc::{self, UnboundedSender};
|
||||
use tokio::sync::Mutex;
|
||||
use tokio_stream::wrappers::UnboundedReceiverStream;
|
||||
|
||||
use tracing::log;
|
||||
use url::Url;
|
||||
|
||||
use std::convert::Infallible;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::bots_manager::BOTS_ROUTES;
|
||||
use crate::config;
|
||||
@@ -50,11 +46,7 @@ pub fn get_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 {
|
||||
let _mutex_guard = START_BOT_LOCK.lock().await;
|
||||
|
||||
let bot = Bot::new(bot_data.token.clone())
|
||||
.set_api_url(config::CONFIG.telegram_bot_api.clone())
|
||||
.throttle(Limits::default())
|
||||
|
||||
Reference in New Issue
Block a user