with push_release
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-08-20 23:55:24 +02:00
parent 39b68e0dde
commit 908ead3887
12 changed files with 111 additions and 1289 deletions

View File

@@ -1,17 +1,34 @@
use std::env::{self, current_dir};
use askama::Template;
mod graphql;
use axum::{
http::StatusCode,
response::{Html, IntoResponse, Response},
routing::{get},
Router,
extract::Extension,
http::Method,
response::{Html, IntoResponse},
routing::get,
Json, Router,
};
use axum_extra::routing::SpaRouter;
use async_graphql::{
http::{playground_source, GraphQLPlaygroundConfig},
EmptyMutation, EmptySubscription, Request, Response, Schema,
};
use graphql::CibusSchema;
use sqlx::PgPool;
use tower_http::{trace::TraceLayer};
use tower_http::{cors::CorsLayer, trace::TraceLayer};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
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("/")))
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Environment
@@ -39,48 +56,31 @@ async fn main() -> anyhow::Result<()> {
tracing::info!("current path: {}", current_dir()?.to_string_lossy());
// Schema
println!("Building schema");
let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription).finish();
// CORS
let cors = vec!["http://localhost:3000".parse().unwrap()];
// Webserver
tracing::info!("Building router");
let app = Router::new()
.route("/", get(index_handler))
.merge(SpaRouter::new("/assets", "assets"))
.layer(TraceLayer::new_for_http());
.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]),
);
tracing::info!("Starting webserver");
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
axum::Server::bind(&"0.0.0.0:3001".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
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(),
})
}