config.rs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. pub const HOST: &'static str = "localhost:8000";
  2. pub const HTTP_SCHEMA: &'static str = "http";
  3. pub const WS_SCHEMA: &'static str = "ws";
  4. pub const HEADER_TOKEN: &'static str = "token";
  5. #[derive(Debug, Clone)]
  6. pub struct ServerConfig {
  7. http_schema: String,
  8. host: String,
  9. ws_schema: String,
  10. }
  11. impl std::default::Default for ServerConfig {
  12. fn default() -> Self {
  13. ServerConfig {
  14. http_schema: HTTP_SCHEMA.to_string(),
  15. host: HOST.to_string(),
  16. ws_schema: WS_SCHEMA.to_string(),
  17. }
  18. }
  19. }
  20. impl ServerConfig {
  21. pub fn new(host: &str, http_schema: &str, ws_schema: &str) -> Self {
  22. Self {
  23. http_schema: http_schema.to_owned(),
  24. host: host.to_owned(),
  25. ws_schema: ws_schema.to_owned(),
  26. }
  27. }
  28. fn scheme(&self) -> String { format!("{}://", self.http_schema) }
  29. pub fn sign_up_url(&self) -> String { format!("{}{}/api/register", self.scheme(), self.host) }
  30. pub fn sign_in_url(&self) -> String { format!("{}{}/api/auth", self.scheme(), self.host) }
  31. pub fn sign_out_url(&self) -> String { format!("{}{}/api/auth", self.scheme(), self.host) }
  32. pub fn user_profile_url(&self) -> String { format!("{}{}/api/user", self.scheme(), self.host) }
  33. pub fn workspace_url(&self) -> String { format!("{}{}/api/workspace", self.scheme(), self.host) }
  34. pub fn app_url(&self) -> String { format!("{}{}/api/app", self.scheme(), self.host) }
  35. pub fn view_url(&self) -> String { format!("{}{}/api/view", self.scheme(), self.host) }
  36. pub fn doc_url(&self) -> String { format!("{}{}/api/doc", self.scheme(), self.host) }
  37. pub fn trash_url(&self) -> String { format!("{}{}/api/trash", self.scheme(), self.host) }
  38. pub fn ws_addr(&self) -> String { format!("{}://{}/ws", self.ws_schema, self.host) }
  39. }