Skip to content

Commit 47c8ccd

Browse files
authored
Merge pull request #231 from H1rono/handler
Handler
2 parents cc9cb73 + 62420b7 commit 47c8ccd

File tree

8 files changed

+575
-5
lines changed

8 files changed

+575
-5
lines changed

Cargo.lock

+32-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+22
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,27 @@ version = "0.1"
5656
features = []
5757
optional = true
5858

59+
[dependencies.tower]
60+
version = "0.4"
61+
default-features = false
62+
features = ["util"]
63+
optional = true
64+
65+
[dependencies.tower-http]
66+
version = "0.6"
67+
default-features = false
68+
optional = true
69+
70+
[dependencies.futures]
71+
version = "0.3"
72+
features = []
73+
optional = true
74+
75+
[dependencies.pin-project-lite]
76+
version = "0.2"
77+
features = []
78+
optional = true
79+
5980
[dev-dependencies]
6081
http = "1"
6182
futures = { version = "0.3", features = ["executor"] }
@@ -65,3 +86,4 @@ uuid = ["dep:uuid"]
6586
time = ["dep:time"]
6687
chrono = ["dep:chrono"]
6788
http = ["dep:http", "dep:http-body", "dep:http-body-util"]
89+
tower = ["http", "dep:tower", "dep:tower-http", "dep:futures", "dep:pin-project-lite"]

examples/handler-with-axum/Cargo.toml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "handler-with-axum"
3+
version = "0.1.0"
4+
edition = "2021"
5+
publish = false
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
http = "1"
11+
tower = "0.4"
12+
axum = "0.7"
13+
tokio = { version = "1", features = ["full"] }
14+
traq-bot-http.path = "../.."
15+
traq-bot-http.features = ["tower"]
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::{env, net::SocketAddr};
2+
3+
use axum::{routing::post_service, Router};
4+
use http::StatusCode;
5+
use tokio::net::TcpListener;
6+
use tower::service_fn;
7+
8+
use traq_bot_http::{payloads, RequestParser};
9+
10+
#[tokio::main]
11+
async fn main() {
12+
let verification_token = env::var("VERIFICATION_TOKEN").unwrap();
13+
let parser = RequestParser::new(&verification_token);
14+
let handler = parser
15+
.into_handler()
16+
.on_message_created(service_fn(on_message_created));
17+
let app = Router::new().route(
18+
"/",
19+
post_service(handler).handle_error(|_| async { StatusCode::INTERNAL_SERVER_ERROR }),
20+
);
21+
let addr = SocketAddr::from(([127, 0, 0, 1], 8080));
22+
let server = TcpListener::bind(addr).await.unwrap();
23+
axum::serve(server, app).await.unwrap();
24+
}
25+
26+
async fn on_message_created(
27+
payload: payloads::MessageCreatedPayload,
28+
) -> Result<(), std::convert::Infallible> {
29+
print!(
30+
"{}さんがメッセージを投稿しました。\n内容: {}\n",
31+
payload.message.user.display_name, payload.message.text
32+
);
33+
Ok(())
34+
}

src/error.rs

+5
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ pub enum ErrorKind {
5757
ReadBodyFailed,
5858
/// リクエストボディの値をパースできなかった
5959
ParseBodyFailed,
60+
/// イベントハンドラ実行中のエラー
61+
Handler,
6062
}
6163

6264
/// type alias
@@ -100,6 +102,8 @@ impl Error {
100102
error_with_source! {pub(crate) BotEventMismatch}
101103
error_with_source! {pub(crate) ReadBodyFailed}
102104
error_with_source! {pub(crate) ParseBodyFailed}
105+
// cfg(not(feature = "tower")) でdead_codeになる
106+
error_with_source! {#[allow(dead_code)] pub(crate) Handler}
103107
}
104108

105109
impl From<ErrorKind> for Error {
@@ -135,6 +139,7 @@ impl ErrorKind {
135139
Self::BotEventMismatch => "X-TRAQ-BOT-EVENT value is wrong",
136140
Self::ReadBodyFailed => "Failed to read request body",
137141
Self::ParseBodyFailed => "Failed to parse request body",
142+
Self::Handler => "Event handler raised an error",
138143
}
139144
}
140145
}

0 commit comments

Comments
 (0)