reactive_graph_config_model/
graphql.rs1use 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#[derive(Debug, Clone, Default, Deserialize, Serialize)]
13pub struct GraphQLLoggingConfig {
14 pub enabled: bool,
16
17 pub format: Option<String>,
20}
21
22#[derive(Debug, Clone, Deserialize, Serialize)]
24pub struct GraphQLServerConfig {
25 pub enabled: Option<bool>,
27
28 pub hostname: Option<String>,
30
31 pub port: Option<u16>,
33
34 pub secure: Option<bool>,
36
37 pub ssl_certificate_path: Option<String>,
39
40 pub ssl_private_key_path: Option<String>,
42
43 pub shutdown_timeout: Option<u64>,
48
49 pub workers: Option<usize>,
52
53 pub default_context_path: Option<String>,
55
56 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}