Migrate to actix-web #8

Manually merged
tlater merged 14 commits from tlater/actix-web into master 2022-09-16 17:44:53 +01:00
Showing only changes of commit 2847dad110 - Show all commits

View file

@ -3,9 +3,9 @@ use std::net::SocketAddr;
use std::path::PathBuf; use std::path::PathBuf;
use actix_files::NamedFile; use actix_files::NamedFile;
use actix_web::http::StatusCode;
use actix_web::middleware::ErrorHandlers;
use actix_web::{get, web, App, HttpRequest, HttpResponse, HttpServer, Responder}; use actix_web::{get, web, App, HttpRequest, HttpResponse, HttpServer, Responder};
use actix_web::http::{Method, StatusCode};
use actix_web::middleware::{self, ErrorHandlers};
use clap::Parser; use clap::Parser;
use handlebars::Handlebars; use handlebars::Handlebars;
@ -56,6 +56,19 @@ async fn template(
} }
} }
#[get(r"/")]
async fn template_index(shared: web::Data<SharedData<'_>>) -> actix_web::Result<impl Responder> {
if shared.handlebars.has_template("index") {
let body = shared
.handlebars
.render("index", &())
.map_err(|_| UserError::InternalError)?;
Ok(HttpResponse::Ok().body(body))
} else {
Err(UserError::NotFound)?
}
}
#[get("/{filename:.*[^/]+}")] #[get("/{filename:.*[^/]+}")]
async fn static_file( async fn static_file(
shared: web::Data<SharedData<'_>>, shared: web::Data<SharedData<'_>>,
@ -104,6 +117,7 @@ async fn main() -> Result<(), std::io::Error> {
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
.wrap(middleware::NormalizePath::trim())
.wrap( .wrap(
ErrorHandlers::new() ErrorHandlers::new()
.handler(StatusCode::NOT_FOUND, generic_error) .handler(StatusCode::NOT_FOUND, generic_error)
@ -112,6 +126,8 @@ async fn main() -> Result<(), std::io::Error> {
.app_data(shared_data.clone()) .app_data(shared_data.clone())
.service(template) .service(template)
.service(static_file) .service(static_file)
.service(template_index)
.default_service(web::route().method(Method::GET))
}) })
.bind(config.address)? .bind(config.address)?
.run() .run()