1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
use dbui_core::Result; use dbui_service::AppConfig; use dbui_service::RequestContext; use actix_session::Session; use actix_web::http::header::LOCATION; use actix_web::web::{Data, Path}; use actix_web::{HttpRequest, HttpResponse}; pub(crate) fn act<F>(session: &Session, cfg: &Data<AppConfig>, req: &HttpRequest, f: F) -> HttpResponse where F: Fn(&RequestContext) -> Result<maud::Markup> { let ctx = crate::req_context(&session, &cfg, &req, "index"); rsp(&ctx, f(&ctx)) } pub(crate) fn rsp(ctx: &RequestContext, res: Result<maud::PreEscaped<String>>) -> HttpResponse { match res { Ok(html) => ok(html.into_string()), Err(e) => { slog::warn!(ctx.log(), "{}", e); err(ctx, &e) } } } pub(crate) fn redir<F>(session: &Session, cfg: &Data<AppConfig>, req: &HttpRequest, f: F) -> HttpResponse where F: Fn(&RequestContext) -> Result<String> { let ctx = crate::req_context(&session, &cfg, &req, "index"); match f(&ctx) { Ok(path) => HttpResponse::SeeOther().header(LOCATION, path).finish().into_body(), Err(e) => { slog::warn!(ctx.log(), "{}", e); err(&ctx, &e) } } } pub(crate) fn ok(content: String) -> HttpResponse { HttpResponse::Ok().content_type("text/html; charset=utf-8").body(content) } pub(crate) fn err(ctx: &RequestContext, e: &dbui_core::Error) -> HttpResponse { let content = match dbui_templates::error::exception(ctx, e) { Ok(c) => c.into_string(), Err(e) => format!("A critical system error has occurred: {}", e.to_string()) }; HttpResponse::InternalServerError() .content_type("text/html; charset=utf-8") .body(content) } pub(crate) fn not_found(session: Session, cfg: Data<AppConfig>, path: Path<String>, req: HttpRequest) -> HttpResponse { let ctx = crate::req_context(&session, &cfg, &req, "index"); let content = match dbui_templates::error::not_found(&ctx, &path) { Ok(c) => c.into_string(), Err(e) => format!("A critical system error has occurred: {}", e.to_string()) }; HttpResponse::NotFound().content_type("text/html; charset=utf-8").body(content) }