feat: store base domain and protocol separately

This commit is contained in:
əlemi 2024-03-25 05:02:39 +01:00
parent de74669bd7
commit 76c0bd5218
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -2,11 +2,14 @@ use std::sync::Arc;
use sea_orm::DatabaseConnection; use sea_orm::DatabaseConnection;
use crate::dispatcher::Dispatcher;
#[derive(Clone)] #[derive(Clone)]
pub struct Context(Arc<ContextInner>); pub struct Context(Arc<ContextInner>);
struct ContextInner { struct ContextInner {
db: DatabaseConnection, db: DatabaseConnection,
domain: String, domain: String,
protocol: String,
} }
#[macro_export] #[macro_export]
@ -18,13 +21,18 @@ macro_rules! url {
impl Context { impl Context {
pub fn new(db: DatabaseConnection, mut domain: String) -> Self { pub fn new(db: DatabaseConnection, mut domain: String) -> Self {
if !domain.starts_with("http") { let protocol = if domain.starts_with("http://")
domain = format!("https://{domain}"); { "http://" } else { "https://" }.to_string();
}
if domain.ends_with('/') { if domain.ends_with('/') {
domain.replace_range(domain.len()-1.., ""); domain.replace_range(domain.len()-1.., "");
} }
Context(Arc::new(ContextInner { db, domain })) if domain.starts_with("http") {
domain = domain.replace("https://", "").replace("http://", "");
}
for _ in 0..1 { // TODO customize delivery workers amount
Dispatcher::spawn(db.clone(), domain.clone(), 30); // TODO ew don't do it this deep and secretly!!
}
Context(Arc::new(ContextInner { db, domain, protocol }))
} }
pub fn db(&self) -> &DatabaseConnection { pub fn db(&self) -> &DatabaseConnection {
@ -37,7 +45,7 @@ impl Context {
pub fn uri(&self, entity: &str, id: String) -> String { pub fn uri(&self, entity: &str, id: String) -> String {
if id.starts_with("http") { id } else { if id.starts_with("http") { id } else {
format!("{}/{}/{}", self.0.domain, entity, id) format!("{}{}/{}/{}", self.0.protocol, self.0.domain, entity, id)
} }
} }