56 lines
1.3 KiB
Rust
56 lines
1.3 KiB
Rust
|
extern crate log;
|
||
|
|
||
|
use anyhow::Result;
|
||
|
use async_graphql::{http::GraphiQLSource, EmptySubscription, Schema};
|
||
|
use async_graphql_axum::GraphQL;
|
||
|
use axum::{
|
||
|
response::{self, IntoResponse},
|
||
|
routing::get,
|
||
|
Router,
|
||
|
};
|
||
|
use dotenv::dotenv;
|
||
|
use mutations::Mutation;
|
||
|
use queries::Query;
|
||
|
use sqlx::postgres::PgPool;
|
||
|
use std::env;
|
||
|
use tokio::net::TcpListener;
|
||
|
|
||
|
mod models;
|
||
|
mod mutations;
|
||
|
mod queries;
|
||
|
|
||
|
async fn ping() -> String {
|
||
|
format!(
|
||
|
"I am healthy: {} v{}",
|
||
|
env!("CARGO_PKG_DESCRIPTION"),
|
||
|
env!("CARGO_PKG_VERSION")
|
||
|
)
|
||
|
}
|
||
|
|
||
|
async fn graphiql() -> impl IntoResponse {
|
||
|
response::Html(GraphiQLSource::build().endpoint("/").finish())
|
||
|
}
|
||
|
|
||
|
#[tokio::main]
|
||
|
async fn main() -> Result<()> {
|
||
|
dotenv().ok();
|
||
|
env_logger::init();
|
||
|
|
||
|
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL is not set");
|
||
|
let db_pool = PgPool::connect(&database_url).await?;
|
||
|
|
||
|
let schema = Schema::build(Query::default(), Mutation::default(), EmptySubscription)
|
||
|
.data(db_pool)
|
||
|
.finish();
|
||
|
|
||
|
let app = Router::new()
|
||
|
.route("/ping", get(ping))
|
||
|
.route("/", get(graphiql).post_service(GraphQL::new(schema)));
|
||
|
|
||
|
println!("GraphiQL IDE: http://localhost:8000");
|
||
|
|
||
|
axum::serve(TcpListener::bind("127.0.0.1:8000").await?, app).await?;
|
||
|
|
||
|
Ok(())
|
||
|
}
|