Description

This example has one route at / where the homepage is served and shows you how you can serve HTML or other types of files with Actix Web.

Note that static assets are declared in the Klyra.toml file.

You can clone the example below by running the following (you’ll need klyra CLI installed):

klyra init --from klyra-hq/klyra-examples --subfolder actix-web/static-files

Code

use actix_files::NamedFile;
use actix_web::{get, web::ServiceConfig, Responder};
use klyra_actix_web::KlyraActixWeb;

#[get("/")]
async fn index() -> impl Responder {
    NamedFile::open_async("assets/index.html").await
}

#[klyra_runtime::main]
async fn actix_web() -> KlyraActixWeb<impl FnOnce(&mut ServiceConfig) + Send + Clone + 'static> {
    let config = move |cfg: &mut ServiceConfig| {
        cfg.service(index);
    };

    Ok(config.into())
}

Usage

After you clone the example, launch it locally by using klyra run then visit the home route at http://localhost:8000 - you should see a homepage that shows our included HTML file.

You can extend this example by adding more routes that serve other files.


If you want to explore other frameworks, we have more examples with popular ones like Tower and Warp. You can find them right here.

Be sure to check out the examples repo for many more examples!