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 HOST: &str = "localhost:8000";
  5. pub const HEADER_TOKEN: &str = "token";
  6. #[derive(serde::Deserialize, Clone, Debug)]
  7. pub struct ClientServerConfiguration {
  8. #[serde(deserialize_with = "deserialize_number_from_string")]
  9. pub port: u16,
  10. pub host: String,
  11. pub http_scheme: String,
  12. pub ws_scheme: String,
  13. }
  14. pub fn get_client_server_configuration() -> Result<ClientServerConfiguration, config::ConfigError> {
  15. let mut settings = config::Config::default();
  16. let base = include_str!("../configuration/base.yaml");
  17. settings.merge(config::File::from_str(base, FileFormat::Yaml).required(true))?;
  18. let environment: Environment = std::env::var("APP_ENVIRONMENT")
  19. .unwrap_or_else(|_| "local".into())
  20. .try_into()
  21. .expect("Failed to parse APP_ENVIRONMENT.");
  22. let custom = match environment {
  23. Environment::Local => include_str!("../configuration/local.yaml"),
  24. Environment::Production => include_str!("../configuration/production.yaml"),
  25. };
  26. settings.merge(config::File::from_str(custom, FileFormat::Yaml).required(true))?;
  27. settings.try_into()
  28. }
  29. impl ClientServerConfiguration {
  30. pub fn reset_host_with_port(&mut self, host: &str, port: u16) {
  31. self.host = host.to_owned();
  32. self.port = port;
  33. }
  34. pub fn base_url(&self) -> String {
  35. format!("{}://{}:{}", self.http_scheme, self.host, self.port)
  36. }
  37. pub fn sign_up_url(&self) -> String {
  38. format!("{}/api/register", self.base_url())
  39. }
  40. pub fn sign_in_url(&self) -> String {
  41. format!("{}/api/auth", self.base_url())
  42. }
  43. pub fn sign_out_url(&self) -> String {
  44. format!("{}/api/auth", self.base_url())
  45. }
  46. pub fn user_profile_url(&self) -> String {
  47. format!("{}/api/user", self.base_url())
  48. }
  49. pub fn workspace_url(&self) -> String {
  50. format!("{}/api/workspace", self.base_url())
  51. }
  52. pub fn app_url(&self) -> String {
  53. format!("{}/api/app", self.base_url())
  54. }
  55. pub fn view_url(&self) -> String {
  56. format!("{}/api/view", self.base_url())
  57. }
  58. pub fn doc_url(&self) -> String {
  59. format!("{}/api/doc", self.base_url())
  60. }
  61. pub fn trash_url(&self) -> String {
  62. format!("{}/api/trash", self.base_url())
  63. }
  64. pub fn ws_addr(&self) -> String {
  65. format!("{}://{}:{}/ws", self.ws_scheme, self.host, self.port)
  66. }
  67. }
  68. pub enum Environment {
  69. Local,
  70. Production,
  71. }
  72. impl Environment {
  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. }