Skip to content
This repository has been archived by the owner on Oct 23, 2022. It is now read-only.

Commit

Permalink
Fix: use custom tracing middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenein committed Jul 27, 2022
1 parent f4dcddd commit dc9304f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
6 changes: 4 additions & 2 deletions rusty-tractive-telegram-bot/src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::borrow::Cow;
use anyhow::Result;
use poem::http::StatusCode;
use poem::listener::TcpListener;
use poem::middleware::{AddData, Tracing};
use poem::middleware::AddData;
use poem::web::{Data, Json, TypedHeader};
use poem::{handler, post, EndpointExt, Route, Server};
use rusty_shared_telegram::api::BotApi;
Expand All @@ -13,6 +13,8 @@ use rusty_shared_telegram::{methods, models};
use secstr::SecUtf8;
use tracing::{debug, info, instrument, warn};

use crate::middleware::TracingMiddleware;

pub async fn run(
api: BotApi,
bind_endpoint: String,
Expand All @@ -38,7 +40,7 @@ pub async fn run(
.at("/", post(on_update))
.with(AddData::new(api))
.with(AddData::new(SecretToken(secret_token)))
.with(Tracing);
.with(TracingMiddleware);
Server::new(TcpListener::bind(bind_endpoint))
.run(app)
.await?;
Expand Down
4 changes: 3 additions & 1 deletion rusty-tractive-telegram-bot/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use std::time::Duration;

use anyhow::Result;
use clap::Parser;
use futures::future::try_join;
use rusty_shared_telegram::api::BotApi;
use rusty_shared_telegram::methods;
use rusty_shared_telegram::methods::Method;
use std::time::Duration;

use crate::listener::Listener;
use crate::opts::Opts;

mod bot;
mod listener;
mod middleware;
mod opts;

#[async_std::main]
Expand Down
48 changes: 48 additions & 0 deletions rusty-tractive-telegram-bot/src/middleware.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use poem::error::{MethodNotAllowedError, NotFoundError, ParsePathError, ParseQueryError};
use poem::http::StatusCode;
use poem::{Endpoint, IntoResponse, Middleware, Request, Response, Result};
use tracing::{error, info};

pub struct TracingMiddleware;

impl<E: Endpoint<Output = Response>> Middleware<E> for TracingMiddleware {
type Output = TracingMiddlewareImpl<E>;

fn transform(&self, ep: E) -> Self::Output {
TracingMiddlewareImpl { ep }
}
}

pub struct TracingMiddlewareImpl<E> {
ep: E,
}

#[poem::async_trait]
impl<E: Endpoint<Output = Response>> Endpoint for TracingMiddlewareImpl<E> {
type Output = Response;

async fn call(&self, request: Request) -> Result<Self::Output> {
let method = request.method().clone();
let uri = request.uri().clone();
match self.ep.call(request).await {
Err(error) if error.is::<NotFoundError>() => {
info!(?method, ?uri, "{:#}", error);
Ok(StatusCode::NOT_FOUND.into_response())
}
Err(error) if error.is::<MethodNotAllowedError>() => {
info!(?method, ?uri, "{:#}", error);
Ok(StatusCode::METHOD_NOT_ALLOWED.into_response())
}
Err(error) => {
if error.is::<ParseQueryError>() || error.is::<ParsePathError>() {
info!(?method, ?uri, "{:#}", error);
Ok(StatusCode::BAD_REQUEST.into_response())
} else {
error!(?method, ?uri, "{:#}", error);
Ok(StatusCode::INTERNAL_SERVER_ERROR.into_response())
}
}
result => result,
}
}
}

0 comments on commit dc9304f

Please sign in to comment.