This commit is contained in:
@@ -1,52 +1,52 @@
|
||||
mod graphql;
|
||||
use std::env::{self, current_dir};
|
||||
|
||||
use std::env;
|
||||
|
||||
use async_graphql::{
|
||||
http::{playground_source, GraphQLPlaygroundConfig},
|
||||
EmptyMutation, EmptySubscription, Request, Response, Schema,
|
||||
};
|
||||
use askama::Template;
|
||||
use axum::{
|
||||
extract::Extension,
|
||||
response::{Html, IntoResponse},
|
||||
routing::get,
|
||||
Json, Router,
|
||||
http::StatusCode,
|
||||
response::{Html, IntoResponse, Response},
|
||||
routing::{get, get_service},
|
||||
Router,
|
||||
};
|
||||
use graphql::{CibusSchema, QueryRoot};
|
||||
use axum_extra::routing::SpaRouter;
|
||||
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("/")))
|
||||
}
|
||||
use tower_http::{services::ServeDir, trace::TraceLayer};
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
// Environment
|
||||
println!("Loading dotenv");
|
||||
tracing::info!("Loading dotenv");
|
||||
dotenv::dotenv()?;
|
||||
|
||||
// Logging
|
||||
tracing_subscriber::registry()
|
||||
.with(tracing_subscriber::EnvFilter::new(
|
||||
std::env::var("RUST_LOG").unwrap_or_else(|_| {
|
||||
"cibus_bin=debug,tower_http=debug,axum_extra=debug,hyper=info,mio=info".into()
|
||||
}),
|
||||
))
|
||||
.with(tracing_subscriber::fmt::layer())
|
||||
.init();
|
||||
|
||||
// Database
|
||||
println!("Creating pool");
|
||||
let pool = PgPool::connect(&env::var("DATABASE_URL")?).await?;
|
||||
tracing::info!("Creating pool");
|
||||
let db_url = env::var("DATABASE_URL")?;
|
||||
let pool = PgPool::connect(&db_url).await?;
|
||||
|
||||
// Database Migrate
|
||||
println!("Migrating db");
|
||||
tracing::info!("Migrating db");
|
||||
sqlx::migrate!("db/migrations").run(&pool).await?;
|
||||
|
||||
tracing::info!("current path: {}", current_dir()?.to_string_lossy());
|
||||
|
||||
// Webserver
|
||||
println!("Building schema");
|
||||
let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription).finish();
|
||||
|
||||
println!("Building router");
|
||||
tracing::info!("Building router");
|
||||
let app = Router::new()
|
||||
.route("/", get(graphql_playground).post(graphql_handler))
|
||||
.layer(Extension(schema));
|
||||
.route("/", get(index_handler))
|
||||
.merge(SpaRouter::new("/assets", "assets"))
|
||||
.layer(TraceLayer::new_for_http());
|
||||
|
||||
println!("Starting webserver");
|
||||
tracing::info!("Starting webserver");
|
||||
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
|
||||
.serve(app.into_make_service())
|
||||
.await
|
||||
@@ -54,3 +54,33 @@ async fn main() -> anyhow::Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
struct HtmlTemplate<T>(T);
|
||||
|
||||
impl<T> IntoResponse for HtmlTemplate<T>
|
||||
where
|
||||
T: Template,
|
||||
{
|
||||
fn into_response(self) -> Response {
|
||||
match self.0.render() {
|
||||
Ok(html) => Html(html).into_response(),
|
||||
Err(err) => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Failed to render template. Error: {}", err),
|
||||
)
|
||||
.into_response(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "index.html")]
|
||||
struct IndexTemplate {
|
||||
name: String,
|
||||
}
|
||||
|
||||
async fn index_handler() -> impl IntoResponse {
|
||||
HtmlTemplate(IndexTemplate {
|
||||
name: "Cibus".into(),
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user