57 lines
1.4 KiB
Rust
57 lines
1.4 KiB
Rust
mod graphql;
|
|
|
|
use std::env;
|
|
|
|
use async_graphql::{
|
|
http::{playground_source, GraphQLPlaygroundConfig},
|
|
EmptyMutation, EmptySubscription, Request, Response, Schema,
|
|
};
|
|
use axum::{
|
|
extract::Extension,
|
|
response::{Html, IntoResponse},
|
|
routing::get,
|
|
Json, Router,
|
|
};
|
|
use graphql::{CibusSchema, QueryRoot};
|
|
use sqlx::PgPool;
|
|
|
|
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("/")))
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() -> anyhow::Result<()> {
|
|
// Environment
|
|
println!("Loading dotenv");
|
|
dotenv::dotenv()?;
|
|
|
|
// Database
|
|
println!("Creating pool");
|
|
let pool = PgPool::connect(&env::var("DATABASE_URL")?).await?;
|
|
|
|
// Database Migrate
|
|
println!("Migrating db");
|
|
sqlx::migrate!("db/migrations").run(&pool).await?;
|
|
|
|
// Webserver
|
|
println!("Building schema");
|
|
let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription).finish();
|
|
|
|
println!("Building router");
|
|
let app = Router::new()
|
|
.route("/", get(graphql_playground).post(graphql_handler))
|
|
.layer(Extension(schema));
|
|
|
|
println!("Starting webserver");
|
|
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
|
|
.serve(app.into_make_service())
|
|
.await
|
|
.unwrap();
|
|
|
|
Ok(())
|
|
}
|