This commit is contained in:
eraden 2023-12-28 09:19:32 +01:00
parent fa31b4ab74
commit b4b851b20f
13 changed files with 1819 additions and 51 deletions

1604
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -3,8 +3,13 @@ resolver = "2"
members = [
"crates/database",
"crates/events",
"crates/identity-agent",
"crates/router",
"crates/sessions-agent",
"crates/shared-config",
"crates/zt",
"bins/identity-agent"
]
[workspace.dependencies]
database = { path = "./crates/database" }
events = { path = "./crates/events" }

View File

@ -0,0 +1,10 @@
[package]
name = "identity-agent"
version = "0.1.0"
edition = "2021"
publish = false
[dependencies]
tokio = { version = "1.27.0", features = ["full"] }
database = { workspace = true }
events = { workspace = true }

View File

@ -0,0 +1 @@
fn main() {}

View File

@ -6,7 +6,5 @@ edition = "2021"
[dependencies]
sea-orm = { version = "0", features = ["runtime-tokio-rustls", "sqlx-sqlite", "sqlx-postgres"] }
sea-orm-migration = { version = "0", features = ["runtime-tokio-rustls", "sqlx-sqlite", "sqlx-postgres"] }
migration = { path = "../migration" }
entities = { path = "../entities" }
chrono = { version = "0.4.31", default-features = false, features = ["serde", "clock", "libc"] }
uuid = { version = "1.6.1", features = ["v4"] }

View File

@ -25,13 +25,15 @@ pub trait DatabaseUrl {
fn provided_url(&self) -> Option<&String>;
}
pub async fn run_migration<Migrator: MigrationTrait>(opts: &impl DatabaseUrl) {
pub async fn run_migration<Migrator: sea_orm_migration::prelude::MigrationTrait + Default>(
opts: &impl DatabaseUrl,
) {
let connection = sea_orm::Database::connect(opts.database_url())
.await
.expect("Failed to connect to database");
Migrator::up(&connection, None)
.await
.expect("Failed to run migration");
// Migrator ::default().up(&connection)
// .await
// .expect("Failed to run migration");
}
pub async fn db_connect(opts: &impl DatabaseUrl) -> DatabaseConnection {

View File

@ -16,7 +16,7 @@ pub enum UserEvent {
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
pub enum TokenEvent {
Invalidated
Invalidated,
}
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
@ -60,10 +60,19 @@ impl Bus {
Self { client }
}
pub async fn publish(&self, topic: &str, qos: QoS, retain: bool, app_event: AppEvent) -> Result<(), ()> {
pub async fn publish(
&self,
topic: &str,
qos: QoS,
retain: bool,
app_event: AppEvent,
) -> Result<(), ()> {
let bytes = bincode::serialize(&app_event).map_err(|_| ())?;
let payload = Bytes::from_iter(bytes.into_iter());
self.client.publish_bytes(topic, qos, retain, payload).await.map_err(|_| ())?;
self.client
.publish_bytes(topic, qos, retain, payload)
.await
.map_err(|_| ())?;
Ok(())
}
}
@ -91,9 +100,7 @@ pub async fn run<Handler, Fut>(
};
let publish = match event {
Event::Incoming(Incoming::Publish(publish)) => {
publish
}
Event::Incoming(Incoming::Publish(publish)) => publish,
Event::Incoming(_) => {
continue;
}

View File

@ -1,8 +0,0 @@
[package]
name = "identity-agent"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -1,3 +0,0 @@
fn main() {
println!("Hello, world!");
}

View File

@ -1,3 +1,2 @@
#[actix::main]
async fn main() {
}
async fn main() {}

8
crates/zt/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "zt"
version = "0.1.0"
edition = "2021"
[dependencies]
chrono = { version = "0.4.31", default-features = false, features = ["libc", "clock", "serde", "pure-rust-locales"] }
gumdrop = "0.8.1"

48
crates/zt/src/main.rs Normal file
View File

@ -0,0 +1,48 @@
use std::process::exit;
use gumdrop::Options;
mod new_cmd;
pub trait RunCmd {
fn run_cmd(self);
}
#[derive(Debug, Options)]
enum Cmd {
New(new_cmd::NewOpts),
}
impl RunCmd for Cmd {
fn run_cmd(self) {
match self {
Self::New(cmd) => cmd.run_cmd(),
}
}
}
#[derive(Debug, Options)]
struct Opts {
help: bool,
#[options(command)]
cmd: Option<Cmd>,
}
impl RunCmd for Opts {
fn run_cmd(self) {
match self {
Opts { help: true, .. } | Opts { cmd: None, .. } => {
println!("{}", new_cmd::NewOpts::usage());
exit(0);
}
Opts { cmd: Some(cmd), .. } => {
cmd.run_cmd();
}
}
}
}
fn main() {
let opts = Opts::parse_args_default_or_exit();
opts.run_cmd();
}

147
crates/zt/src/new_cmd.rs Normal file
View File

@ -0,0 +1,147 @@
use super::*;
use std::path::Path;
#[derive(Debug, Options)]
struct NewAgentOpts {
help: bool,
#[options(help = "Add database to actor", short = 'd')]
with_database: bool,
#[options(help = "Add event bus to actor", short = 'e')]
with_event_bus: bool,
#[options(help = "Add http server to actor", short = 'w')]
with_http: bool,
#[options(free)]
name: String,
}
impl RunCmd for NewAgentOpts {
fn run_cmd(self) {
use std::io::Write;
let agent_name = format!("{}-agent", self.name);
let bins_root = Path::new("bins");
std::fs::create_dir_all(&bins_root).expect("Failed to create binaries directory");
let agent_root = bins_root.join(agent_name.as_str());
std::fs::create_dir_all(&agent_root).expect("Failed to create agent sub-directory");
let mut cargo_file = std::fs::OpenOptions::new()
.write(true)
.create(true)
.open(&agent_root.join("Cargo.toml"))
.expect("Failed to create agent Cargo.toml file");
cargo_file
.write(b"[package]\n")
.expect("Failed to create agent Cargo.toml file");
cargo_file
.write(format!("name = {:?}\n", agent_name).as_bytes())
.expect("Failed to create agent Cargo.toml file");
cargo_file
.write(b"version = \"0.1.0\"\n")
.expect("Failed to create agent Cargo.toml file");
cargo_file
.write(b"edition = \"2021\"\n")
.expect("Failed to create agent Cargo.toml file");
cargo_file
.write(b"publish = false\n")
.expect("Failed to create agent Cargo.toml file");
cargo_file
.write(b"\n[dependencies]\n")
.expect("Failed to create agent Cargo.toml file\n");
cargo_file
.write(b"tokio = { version = \"1.27.0\", features = [\"full\"] }\n")
.expect("Failed to create agent Cargo.toml file");
if self.with_database {
cargo_file
.write(b"database = { workspace = true }\n")
.expect("Failed to create agent Cargo.toml file");
}
if self.with_event_bus {
cargo_file
.write(b"events = { workspace = true }\n")
.expect("Failed to create agent Cargo.toml file");
}
if self.with_http {}
let src_path = agent_root.join("src");
std::fs::create_dir_all(&src_path).expect("Failed to create agent src directory");
{
let mut main_file = std::fs::OpenOptions::new()
.write(true)
.create(true)
.open(src_path.join("main.rs"))
.expect("Failed to create main.rs for agent");
if self.with_database {
main_file
.write(b"mod migration;\n")
.expect("Failed to create agent main.rs");
}
main_file
.write(b"fn main() {\n")
.expect("Failed to create agent main.rs");
main_file
.write(b"}\n")
.expect("Failed to create agent main.rs");
}
if self.with_database {
let migration_path = src_path.join("migration");
std::fs::create_dir_all(&migration_path)
.expect("Failed to create agent src/migration directory");
let mut migration_file = std::fs::OpenOptions::new()
.write(true)
.create(true)
.open(migration_path.join("mod.rs"))
.expect("Failed to create main.rs for agent");
migration_file
.write(
r#"pub use sea_orm_migration::prelude::*;
pub struct Migrator;
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![]
}
}
"#
.as_bytes(),
)
.expect("Failed to fill agent src/migration module");
}
}
}
#[derive(Debug, Options)]
enum NewCmd {
Agent(NewAgentOpts),
}
impl RunCmd for NewCmd {
fn run_cmd(self) {
match self {
Self::Agent(opts) => opts.run_cmd(),
}
}
}
#[derive(Debug, Options)]
pub struct NewOpts {
help: bool,
#[options(command)]
cmd: Option<NewCmd>,
}
impl RunCmd for NewOpts {
fn run_cmd(self) {
match self {
NewOpts { help: true, .. } | NewOpts { cmd: None, .. } => {
println!("{}", NewOpts::usage());
exit(0);
}
NewOpts { cmd: Some(cmd), .. } => {
cmd.run_cmd();
}
}
}
}