Created
October 18, 2022 19:20
-
-
Save AntanasGa/afdd35e6f717b928df8dbc418631d06f to your computer and use it in GitHub Desktop.
Rust actix spa example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Quick and dirty way to run spa apps on rust actix server | |
use dotenvy::dotenv; | |
use std::env; | |
use actix_web::{HttpServer, App, Result}; | |
use actix_web::dev::{ServiceRequest, ServiceResponse}; | |
use actix_files as a_fs; | |
#[actix_web::main] | |
async fn main() -> std::io::Result<()> { | |
dotenv().ok(); | |
let host = env::var("HOST").expect("HOST must be set"); | |
let port = env::var("PORT").expect("PORT must be set").parse::<u16>().expect("PORT must be a number"); | |
HttpServer::new(|| { | |
App::new() | |
.service(a_fs::Files::new("/", "[SPA DIR]").index_file("index.html").default_handler(index)) | |
}) | |
.bind((host, port))? | |
.run() | |
.await | |
} | |
async fn index(reqs: ServiceRequest) -> Result<ServiceResponse> { | |
let (req, _) = reqs.into_parts(); | |
let file = a_fs::NamedFile::open_async("[SPA DIR]/index.html").await?; | |
let res = file.into_response(&req); | |
Ok(ServiceResponse::new(req, res)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment