2022-10-04 11:06:48 +02:00
|
|
|
use anyhow::Context;
|
|
|
|
use axum::{
|
|
|
|
http::{HeaderValue, Method},
|
2023-05-27 13:12:29 +02:00
|
|
|
response::IntoResponse,
|
2022-10-04 11:06:48 +02:00
|
|
|
Router,
|
|
|
|
};
|
|
|
|
use como_infrastructure::register::ServiceRegister;
|
|
|
|
use tower::ServiceBuilder;
|
|
|
|
use tower_http::{cors::CorsLayer, trace::TraceLayer};
|
2023-05-27 13:12:29 +02:00
|
|
|
use zitadel::axum::introspection::IntrospectedUser;
|
2022-10-04 11:06:48 +02:00
|
|
|
|
2023-05-27 13:12:29 +02:00
|
|
|
use crate::controllers::auth::AuthController;
|
2022-10-04 11:06:48 +02:00
|
|
|
use crate::controllers::graphql::GraphQLController;
|
|
|
|
|
2023-05-27 13:12:29 +02:00
|
|
|
async fn authed(user: IntrospectedUser) -> impl IntoResponse {
|
|
|
|
format!(
|
|
|
|
"Hello authorized user: {:?} with id {}",
|
|
|
|
user.username, user.user_id
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-10-04 11:06:48 +02:00
|
|
|
pub struct Api;
|
|
|
|
|
|
|
|
impl Api {
|
|
|
|
pub async fn new(
|
|
|
|
port: u32,
|
|
|
|
cors_origin: &str,
|
|
|
|
service_register: ServiceRegister,
|
|
|
|
) -> anyhow::Result<()> {
|
|
|
|
let router = Router::new()
|
2023-05-27 13:12:29 +02:00
|
|
|
.nest(
|
|
|
|
"/auth",
|
|
|
|
AuthController::new_router(service_register.clone()).await?,
|
|
|
|
)
|
2022-10-04 11:06:48 +02:00
|
|
|
.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]),
|
|
|
|
);
|
|
|
|
|
2023-05-27 13:12:29 +02:00
|
|
|
tracing::info!("running on: 0.0.0.0:{}", port);
|
|
|
|
|
2022-10-04 11:06:48 +02:00
|
|
|
axum::Server::bind(&format!("0.0.0.0:{}", port).parse().unwrap())
|
|
|
|
.serve(router.into_make_service())
|
|
|
|
.await
|
|
|
|
.context("error while starting API")?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|