Merge pull request #1119 from IlyaBizyaev/feat/webhook-path

Allow serving path configuration for the webhook
This commit is contained in:
Waffle Maybe 2024-08-01 04:47:24 +00:00 committed by GitHub
commit 151ae673b6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 18 additions and 1 deletions

View file

@ -48,6 +48,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Implement `GetChatId` for `teloxide_core::types::{Chat, ChatJoinRequest, ChatMemberUpdated}`. - Implement `GetChatId` for `teloxide_core::types::{Chat, ChatJoinRequest, ChatMemberUpdated}`.
- Use [deadpool-redis](https://crates.io/crates/deadpool-redis) for Redis connection pooling ([PR 1081](https://github.com/teloxide/teloxide/pull/1081)). - Use [deadpool-redis](https://crates.io/crates/deadpool-redis) for Redis connection pooling ([PR 1081](https://github.com/teloxide/teloxide/pull/1081)).
- Add `MessageExt::filter_story` method for the corresponding `MediaKind::Story` variant ([PR 1087](https://github.com/teloxide/teloxide/pull/1087)). - Add `MessageExt::filter_story` method for the corresponding `MediaKind::Story` variant ([PR 1087](https://github.com/teloxide/teloxide/pull/1087)).
- Add `update_listeners::webhooks::Options::path`, an option to make the webhook server listen on a different path, which can be useful behind a reverse proxy.
### Fixed ### Fixed

View file

@ -23,6 +23,13 @@ pub struct Options {
/// [addr]: (self::Options.address) /// [addr]: (self::Options.address)
pub url: url::Url, pub url: url::Url,
/// Server-internal path to listen for requests on.
///
/// This can differ from the path in `url` when you use a reverse proxy.
///
/// Default - the URL path is reused.
pub path: String,
/// Upload your public key certificate so that the root certificate in use /// Upload your public key certificate so that the root certificate in use
/// can be checked. See Telegram's [self-signed guide] for details. /// can be checked. See Telegram's [self-signed guide] for details.
/// ///
@ -57,9 +64,11 @@ impl Options {
/// Construct a new webhook options, see [`Options::address`] and /// Construct a new webhook options, see [`Options::address`] and
/// [`Options::url`] for details. /// [`Options::url`] for details.
pub fn new(address: SocketAddr, url: url::Url) -> Self { pub fn new(address: SocketAddr, url: url::Url) -> Self {
let path = url.path().to_owned();
Self { Self {
address, address,
url, url,
path,
certificate: None, certificate: None,
max_connections: None, max_connections: None,
drop_pending_updates: false, drop_pending_updates: false,
@ -67,6 +76,13 @@ impl Options {
} }
} }
/// Specify a custom routing path. This can be useful when the server is
/// behind a reverse proxy. By default, the path will be taken from the
/// public URL.
pub fn path(self, path: String) -> Self {
Self { path, ..self }
}
/// Upload your public key certificate so that the root certificate in use /// Upload your public key certificate so that the root certificate in use
/// can be checked. See Telegram's [self-signed guide] for details. /// can be checked. See Telegram's [self-signed guide] for details.
/// ///

View file

@ -219,7 +219,7 @@ pub fn axum_no_setup(
let (stop_token, stop_flag) = mk_stop_token(); let (stop_token, stop_flag) = mk_stop_token();
let app = axum::Router::new() let app = axum::Router::new()
.route(options.url.path(), post(telegram_request)) .route(&options.path, post(telegram_request))
.layer(TraceLayer::new_for_http()) .layer(TraceLayer::new_for_http())
.with_state(WebhookState { .with_state(WebhookState {
tx: ClosableSender::new(tx), tx: ClosableSender::new(tx),