2022-08-15 22:55:04 +02:00
|
|
|
use std::env::{self, current_dir};
|
2022-08-09 01:33:34 +02:00
|
|
|
|
2022-08-20 23:55:24 +02:00
|
|
|
mod graphql;
|
|
|
|
|
2022-08-09 01:33:34 +02:00
|
|
|
use axum::{
|
2022-08-20 23:55:24 +02:00
|
|
|
extract::Extension,
|
|
|
|
http::Method,
|
|
|
|
response::{Html, IntoResponse},
|
|
|
|
routing::get,
|
|
|
|
Json, Router,
|
|
|
|
};
|
|
|
|
|
|
|
|
use async_graphql::{
|
|
|
|
http::{playground_source, GraphQLPlaygroundConfig},
|
|
|
|
EmptyMutation, EmptySubscription, Request, Response, Schema,
|
2022-08-09 01:33:34 +02:00
|
|
|
};
|
2022-08-20 23:55:24 +02:00
|
|
|
use graphql::CibusSchema;
|
2022-08-09 01:33:34 +02:00
|
|
|
use sqlx::PgPool;
|
2022-08-20 23:55:24 +02:00
|
|
|
use tower_http::{cors::CorsLayer, trace::TraceLayer};
|
2022-08-15 22:55:04 +02:00
|
|
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
2022-08-09 01:33:34 +02:00
|
|
|
|
2022-08-20 23:55:24 +02:00
|
|
|
use crate::graphql::QueryRoot;
|
|
|
|
|
|
|
|
async fn graphql_handler(schema: Extension<CibusSchema>, req: Json<Request>) -> Json<Response> {
|
|
|
|
schema.execute(req.0).await.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn graphql_playground() -> impl IntoResponse {
|
|
|
|
Html(playground_source(GraphQLPlaygroundConfig::new("/")))
|
|
|
|
}
|
|
|
|
|
2022-08-09 01:33:34 +02:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> anyhow::Result<()> {
|
|
|
|
// Environment
|
2022-08-15 22:55:04 +02:00
|
|
|
tracing::info!("Loading dotenv");
|
2022-08-09 01:33:34 +02:00
|
|
|
dotenv::dotenv()?;
|
|
|
|
|
2022-08-15 22:55:04 +02:00
|
|
|
// Logging
|
|
|
|
tracing_subscriber::registry()
|
|
|
|
.with(tracing_subscriber::EnvFilter::new(
|
|
|
|
std::env::var("RUST_LOG").unwrap_or_else(|_| {
|
2024-04-05 21:00:43 +02:00
|
|
|
"cibus-backend=debug,tower_http=debug,axum_extra=debug,hyper=info,mio=info".into()
|
2022-08-15 22:55:04 +02:00
|
|
|
}),
|
|
|
|
))
|
|
|
|
.with(tracing_subscriber::fmt::layer())
|
|
|
|
.init();
|
|
|
|
|
2022-08-09 01:33:34 +02:00
|
|
|
// Database
|
2022-08-15 22:55:04 +02:00
|
|
|
tracing::info!("Creating pool");
|
|
|
|
let db_url = env::var("DATABASE_URL")?;
|
|
|
|
let pool = PgPool::connect(&db_url).await?;
|
2022-08-09 01:33:34 +02:00
|
|
|
|
|
|
|
// Database Migrate
|
2022-08-15 22:55:04 +02:00
|
|
|
tracing::info!("Migrating db");
|
2022-08-09 01:33:34 +02:00
|
|
|
sqlx::migrate!("db/migrations").run(&pool).await?;
|
|
|
|
|
2022-08-15 22:55:04 +02:00
|
|
|
tracing::info!("current path: {}", current_dir()?.to_string_lossy());
|
2022-08-09 01:33:34 +02:00
|
|
|
|
2022-08-20 23:55:24 +02:00
|
|
|
// Schema
|
|
|
|
println!("Building schema");
|
|
|
|
let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription).finish();
|
|
|
|
|
|
|
|
// CORS
|
|
|
|
let cors = vec!["http://localhost:3000".parse().unwrap()];
|
|
|
|
|
2022-08-15 22:55:04 +02:00
|
|
|
// Webserver
|
|
|
|
tracing::info!("Building router");
|
2022-08-09 01:33:34 +02:00
|
|
|
let app = Router::new()
|
2022-08-20 23:55:24 +02:00
|
|
|
.route("/", get(graphql_playground).post(graphql_handler))
|
|
|
|
.layer(Extension(schema))
|
|
|
|
.layer(TraceLayer::new_for_http())
|
|
|
|
.layer(
|
|
|
|
CorsLayer::new()
|
|
|
|
.allow_origin(cors)
|
|
|
|
.allow_headers([axum::http::header::CONTENT_TYPE])
|
|
|
|
.allow_methods([Method::GET, Method::POST, Method::OPTIONS]),
|
|
|
|
);
|
2022-08-09 01:33:34 +02:00
|
|
|
|
2022-08-15 22:55:04 +02:00
|
|
|
tracing::info!("Starting webserver");
|
2022-08-20 23:55:24 +02:00
|
|
|
axum::Server::bind(&"0.0.0.0:3001".parse().unwrap())
|
2022-08-09 01:33:34 +02:00
|
|
|
.serve(app.into_make_service())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|