diff --git a/Cargo.lock b/Cargo.lock index 704b111..cbbb48c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -557,23 +557,6 @@ dependencies = [ "inout", ] -[[package]] -name = "como_bff" -version = "0.1.0" -dependencies = [ - "anyhow", - "axum", - "axum-extra", - "axum-sessions", - "dotenv", - "sqlx", - "tokio", - "tower-http", - "tracing", - "tracing-subscriber", - "uuid", -] - [[package]] name = "como_bin" version = "0.1.0" @@ -598,6 +581,20 @@ dependencies = [ "uuid", ] +[[package]] +name = "como_core" +version = "0.1.0" + +[[package]] +name = "como_domain" +version = "0.1.0" +dependencies = [ + "anyhow", + "serde", + "serde_json", + "uuid", +] + [[package]] name = "constant_time_eq" version = "0.1.5" diff --git a/Cargo.toml b/Cargo.toml index 843f87c..37ecdb0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,2 +1,2 @@ [workspace] -members = ["como_bin", "como_bff"] +members = ["como_bin", "como_core", "como_domain"] diff --git a/como_bff/Cargo.toml b/como_bff/Cargo.toml deleted file mode 100644 index 23d500f..0000000 --- a/como_bff/Cargo.toml +++ /dev/null @@ -1,25 +0,0 @@ -[package] -name = "como_bff" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -axum = "0.5.13" -axum-extra = { version = "*", features = ["cookie", "cookie-private"] } -axum-sessions = { version = "*" } -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"] } diff --git a/como_bff/README.md b/como_bff/README.md deleted file mode 100644 index 0db1098..0000000 --- a/como_bff/README.md +++ /dev/null @@ -1,21 +0,0 @@ -# Como BFF - -```mermaid -sequenceDiagram - client ->> bff: request /events - bff ->> bff: Validates cookie como.sid - bff ->> bff: Cookie does not exist - bff ->> client: 401 - client ->> bff: Redirect /login - bff ->> auth: /login - auth ->> client: show webpage (give options) - client ->> auth: Submit credentials - auth ->> auth: Produce JWT - auth ->> bff: redirect with jwt/refreshtoken - bff ->> bff: wrap in cookie - bff ->> client: Redirect with cookie - client ->> bff: request /events - bff ->> events: forward request - events --> bff: returns events - bff --> client: return events -``` diff --git a/como_bff/src/main.rs b/como_bff/src/main.rs deleted file mode 100644 index 6813b6c..0000000 --- a/como_bff/src/main.rs +++ /dev/null @@ -1,57 +0,0 @@ -use axum::{extract::Extension, http::Method, Router}; -use sqlx::PgPool; -use std::env::{self, current_dir}; -use tower_http::{cors::CorsLayer, trace::TraceLayer}; -use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; - -#[tokio::main] -async fn main() -> anyhow::Result<()> { - // Environment - tracing::info!("Loading dotenv"); - dotenv::dotenv()?; - - // Logging - tracing_subscriber::registry() - .with(tracing_subscriber::EnvFilter::new( - std::env::var("RUST_LOG").unwrap_or_else(|_| { - "como_bin=debug,tower_http=debug,axum_extra=debug,hyper=info,mio=info,sqlx=info,async_graphql=debug" - .into() - }), - )) - .with(tracing_subscriber::fmt::layer()) - .init(); - - // Database - tracing::info!("Creating pool"); - let db_url = env::var("DATABASE_URL")?; - let pool = PgPool::connect(&db_url).await?; - - // Database Migrate - tracing::info!("Migrating db"); - sqlx::migrate!("db/migrations").run(&pool).await?; - - tracing::info!("current path: {}", current_dir()?.to_string_lossy()); - - // CORS - let cors = vec!["http://localhost:3000".parse().unwrap()]; - - // Webserver - tracing::info!("Building router"); - let app = Router::new() - .layer(TraceLayer::new_for_http()) - .layer(Extension(pool)) - .layer( - CorsLayer::new() - .allow_origin(cors) - .allow_headers([axum::http::header::CONTENT_TYPE]) - .allow_methods([Method::GET, Method::POST, Method::OPTIONS]), - ); - - tracing::info!("Starting webserver"); - axum::Server::bind(&"0.0.0.0:3002".parse().unwrap()) - .serve(app.into_make_service()) - .await - .unwrap(); - - Ok(()) -} diff --git a/como_bin/src/main.rs b/como_bin/src/main.rs index a62d7ce..1b394a9 100644 --- a/como_bin/src/main.rs +++ b/como_bin/src/main.rs @@ -13,7 +13,7 @@ use axum::{ routing::{get, post}, Json, Router, }; -use axum_extra::extract::{cookie::Key}; +use axum_extra::extract::cookie::Key; use async_graphql::{ http::{playground_source, GraphQLPlaygroundConfig}, @@ -40,17 +40,17 @@ async fn graphql_handler( session: ReadableSession, req: GraphQLRequest, ) -> Result { - let mut req = req.into_inner(); - if let Some(user_id) = session.get::("userId") { - req = req.data(user_id); - return Ok(schema.execute(req).await.into()); - } else if let Some(on) = &req.operation_name { - if on == "IntrospectionQuery" { - return Ok(schema.execute(req).await.into()); - } - } + let req = req.into_inner(); + //if let Some(user_id) = session.get::("userId") { + // req = req.data(user_id); + return Ok(schema.execute(req).await.into()); + //} else if let Some(on) = &req.operation_name { + // if on == "IntrospectionQuery" { + // return Ok(schema.execute(req).await.into()); + // } + //} - Err(StatusCode::FORBIDDEN) + //Err(StatusCode::FORBIDDEN) } async fn graphql_playground() -> impl IntoResponse { diff --git a/como_core/Cargo.toml b/como_core/Cargo.toml new file mode 100644 index 0000000..9beac69 --- /dev/null +++ b/como_core/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "como_core" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/como_core/src/lib.rs b/como_core/src/lib.rs new file mode 100644 index 0000000..139597f --- /dev/null +++ b/como_core/src/lib.rs @@ -0,0 +1,2 @@ + + diff --git a/como_domain/Cargo.toml b/como_domain/Cargo.toml new file mode 100644 index 0000000..e95d23f --- /dev/null +++ b/como_domain/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "como_domain" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0.60" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0.68" +uuid = { version = "1.1.2", features = ["v4", "fast-rng"] } diff --git a/como_domain/src/lib.rs b/como_domain/src/lib.rs new file mode 100644 index 0000000..913bd46 --- /dev/null +++ b/como_domain/src/lib.rs @@ -0,0 +1 @@ +pub mod users; diff --git a/como_domain/src/projects/mod.rs b/como_domain/src/projects/mod.rs new file mode 100644 index 0000000..e69de29 diff --git a/como_domain/src/users/mod.rs b/como_domain/src/users/mod.rs new file mode 100644 index 0000000..96d0662 --- /dev/null +++ b/como_domain/src/users/mod.rs @@ -0,0 +1,12 @@ +pub mod requests; + +use serde::{Deserialize, Serialize}; +use uuid::Uuid; + +#[derive(Serialize, Deserialize, Debug)] +pub struct UserDto { + #[serde(skip_serializing, skip_deserializing)] + pub id: Uuid, + pub username: String, + pub email: String, +} diff --git a/como_domain/src/users/requests.rs b/como_domain/src/users/requests.rs new file mode 100644 index 0000000..435df25 --- /dev/null +++ b/como_domain/src/users/requests.rs @@ -0,0 +1,8 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize)] +pub struct CreateUserDto { + pub username: String, + pub email: String, + pub password: String, +} diff --git a/como_domain/src/users/responses.rs b/como_domain/src/users/responses.rs new file mode 100644 index 0000000..e69de29 diff --git a/cuddle.yaml b/cuddle.yaml index ddd8f5f..2c1ec78 100644 --- a/cuddle.yaml +++ b/cuddle.yaml @@ -11,5 +11,9 @@ scripts: type: shell local_up: type: shell + local_down: + type: shell run_como: type: shell + migrate_como: + type: shell diff --git a/scripts/local_down.sh b/scripts/local_down.sh new file mode 100755 index 0000000..37767c0 --- /dev/null +++ b/scripts/local_down.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +set -e + +cuddle_cli render_template --template-file $TMP/docker-compose.local_up.yml.tmpl --dest $TMP/docker-compose.local_up.yml + +docker compose -f $TMP/docker-compose.local_up.yml down -v diff --git a/scripts/local_up.sh b/scripts/local_up.sh index f1038a0..811227d 100755 --- a/scripts/local_up.sh +++ b/scripts/local_up.sh @@ -4,4 +4,4 @@ set -e cuddle_cli render_template --template-file $TMP/docker-compose.local_up.yml.tmpl --dest $TMP/docker-compose.local_up.yml -docker compose -f $TMP/docker-compose.local_up.yml up -d --remove-orphans +docker compose -f $TMP/docker-compose.local_up.yml up -d --remove-orphans --build diff --git a/scripts/migrate_como.sh b/scripts/migrate_como.sh new file mode 100755 index 0000000..2a49ce8 --- /dev/null +++ b/scripts/migrate_como.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +export $(cat .env | xargs) + +cargo sqlx migrate run --source como_bin/db/migrations --database-url=$DATABASE_URL + diff --git a/templates/docker-compose.local_up.yml.tmpl b/templates/docker-compose.local_up.yml.tmpl index 3fb47ff..b5dc024 100644 --- a/templates/docker-compose.local_up.yml.tmpl +++ b/templates/docker-compose.local_up.yml.tmpl @@ -2,14 +2,16 @@ version: '3.7' services: db: - image: postgres:13.5 + build: + context: . + dockerfile: local_up.Dockerfile restart: always environment: - - POSTGRES_DB=como - - POSTGRES_USER=como - POSTGRES_PASSWORD=somenotverysecurepassword - - DATABASE_URL="postgres://como:somenotverysecurepassword@localhost:5432/como" ports: - 5432:5432 volumes: - - ./data/postgres:/var/lib/postgresql/data + - pgdata:/var/lib/postgresql/data + +volumes: + pgdata: diff --git a/templates/init-user-db.sh b/templates/init-user-db.sh new file mode 100644 index 0000000..e83e492 --- /dev/null +++ b/templates/init-user-db.sh @@ -0,0 +1,8 @@ +#!/bin/bash +set -e + +psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL + CREATE USER como WITH PASSWORD 'somenotverysecurepassword'; + CREATE DATABASE como; + GRANT ALL PRIVILEGES ON DATABASE como TO como; +EOSQL diff --git a/templates/local_up.Dockerfile b/templates/local_up.Dockerfile new file mode 100644 index 0000000..c6f6be7 --- /dev/null +++ b/templates/local_up.Dockerfile @@ -0,0 +1,3 @@ +FROM postgres:14-alpine + +COPY *.sh /docker-entrypoint-initdb.d/