credence_lib/server/
site.rs

1use super::{
2    super::{configuration::*, coordinator::*},
3    configuration::*,
4    routers::*,
5};
6
7use {axum::routing::*, kutil::http::axum::*, std::path::*};
8
9//
10// Site
11//
12
13/// Credence site.
14#[derive(Clone, Debug)]
15pub struct Site {
16    /// Configuration.
17    pub configuration: CredenceConfiguration,
18
19    /// Router
20    pub router: Router,
21}
22
23impl Site {
24    /// Constructor.
25    pub fn new<PathT>(assets_path: PathT, shutdown: &Shutdown) -> Result<Self, ConfigurationError>
26    where
27        PathT: AsRef<Path>,
28    {
29        let assets_path = assets_path.as_ref();
30
31        if !assets_path.exists() {
32            return Err(format!("assets path does not exist: {}", assets_path.display()).into());
33        } else if !assets_path.is_dir() {
34            return Err(format!("assets path is not a directory: {}", assets_path.display()).into());
35        }
36
37        let configuration = load_configuration(assets_path)?;
38        let cache = configuration.caching.cache();
39        let router = new_site_router(shutdown, &cache, &configuration);
40
41        Ok(Self { configuration, router })
42    }
43
44    /// Create a [Coordinator] if configured.
45    pub fn new_coordinator(&self) -> Result<Option<Coordinator>, ConfigurationError> {
46        Ok(self.configuration.files.coordinate.new_coordinator()?)
47    }
48}