reactive_graph_config_model/
graphql.rs

1use reactive_graph_remotes_model::InstanceAddress;
2use serde::Deserialize;
3use serde::Serialize;
4
5pub const GRAPHQL_DEFAULT_HOSTNAME: &str = "localhost";
6pub const GRAPHQL_DEFAULT_PORT: u16 = 31415;
7
8pub const GRAPHQL_SSL_CERTIFICATE_PATH: &str = "./keys/cert.pem";
9pub const GRAPHQL_SSL_PRIVATE_KEY_PATH: &str = "./keys/key.pem";
10
11/// Configuration for the logging middleware of the GraphQL server.
12#[derive(Debug, Clone, Default, Deserialize, Serialize)]
13pub struct GraphQLLoggingConfig {
14    /// If true, a request logging middleware is enabled.
15    pub enabled: bool,
16
17    /// The log format of the request logging middleware.
18    /// See: <https://docs.rs/actix-web/0.6.0/actix_web/middleware/struct.Logger.html#format>
19    pub format: Option<String>,
20}
21
22/// Configuration for the GraphQL server.
23#[derive(Debug, Clone, Deserialize, Serialize)]
24pub struct GraphQLServerConfig {
25    /// If false, the GraphQL server will be disabled.
26    pub enabled: Option<bool>,
27
28    /// The hostname to bind the GraphQL HTTP server.
29    pub hostname: Option<String>,
30
31    /// The port to bind the GraphQL HTTP server.
32    pub port: Option<u16>,
33
34    /// If true, HTTPS is enabled.
35    pub secure: Option<bool>,
36
37    /// The location of the certificate.
38    pub ssl_certificate_path: Option<String>,
39
40    /// The location of the private key.
41    pub ssl_private_key_path: Option<String>,
42
43    /// Timeout for graceful workers shutdown in seconds.
44    /// After receiving a stop signal, workers have this much time to finish serving requests.
45    /// Workers still alive after the timeout are force dropped.
46    /// By default shutdown timeout sets to 30 seconds.
47    pub shutdown_timeout: Option<u64>,
48
49    /// The number of workers to start.
50    /// The default worker count is the number of physical CPU cores available.
51    pub workers: Option<usize>,
52
53    /// The default context path which redirects the root context to a web resource provider.
54    pub default_context_path: Option<String>,
55
56    /// The logging middleware configuration.
57    pub logging: Option<GraphQLLoggingConfig>,
58}
59
60impl GraphQLServerConfig {
61    pub fn is_enabled(&self) -> bool {
62        self.enabled.unwrap_or(true)
63    }
64
65    pub fn hostname(&self) -> String {
66        self.hostname.clone().unwrap_or(String::from(GRAPHQL_DEFAULT_HOSTNAME))
67    }
68
69    pub fn port(&self) -> u16 {
70        self.port.unwrap_or(GRAPHQL_DEFAULT_PORT)
71    }
72
73    pub fn address(&self) -> InstanceAddress {
74        InstanceAddress::new(self.hostname(), self.port(), self.is_secure())
75    }
76
77    pub fn addr(&self) -> String {
78        format!("{}:{}", self.hostname(), self.port())
79    }
80
81    pub fn url(&self) -> String {
82        format!("{}://{}", self.protocol(), self.addr())
83    }
84
85    pub fn protocol(&self) -> &str {
86        if self.is_secure() { "https" } else { "http" }
87    }
88
89    pub fn is_secure(&self) -> bool {
90        self.secure.unwrap_or(false)
91    }
92
93    pub fn ssl_certificate_path(&self) -> String {
94        self.ssl_certificate_path.clone().unwrap_or(String::from(GRAPHQL_SSL_CERTIFICATE_PATH))
95    }
96
97    pub fn ssl_private_key_path(&self) -> String {
98        self.ssl_private_key_path.clone().unwrap_or(String::from(GRAPHQL_SSL_PRIVATE_KEY_PATH))
99    }
100
101    pub fn shutdown_timeout(&self) -> u64 {
102        self.shutdown_timeout.unwrap_or(30)
103    }
104
105    pub fn workers(&self) -> usize {
106        self.workers.unwrap_or(num_cpus::get_physical())
107    }
108
109    pub fn default_context_path(&self) -> Option<String> {
110        self.default_context_path.clone()
111    }
112}
113
114impl Default for GraphQLServerConfig {
115    fn default() -> Self {
116        GraphQLServerConfig {
117            enabled: None,
118            hostname: Some(String::from(GRAPHQL_DEFAULT_HOSTNAME)),
119            port: Some(GRAPHQL_DEFAULT_PORT),
120            secure: None,
121            ssl_certificate_path: Some(String::from(GRAPHQL_SSL_CERTIFICATE_PATH)),
122            ssl_private_key_path: Some(String::from(GRAPHQL_SSL_PRIVATE_KEY_PATH)),
123            shutdown_timeout: None,
124            workers: None,
125            default_context_path: None,
126            logging: None,
127        }
128    }
129}