#Router<PgPool> does not work with await

2 messages · Page 1 of 1 (latest)

sleek flicker
#

I can't implement a router for axum...

// router.rs
use axum::Router;
use axum::routing::{get, put};
use sqlx::{Pool, Postgres};

use crate::handlers::{
    health,
    projects::{create_project, delete_project, update_project}
};
use crate::handlers::projects::list_projects;

pub fn init() -> Router<Pool<Postgres>> {
    let router = Router::new()
        
        .route("/", get(health))
        
        .route("/projects", get(list_projects).post(create_project))
        .route("/projects/:id", put(update_project).delete(delete_project));
    router
}
// main.rs
    ...
    info!("Initializing router...");
    let router = api::router::init();
    
    info!("Starting server on {}, port {}", "localhost".to_string(), 8080);
    let listener = tokio::net::TcpListener::bind("0.0.0.0:8080")
        .await
        .unwrap();
    axum::serve(listener, router)
        .await
        .unwrap();
// console log
error[E0277]: the trait bound `for<'a> Router<Pool<Postgres>>: tower_service::Service<IncomingStream<'a>>` is not satisfied
  --> src/main.rs:44:27
   |
44 |     axum::serve(listener, router)
   |     -----------           ^^^^^^ the trait `for<'a> tower_service::Service<IncomingStream<'a>>` is not implemented for `Router<Pool<Postgres>>`
   |     |
   |     required by a bound introduced by this call
   |
   = help: the following other types implement trait `tower_service::Service<Request>`:
             <Router as tower_service::Service<axum::http::Request<B>>>
             <Router as tower_service::Service<IncomingStream<'_>>>
note: required by a bound in `serve`
worthy iris