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
62
63
64
65
66
67
use crate::cfg::AppConfig;

use anyhow::Result;
use dbui_core::profile::UserProfile;
use uuid::Uuid;

/// Provides reverse routing of urls
pub trait Router {
  fn route(&self, path: &str, args: &[&str]) -> Result<String>;
  fn route_simple(&self, path: &str) -> Result<String> {
    const NO_PARAMS: [&str; 0] = [];
    self.route(path, &NO_PARAMS)
  }
  fn route_static(&self, path: &str) -> Result<String> {
    self.route("static", &[&path])
  }
}

/// Contains information about the application and current user, along with common components
pub struct RequestContext {
  app: AppConfig,
  user_id: Uuid,
  user_profile: UserProfile,
  flash: Option<(String, String)>,
  log: slog::Logger
}

impl std::fmt::Debug for RequestContext {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    write!(f, "RequestContext [{}] [{:?}]", self.user_id, self.user_profile)
  }
}

impl RequestContext {
  pub fn new(
    app: AppConfig, user_id: Uuid, user_profile: UserProfile, log: slog::Logger, flash: Option<(String, String)>
  ) -> RequestContext {
    let log = log.new(slog::o!("user_id" => user_id.to_string()));
    RequestContext {
      app,
      user_id,
      user_profile,
      flash,
      log
    }
  }

  pub fn app(&self) -> &AppConfig {
    &self.app
  }

  pub fn user_id(&self) -> &Uuid {
    &self.user_id
  }

  pub fn user_profile(&self) -> &UserProfile {
    &self.user_profile
  }

  pub fn flash(&self) -> &Option<(String, String)> {
    &self.flash
  }

  pub fn log(&self) -> &slog::Logger {
    &self.log
  }
}