Added a service for generating IDs
This commit is contained in:
parent
5241b18b0d
commit
766fd8ea7d
|
@ -79,6 +79,7 @@ tower-http = "0.5"
|
||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
tracing-subscriber = "0.3"
|
tracing-subscriber = "0.3"
|
||||||
ts-rs = "7"
|
ts-rs = "7"
|
||||||
|
ulid = "1"
|
||||||
unicode-segmentation = "1.10"
|
unicode-segmentation = "1.10"
|
||||||
url = "2.3"
|
url = "2.3"
|
||||||
walkdir = "2.3"
|
walkdir = "2.3"
|
||||||
|
@ -108,6 +109,7 @@ tokio = { workspace = true, features = ["full"] }
|
||||||
tokio-stream = { workspace = true }
|
tokio-stream = { workspace = true }
|
||||||
tower = { workspace = true }
|
tower = { workspace = true }
|
||||||
tower-http = { workspace = true, features = ["cors", "trace", "fs"] }
|
tower-http = { workspace = true, features = ["cors", "trace", "fs"] }
|
||||||
|
ulid = { workspace = true }
|
||||||
url = { workspace = true }
|
url = { workspace = true }
|
||||||
idna = { workspace = true }
|
idna = { workspace = true }
|
||||||
|
|
||||||
|
@ -118,6 +120,7 @@ tracing = { workspace = true }
|
||||||
|
|
||||||
cfg-if = { workspace = true }
|
cfg-if = { workspace = true }
|
||||||
|
|
||||||
|
bytes = { workspace = true }
|
||||||
compact_str = { workspace = true }
|
compact_str = { workspace = true }
|
||||||
either = { workspace = true }
|
either = { workspace = true }
|
||||||
futures = { workspace = true }
|
futures = { workspace = true }
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
use std::{sync::Arc, time::SystemTime};
|
||||||
|
|
||||||
|
use super::MagnetarService;
|
||||||
|
|
||||||
|
pub struct GenIdService;
|
||||||
|
|
||||||
|
impl GenIdService {
|
||||||
|
pub fn new_id(&self) -> ulid::Ulid {
|
||||||
|
ulid::Ulid::new()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_id_str(&self) -> String {
|
||||||
|
self.new_id().to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_for_time(&self, time: impl Into<SystemTime>) -> ulid::Ulid {
|
||||||
|
ulid::Ulid::from_datetime(time.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_str_for_time(&self, time: impl Into<SystemTime>) -> String {
|
||||||
|
self.new_for_time(time).to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AsRef<GenIdService> for Arc<MagnetarService> {
|
||||||
|
fn as_ref(&self) -> &GenIdService {
|
||||||
|
&self.gen_id
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
|
use gen_id::GenIdService;
|
||||||
use magnetar_common::config::MagnetarConfig;
|
use magnetar_common::config::MagnetarConfig;
|
||||||
use magnetar_model::{ck, CalckeyCache, CalckeyModel};
|
use magnetar_model::{ck, CalckeyCache, CalckeyModel};
|
||||||
use std::fmt::{Debug, Formatter};
|
use std::fmt::{Debug, Formatter};
|
||||||
|
@ -5,15 +6,16 @@ use std::time::Duration;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
pub mod emoji_cache;
|
pub mod emoji_cache;
|
||||||
|
pub mod gen_id;
|
||||||
pub mod generic_id_cache;
|
pub mod generic_id_cache;
|
||||||
pub mod instance_cache;
|
pub mod instance_cache;
|
||||||
pub mod instance_meta_cache;
|
pub mod instance_meta_cache;
|
||||||
pub mod local_user_cache;
|
pub mod local_user_cache;
|
||||||
|
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
|
|
||||||
pub struct MagnetarService {
|
pub struct MagnetarService {
|
||||||
pub db: CalckeyModel,
|
pub db: CalckeyModel,
|
||||||
|
pub gen_id: GenIdService,
|
||||||
pub cache: CalckeyCache,
|
pub cache: CalckeyCache,
|
||||||
pub config: &'static MagnetarConfig,
|
pub config: &'static MagnetarConfig,
|
||||||
pub local_user_cache: local_user_cache::LocalUserCacheService,
|
pub local_user_cache: local_user_cache::LocalUserCacheService,
|
||||||
|
@ -67,6 +69,14 @@ impl MagnetarService {
|
||||||
remote_instance_cache,
|
remote_instance_cache,
|
||||||
emoji_cache,
|
emoji_cache,
|
||||||
drive_file_cache,
|
drive_file_cache,
|
||||||
|
gen_id: GenIdService,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn service<T>(&self) -> &T
|
||||||
|
where
|
||||||
|
Self: AsRef<T>,
|
||||||
|
{
|
||||||
|
self.as_ref()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue