Add initial services

This commit is contained in:
2022-10-03 23:00:31 +02:00
parent b01b33f7d1
commit 6234cf18e8
22 changed files with 932 additions and 91 deletions

View File

@@ -0,0 +1,36 @@
[package]
name = "como_infrastructure"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
como_core = { path = "../como_core" }
como_domain = { path = "../como_domain" }
async-graphql = "4.0.6"
async-graphql-axum = "*"
axum = "0.5.13"
axum-extra = { version = "*", features = ["cookie", "cookie-private"] }
axum-sessions = { version = "*" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.68"
tokio = { version = "1.20.1", features = ["full"] }
uuid = { version = "1.1.2", features = ["v4", "fast-rng"] }
sqlx = { version = "0.6", features = [
"runtime-tokio-rustls",
"postgres",
"migrate",
"uuid",
"offline",
] }
anyhow = "1.0.60"
dotenv = "0.15.0"
tracing = "0.1.36"
tracing-subscriber = { version = "0.3.15", features = ["env-filter"] }
tower-http = { version = "0.3.4", features = ["full"] }
argon2 = "0.4"
rand_core = { version = "0.6", features = ["std"] }
cookie = { version = "0.16", features = ["secure", "percent-encode"] }
clap = { version = "3", features = ["derive", "env"] }

View File

@@ -0,0 +1,8 @@
-- Add migration script here
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
username varchar not null,
password_hash varchar not null
);
CREATE unique index users_username_idx on users(username)

View File

@@ -0,0 +1,18 @@
#[derive(clap::Parser)]
pub struct AppConfig {
#[clap(long, env)]
pub database_url: String,
#[clap(long, env)]
pub rust_log: String,
#[clap(long, env)]
pub token_secret: String,
#[clap(long, env)]
pub port: u32,
#[clap(long, env)]
pub run_migrations: bool,
#[clap(long, env)]
pub seed: bool,
#[clap(long, env)]
pub cors_origin: String,
}

View File

@@ -0,0 +1,33 @@
use anyhow::Context;
use sqlx::{postgres::PgPoolOptions, Pool, Postgres};
use tracing::log::info;
pub type ConnectionPool = Pool<Postgres>;
pub struct ConnectionPoolManager;
impl ConnectionPoolManager {
pub async fn new_pool(
connection_string: &str,
run_migrations: bool,
) -> anyhow::Result<ConnectionPool> {
info!("initializing the database connection pool");
let pool = PgPoolOptions::new()
.max_connections(5)
.connect(connection_string)
.await
.context("error while initializing the database connection pool")?;
if run_migrations {
info!("migrations enabled");
info!("migrating database");
sqlx::migrate!()
.run(&pool)
.await
.context("error while running database migrations");
}
Ok(pool)
}
}

View File

@@ -0,0 +1,5 @@
pub mod configs;
pub mod database;
pub mod register;
pub mod repositories;
pub mod services;

View File

@@ -0,0 +1,38 @@
use std::sync::Arc;
use como_core::{items::DynItemService, projects::DynProjectService, users::DynUserService};
use tracing::log::info;
use crate::{
configs::AppConfig,
database::ConnectionPool,
services::{
item_service::DefaultItemService, project_service::DefaultProjectService,
user_service::DefaultUserService,
},
};
#[derive(Clone)]
pub struct ServiceRegister {
pub item_service: DynItemService,
pub project_service: DynProjectService,
pub user_service: DynUserService,
}
impl ServiceRegister {
pub fn new(_pool: ConnectionPool, _config: AppConfig) -> Self {
info!("creating services");
let item_service = Arc::new(DefaultItemService::new()) as DynItemService;
let project_service = Arc::new(DefaultProjectService::new()) as DynProjectService;
let user_service = Arc::new(DefaultUserService::new()) as DynUserService;
info!("services created succesfully");
return Self {
item_service,
user_service,
project_service,
};
}
}

View File

@@ -0,0 +1,11 @@
use como_core::items::ItemService;
pub struct DefaultItemService {}
impl DefaultItemService {
pub fn new() -> Self {
Self {}
}
}
impl ItemService for DefaultItemService {}

View File

@@ -0,0 +1,3 @@
pub mod item_service;
pub mod project_service;
pub mod user_service;

View File

@@ -0,0 +1,11 @@
use como_core::projects::ProjectService;
pub struct DefaultProjectService {}
impl DefaultProjectService {
pub fn new() -> Self {
Self {}
}
}
impl ProjectService for DefaultProjectService {}

View File

@@ -0,0 +1,11 @@
use como_core::users::UserService;
pub struct DefaultUserService {}
impl DefaultUserService {
pub fn new() -> Self {
Self {}
}
}
impl UserService for DefaultUserService {}