diff options
author | Keuin <[email protected]> | 2022-03-28 10:12:23 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2022-03-28 10:12:23 +0800 |
commit | 52013d1fde52360694f7ef5d8eed43585c8f4337 (patch) | |
tree | 58ebe5fb53b81fbc903613761f26c96d243da3ec | |
parent | 8b51713766743b1fa10bd5ff108886c72de8acae (diff) |
Support legacy GET request (for backward compatibility).
-rw-r--r-- | src/main.rs | 18 | ||||
-rw-r--r-- | src/web.rs | 22 |
2 files changed, 35 insertions, 5 deletions
diff --git a/src/main.rs b/src/main.rs index f67a6bc..86f1428 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; use std::convert::Infallible; use std::net::SocketAddr; use std::ops::Deref; @@ -59,25 +60,34 @@ async fn main() { let db = config.db_file.as_str(); info!(db, "Opening database..."); + // TODO replace Arc<DbPool> with DbPool::clone let db: Arc<DbPool> = Arc::new(database::open(db) .await.expect(&*format!("cannot open database {}", db))); info!("Spawning bot coroutine..."); let bot = Bot::new(config.bot_token); - let send_message = warp::path("message") - .and(warp::post()) + tokio::spawn(bot::repl(bot.clone(), db.clone())); + + info!("Initializing HTTP routes..."); + let route_post = warp::post() .and(warp::body::content_length_limit(MAX_BODY_LENGTH)) .and(warp::body::json()) .and(with_db(db.deref().clone())) .and(with_bot(bot.clone())) .and_then(web::handler); - tokio::spawn(bot::repl(bot, db.clone())); + let route_get = warp::get() + .and(warp::query::<HashMap<String, String>>()) + .and(with_db(db.deref().clone())) + .and(with_bot(bot.clone())) + .and_then(web::get_handler); + let routes = warp::path("message") + .and(route_post).or(route_get); info!("Starting HTTP server..."); let endpoint: SocketAddr = config.listen.parse() .expect("Cannot parse `listen` as endpoint."); info!("Start listening on {}", endpoint); - tokio::spawn(warp::serve(send_message).run(endpoint)); + tokio::spawn(warp::serve(routes).run(endpoint)); debug!("Waiting for Ctrl-C in main coroutine..."); tokio::signal::ctrl_c().await.unwrap(); @@ -1,3 +1,4 @@ +use std::collections::HashMap; use std::fmt; use std::fmt::Formatter; @@ -5,6 +6,7 @@ use serde_derive::{Deserialize, Serialize}; use teloxide::{prelude2::*}; use tracing::{debug, error, info, warn}; use warp::{Rejection, Reply}; +use warp::reply::Json; use crate::{Bot, database, DbPool}; @@ -32,8 +34,26 @@ impl fmt::Display for SendMessageResponse { } } +pub async fn get_handler(params: HashMap<String, String>, db: DbPool, bot: Bot) + -> std::result::Result<impl Reply, Rejection> { + let token = params.get("token"); + let message = params.get("message"); + if token == None || message == None { + return Ok(warp::reply::json(&SendMessageResponse { + success: false, + message: String::from(concat!("Missing parameters. ", + "Usage: GET /<path>?token=<kimikuri_token>&message=<message>")), + })); + } + let message = SendMessage { + token: token.unwrap().clone(), + message: message.unwrap().clone(), + }; + handler(message, db, bot).await +} + pub async fn handler(req: SendMessage, db: DbPool, bot: Bot) - -> std::result::Result<impl Reply, Rejection> { + -> std::result::Result<Json, Rejection> { info!("Income API request: {}", req); let user = database::get_user_by_token(&db, req.token.as_str()).await; |