This commit is contained in:
2022-10-02 20:51:06 +02:00
parent 71bdea4001
commit b20d3c418c
13 changed files with 821 additions and 61 deletions

25
como_bff/Cargo.toml Normal file
View File

@@ -0,0 +1,25 @@
[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"] }

21
como_bff/README.md Normal file
View File

@@ -0,0 +1,21 @@
# 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
```

82
como_bff/src/main.rs Normal file
View File

@@ -0,0 +1,82 @@
use std::env::{self, current_dir};
use async_graphql_axum::{GraphQLRequest, GraphQLResponse};
use axum::{
extract::Extension,
http::{Method, StatusCode},
response::{Html, IntoResponse},
routing::{get, post},
Json, Router,
};
use axum_extra::extract::{cookie::Key, PrivateCookieJar};
use async_graphql::{
http::{playground_source, GraphQLPlaygroundConfig},
EmptySubscription, Request, Response, Schema,
};
use axum_sessions::{
async_session::MemoryStore,
extractors::{ReadableSession, WritableSession},
SessionLayer,
};
use error::AppError;
use graphql::CibusSchema;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use services::users_service;
use sqlx::PgPool;
use tower_http::{cors::CorsLayer, trace::TraceLayer};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use crate::graphql::{MutationRoot, QueryRoot};
#[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(())
}