next_web_dev/application/
next_application.rs

1use std::ops::Deref;
2
3use hashbrown::HashMap;
4use next_web_core::context::properties::ApplicationProperties;
5
6use super::application::Application;
7use next_web_core::autoconfigure::context::server_properties::ServerProperties;
8
9#[derive(serde::Deserialize, Default)]
10pub struct NextApplication<A: Application> {
11    application_properties: ApplicationProperties,
12    application: A,
13}
14
15impl<A: Application + Default> NextApplication<A> {
16    pub fn new() -> Self {
17        Self {
18            application_properties: ApplicationProperties::new(),
19            application: A::default(),
20        }
21    }
22
23    /// Get the application register.
24    pub fn application_properties(&self) -> &ApplicationProperties {
25        &self.application_properties
26    }
27
28    /// Get the application name.
29    pub fn application_name(&self) -> &str {
30        self.application_properties()
31            .next()
32            .appliation()
33            .map(|var| var.name())
34            .unwrap_or_default()
35    }
36
37    /// Get the application context path.
38    pub fn server_context_path(&mut self) -> Option<&str> {
39        self.server_properties().context_path()
40    }
41
42    /// Get the application server port.
43    pub fn server_port(&mut self) -> Option<u16> {
44        self.server_properties().port()
45    }
46
47    /// The function to get the application server configuration.
48    pub fn server_properties(&self) -> &ServerProperties {
49        self.application_properties().next().server()
50    }
51
52    /// Get the application.
53    pub fn application(&mut self) -> &mut A {
54        &mut self.application
55    }
56
57    /// Set the application register.
58    pub fn set_application_properties(&mut self, application_properties: ApplicationProperties) {
59        self.application_properties = application_properties;
60    }
61
62    /// Get the application configure mappping.
63    pub fn set_configure_mappping(&mut self, mapping: HashMap<String, serde_yaml::Value>) {
64        self.application_properties.set_mapping(mapping);
65    }
66}
67
68impl<A: Application> Deref for NextApplication<A> {
69    type Target = A;
70
71    fn deref(&self) -> &Self::Target {
72        &self.application
73    }
74}