remove bff

This commit is contained in:
Kasper Juul Hermansen 2022-10-03 22:08:25 +02:00
parent d88abefa9a
commit 8c51b68523
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
21 changed files with 105 additions and 138 deletions

31
Cargo.lock generated
View File

@ -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"

View File

@ -1,2 +1,2 @@
[workspace]
members = ["como_bin", "como_bff"]
members = ["como_bin", "como_core", "como_domain"]

View File

@ -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"] }

View File

@ -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
```

View File

@ -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(())
}

View File

@ -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<GraphQLResponse, StatusCode> {
let mut req = req.into_inner();
if let Some(user_id) = session.get::<String>("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::<String>("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 {

8
como_core/Cargo.toml Normal file
View File

@ -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]

2
como_core/src/lib.rs Normal file
View File

@ -0,0 +1,2 @@

12
como_domain/Cargo.toml Normal file
View File

@ -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"] }

1
como_domain/src/lib.rs Normal file
View File

@ -0,0 +1 @@
pub mod users;

View File

View File

@ -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,
}

View File

@ -0,0 +1,8 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct CreateUserDto {
pub username: String,
pub email: String,
pub password: String,
}

View File

View File

@ -11,5 +11,9 @@ scripts:
type: shell
local_up:
type: shell
local_down:
type: shell
run_como:
type: shell
migrate_como:
type: shell

7
scripts/local_down.sh Executable file
View File

@ -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

View File

@ -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

6
scripts/migrate_como.sh Executable file
View File

@ -0,0 +1,6 @@
#!/bin/bash
export $(cat .env | xargs)
cargo sqlx migrate run --source como_bin/db/migrations --database-url=$DATABASE_URL

View File

@ -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:

View File

@ -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

View File

@ -0,0 +1,3 @@
FROM postgres:14-alpine
COPY *.sh /docker-entrypoint-initdb.d/