leptos-rs / start-axum-workspace

The Unlicense
48 stars 15 forks source link

can't get .with_state() to accept anything other than LeptosOptions #16

Closed blowback closed 8 months ago

blowback commented 8 months ago

I am trying to modify a start-axum-workspace project to use Postgres, so I need to pass a connection pool around. I tried to follow the Axum Substate technique (as demonstrated in the session_auth_axum example) but I always get this error:

error[E0308]: mismatched types
   --> crates/server/src/main.rs:39:21
    |
39  |         .with_state(state);
    |          ---------- ^^^^^ expected `LeptosOptions`, found `AppState`
    |          |
    |          arguments to this method are incorrect

I added a server/state.rs:

use axum::extract::FromRef;
use db::Pool;
use leptos::LeptosOptions;

// Take advantage of Axum;s Substate feature by deriving FromRef.
// Leptos requires you to have LeptosOptions in your State struct
// for the Leptos route handlers; this is the only way to have more
// than one item in Axum's State.
#[derive(FromRef, Debug, Clone)]
pub struct AppState {
    pub leptos_options: LeptosOptions,
    pub pool: Pool,
}

and modified server/main.rs:

use app::*;
use axum::Router;
use db::*;
use fileserv::file_and_error_handler;
use leptos::*;
use leptos_axum::{generate_route_list, LeptosRoutes};

pub mod fileserv;
pub mod state;
use crate::state::AppState;

#[tokio::main]
async fn main() {
    simple_logger::init_with_level(log::Level::Debug).expect("couldn't initialize logging");

    // Setting get_configuration(None) means we'll be using cargo-leptos's env values
    // For deployment these variables are:
    // <https://github.com/leptos-rs/start-axum#executing-a-server-on-a-remote-machine-without-the-toolchain>
    // Alternately a file can be specified such as Some("Cargo.toml")
    // The file would need to be included with the executable when moved to deployment
    let conf = get_configuration(None).await.unwrap();
    let leptos_options = conf.leptos_options;
    let addr = leptos_options.site_addr;
    let routes = generate_route_list(App);

    let pool = create_pool(env!("DATABASE_URL"));

    let state = AppState {
        leptos_options,
        pool: pool.clone(),
    };

    // build our application with a route
    let app = Router::new()
        .leptos_routes(&leptos_options, routes, App)
        .fallback(file_and_error_handler)
        // .with_state(leptos_options)
        // .with_state(pool);
        .with_state(state);

    // run our app with hyper
    // `axum::Server` is a re-export of `hyper::Server`
    log::info!("listening on http://{}", &addr);
    let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
    axum::serve(listener, app.into_make_service())
        .await
        .unwrap();
}

and modified server/fileserv.js:

use app::App;
use axum::response::Response as AxumResponse;
use axum::{
    body::Body,
    debug_handler,
    extract::State,
    http::{Request, Response, StatusCode, Uri},
    response::IntoResponse,
};
use leptos::*;
use tower::ServiceExt;
use tower_http::services::ServeDir;

#[debug_handler]
pub async fn file_and_error_handler(
    uri: Uri,
    // State(state): State<AppState>,
    State(options): State<LeptosOptions>,
    req: Request<Body>,
) -> AxumResponse {
    let root = options.site_root.clone();
    let res = get_static_file(uri.clone(), &root).await.unwrap();

    if res.status() == StatusCode::OK {
        res.into_response()
    } else {
        let handler =
            leptos_axum::render_app_to_stream(options.to_owned(), move || view! { <App/> });
        handler(req).await.into_response()
    }
}

async fn get_static_file(uri: Uri, root: &str) -> Result<Response<Body>, (StatusCode, String)> {
    let req = Request::builder()
        .uri(uri.clone())
        .body(Body::empty())
        .unwrap();
    // `ServeDir` implements `tower::Service` so we can call it with `tower::ServiceExt::oneshot`
    // This path is relative to the cargo root
    match ServeDir::new(root).oneshot(req).await {
        Ok(res) => Ok(res.map(Body::new)),
        Err(err) => Err((
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("Something went wrong: {err}"),
        )),
    }
}

I also tried making file_and_error_handler() take State(state): State<AppState> and got a different error:

error[E0277]: the trait bound `fn(Uri, State<AppState>, axum::http::Request<axum::body::Body>) -> impl std::future::Future<Output = axum::http::Response<axum::body::Body>> {fileserv::file_and_error_handler}: Handler<_, LeptosOptions>` is not satisfied
   --> crates/server/src/main.rs:36:19
    |
36  |         .fallback(file_and_error_handler)
    |          -------- ^^^^^^^^^^^^^^^^^^^^^^ the trait `Handler<_, LeptosOptions>` is not implemented for fn item `fn(Uri, State<AppState>, axum::http::Request<axum::body::Body>) -> impl std::future::Future<Output = axum::http::Response<axum::body::Body>> {fileserv::file_and_error_handler}`
    |          |
    |          required by a bound introduced by this call
    |
    = note: Consider using `#[axum::debug_handler]` to improve the error message

I'm sure I'm missing something terribly obvious...any clue gratefully received!

(I am following rustonnails.com, but swapping out Dioxus for Leptos.)

blowback commented 8 months ago

In server/main.rs I changed:

        .leptos_routes(&leptos_options, routes, App)

to

        .leptos_routes(&state, routes, App)

and that seems to have fixeded it. I tihnk i grasp why, but I lack the rust words to explain it ;)