Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serving subroutes in a static built page #56

Open
christ-offer opened this issue Mar 19, 2023 · 1 comment
Open

Serving subroutes in a static built page #56

christ-offer opened this issue Mar 19, 2023 · 1 comment

Comments

@christ-offer
Copy link

christ-offer commented Mar 19, 2023

Hey there.

Fair warning, I am quite new at this Rust thing - so bear with me if I'm asking something quite obvious.

I'm trying to serve a ./www/dist folder that is generated by Astro. (static html that has a interactive island).

So currently, I get it to build, but It only points to the root index.html, I can not access any subroutes, even though they too are just static html files in a subfolder(/projects/index.html).

It also does not serve my api::openmeteo endpoint.

This is my current main.rs, that works in cargo run, but not build --release:

use actix_files as fs;
use actix_web::{App, HttpServer};

mod api;
mod models;

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    std::env::set_var("RUST_LOG", "debug");
    std::env::set_var("RUST_BACKTRACE", "1");
    env_logger::init();

    HttpServer::new(|| {
        App::new()
        .service(api::openmeteo::openmeteo)
        .service(
            fs::Files::new("/", "./www/dist")
                .index_file("index.html")
                .show_files_listing(),
        )
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}

If I follow the guide in the readme I get something like this:

// build.rs
use static_files::resource_dir;

fn main() -> std::io::Result<()> {
    resource_dir("./www/dist").build()
}

// main.rs
include!(concat!(env!("OUT_DIR"), "/generated.rs"));

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    std::env::set_var("RUST_LOG", "debug");
    std::env::set_var("RUST_BACKTRACE", "1");
    env_logger::init();

    HttpServer::new(move || {
        let generated = generate();
        App::new()
        .service(ResourceFiles::new("/", generated))
        .service(api::openmeteo::openmeteo)
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}

This only ends up serving the index on /, the api/opemeteo route does not exist and only GET/HEAD requests are allowed.

However it does end up building/bundling the _astro folder containing the built css, so at least the index looks pretty.

So, I'm probably just doing something very silly here, but I'd appreciate a little pointer in the right direction. (I tried searching, but couldnt quite find what I was looking for)

@kilork
Copy link
Owner

kilork commented Mar 20, 2023

You can reorder your routes, static files route usually is on the last place. There is now also other possibility, if you strongly need it on the first place (I do not think in your case it is the case tbh): see #54

For static resources I think only GET and HEAD requests are supported. It is fine, you cannot POST or PUT to static read only resource.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants