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
60
61
use actix_session::Session;
use actix_web::http::header::LOCATION;
use actix_web::web::{Data, Path};
use actix_web::{HttpRequest, HttpResponse};
use anyhow::Result;
use dbui_service::Router;
use dbui_service::{AppConfig, RequestContext};

pub(crate) fn act<F>(session: &Session, cfg: &Data<AppConfig>, req: HttpRequest, f: F) -> HttpResponse
where F: Fn(&RequestContext, &dyn Router) -> Result<maud::Markup> {
  let ctx = crate::req_context(&session, &cfg, "index");
  let router = crate::util::router::RequestRouter::new(req);
  rsp(&ctx, &router, f(&ctx, &router))
}

pub(crate) fn rsp(ctx: &RequestContext, router: &dyn Router, res: Result<maud::PreEscaped<String>>) -> HttpResponse {
  match res {
    Ok(html) => ok(html.into_string()),
    Err(e) => {
      slog::warn!(ctx.log(), "{}", e);
      err(ctx, router, &e)
    }
  }
}

pub(crate) fn redir<F>(session: &Session, cfg: &Data<AppConfig>, req: HttpRequest, f: F) -> HttpResponse
where F: Fn(&RequestContext, &dyn Router) -> Result<String> {
  let ctx = crate::req_context(&session, &cfg, "index");
  let router = crate::util::router::RequestRouter::new(req);
  match f(&ctx, &router) {
    Ok(path) => HttpResponse::SeeOther().header(LOCATION, path).finish().into_body(),
    Err(e) => {
      slog::warn!(ctx.log(), "{}", e);
      err(&ctx, &router, &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, router: &dyn Router, e: &anyhow::Error) -> HttpResponse {
  let content = match dbui_templates::error::exception(ctx, router, 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, "index");
  let router = crate::util::router::RequestRouter::new(req);
  let content = match dbui_templates::error::not_found(&ctx, &router, &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)
}