2023-11-13 23:21:57 +00:00
|
|
|
pub mod schema;
|
2023-11-09 18:51:07 +00:00
|
|
|
mod ui;
|
2023-11-12 22:52:28 +00:00
|
|
|
mod webserver;
|
2023-11-09 18:51:07 +00:00
|
|
|
mod worker;
|
|
|
|
|
|
2023-11-12 22:52:28 +00:00
|
|
|
use std::sync::Arc;
|
2023-11-09 18:51:07 +00:00
|
|
|
|
|
|
|
|
use axum::{
|
|
|
|
|
extract::State,
|
|
|
|
|
http::Request,
|
|
|
|
|
middleware::{from_fn_with_state, Next},
|
2023-11-12 22:52:28 +00:00
|
|
|
routing, Extension, Router,
|
2023-11-09 18:51:07 +00:00
|
|
|
};
|
2023-11-12 22:52:28 +00:00
|
|
|
use hyper::Body;
|
|
|
|
|
use juniper::EmptySubscription;
|
|
|
|
|
use juniper_axum::{graphiql, graphql, playground};
|
|
|
|
|
use schema::{Mutation, Query, Schema};
|
|
|
|
|
use webserver::Webserver;
|
2023-11-09 18:51:07 +00:00
|
|
|
|
|
|
|
|
pub fn attach_webserver(router: Router) -> Router {
|
|
|
|
|
let ws = Arc::new(Webserver::default());
|
2023-11-12 22:52:28 +00:00
|
|
|
let schema = Arc::new(Schema::new(Query, Mutation, EmptySubscription::new()));
|
|
|
|
|
|
|
|
|
|
let app = Router::new()
|
|
|
|
|
.route("/graphql", routing::get(playground("/graphql", None)))
|
|
|
|
|
.route("/graphiql", routing::get(graphiql("/graphql", None)))
|
|
|
|
|
.route(
|
|
|
|
|
"/graphql",
|
|
|
|
|
routing::post(graphql::<Arc<Schema>, Arc<Webserver>>).with_state(ws.clone()),
|
|
|
|
|
)
|
|
|
|
|
.layer(Extension(schema));
|
2023-11-09 18:51:07 +00:00
|
|
|
|
|
|
|
|
router
|
2023-11-12 22:52:28 +00:00
|
|
|
.merge(app)
|
2023-11-09 18:51:07 +00:00
|
|
|
.fallback(ui::handler)
|
|
|
|
|
.layer(from_fn_with_state(ws, distributed_tabby_layer))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn distributed_tabby_layer(
|
|
|
|
|
State(ws): State<Arc<Webserver>>,
|
|
|
|
|
request: Request<Body>,
|
|
|
|
|
next: Next<Body>,
|
|
|
|
|
) -> axum::response::Response {
|
|
|
|
|
ws.dispatch_request(request, next).await
|
|
|
|
|
}
|