configuration.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. use config::FileFormat;
  2. use serde_aux::field_attributes::deserialize_number_from_string;
  3. use std::convert::{TryFrom, TryInto};
  4. pub const HEADER_TOKEN: &str = "token";
  5. #[derive(serde::Deserialize, Clone, Debug)]
  6. pub struct ClientServerConfiguration {
  7. #[serde(deserialize_with = "deserialize_number_from_string")]
  8. pub port: u16,
  9. pub host: String,
  10. pub http_scheme: String,
  11. pub ws_scheme: String,
  12. }
  13. pub fn get_client_server_configuration() -> Result<ClientServerConfiguration, config::ConfigError> {
  14. let mut settings = config::Config::default();
  15. let base = include_str!("../configuration/base.yaml");
  16. settings.merge(config::File::from_str(base, FileFormat::Yaml).required(true))?;
  17. let environment: Environment = std::env::var("APP_ENVIRONMENT")
  18. .unwrap_or_else(|_| "local".into())
  19. .try_into()
  20. .expect("Failed to parse APP_ENVIRONMENT.");
  21. let custom = match environment {
  22. Environment::Local => include_str!("../configuration/local.yaml"),
  23. Environment::Production => include_str!("../configuration/production.yaml"),
  24. };
  25. settings.merge(config::File::from_str(custom, FileFormat::Yaml).required(true))?;
  26. settings.try_into()
  27. }
  28. impl ClientServerConfiguration {
  29. pub fn reset_host_with_port(&mut self, host: &str, port: u16) {
  30. self.host = host.to_owned();
  31. self.port = port;
  32. }
  33. pub fn base_url(&self) -> String {
  34. format!("{}://{}:{}", self.http_scheme, self.host, self.port)
  35. }
  36. pub fn sign_up_url(&self) -> String {
  37. format!("{}/api/register", self.base_url())
  38. }
  39. pub fn sign_in_url(&self) -> String {
  40. format!("{}/api/auth", self.base_url())
  41. }
  42. pub fn sign_out_url(&self) -> String {
  43. format!("{}/api/auth", self.base_url())
  44. }
  45. pub fn user_profile_url(&self) -> String {
  46. format!("{}/api/user", self.base_url())
  47. }
  48. pub fn workspace_url(&self) -> String {
  49. format!("{}/api/workspace", self.base_url())
  50. }
  51. pub fn app_url(&self) -> String {
  52. format!("{}/api/app", self.base_url())
  53. }
  54. pub fn view_url(&self) -> String {
  55. format!("{}/api/view", self.base_url())
  56. }
  57. pub fn doc_url(&self) -> String {
  58. format!("{}/api/doc", self.base_url())
  59. }
  60. pub fn trash_url(&self) -> String {
  61. format!("{}/api/trash", self.base_url())
  62. }
  63. pub fn ws_addr(&self) -> String {
  64. format!("{}://{}:{}/ws", self.ws_scheme, self.host, self.port)
  65. }
  66. }
  67. pub enum Environment {
  68. Local,
  69. Production,
  70. }
  71. impl Environment {
  72. #[allow(dead_code)]
  73. pub fn as_str(&self) -> &'static str {
  74. match self {
  75. Environment::Local => "local",
  76. Environment::Production => "production",
  77. }
  78. }
  79. }
  80. impl TryFrom<String> for Environment {
  81. type Error = String;
  82. fn try_from(s: String) -> Result<Self, Self::Error> {
  83. match s.to_lowercase().as_str() {
  84. "local" => Ok(Self::Local),
  85. "production" => Ok(Self::Production),
  86. other => Err(format!(
  87. "{} is not a supported environment. Use either `local` or `production`.",
  88. other
  89. )),
  90. }
  91. }
  92. }