Added apis

This commit is contained in:
2022-10-04 11:06:48 +02:00
parent 6234cf18e8
commit ae74f66c3a
28 changed files with 392 additions and 300 deletions

39
como_api/Cargo.toml Normal file
View File

@@ -0,0 +1,39 @@
[package]
name = "como_api"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
como_gql = { path = "../como_gql" }
como_core = { path = "../como_core" }
como_domain = { path = "../como_domain" }
como_infrastructure = { path = "../como_infrastructure" }
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"] }
argon2 = "0.4"
rand_core = { version = "0.6", features = ["std"] }
cookie = { version = "0.16", features = ["secure", "percent-encode"] }
tower = { version = "0.4", features = ["timeout"] }
tower-http = { version = "0.3", features = ["trace", "cors"] }

View File

@@ -0,0 +1,11 @@
use axum::{routing::get, Router};
use como_gql::{graphql_handler, graphql_playground};
use como_infrastructure::register::ServiceRegister;
pub struct GraphQLController;
impl GraphQLController {
pub fn new_router(_service_register: ServiceRegister) -> Router {
Router::new().route("/", get(graphql_playground).post(graphql_handler))
}
}

View File

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

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

@@ -0,0 +1,2 @@
mod controllers;
pub mod router;

44
como_api/src/router.rs Normal file
View File

@@ -0,0 +1,44 @@
use anyhow::Context;
use axum::{
http::{HeaderValue, Method},
Router,
};
use como_infrastructure::register::ServiceRegister;
use tower::ServiceBuilder;
use tower_http::{cors::CorsLayer, trace::TraceLayer};
use crate::controllers::graphql::GraphQLController;
pub struct Api;
impl Api {
pub async fn new(
port: u32,
cors_origin: &str,
service_register: ServiceRegister,
) -> anyhow::Result<()> {
let router = Router::new()
.nest(
"/graphql",
GraphQLController::new_router(service_register.clone()),
)
.layer(ServiceBuilder::new().layer(TraceLayer::new_for_http()))
.layer(
CorsLayer::new()
.allow_origin(
cors_origin
.parse::<HeaderValue>()
.context("could not parse cors origin as header")?,
)
.allow_headers([axum::http::header::CONTENT_TYPE])
.allow_methods([Method::GET, Method::POST, Method::OPTIONS]),
);
axum::Server::bind(&format!("0.0.0.0:{}", port).parse().unwrap())
.serve(router.into_make_service())
.await
.context("error while starting API")?;
Ok(())
}
}