Try using axum instead of rocket. WS doesn't seem to work through trunk

This commit is contained in:
2025-04-14 10:37:51 -07:00
parent d7217d1b3c
commit b2c73ffa15
6 changed files with 210 additions and 425 deletions

View File

@@ -1,12 +1,15 @@
// Rocket generates a lot of warnings for handlers
// TODO: figure out why
#![allow(unreachable_patterns)]
#[macro_use]
extern crate rocket;
use std::{error::Error, io::Cursor, str::FromStr};
use async_graphql::{extensions, http::GraphiQLSource, EmptySubscription, Schema};
use async_graphql_rocket::{GraphQLQuery, GraphQLRequest, GraphQLResponse};
use async_graphql::{extensions, http::GraphiQLSource, Schema};
use async_graphql_axum::{GraphQL, GraphQLSubscription};
use axum::{
response::{self, IntoResponse},
routing::get,
Router,
};
use cacher::FilesystemCacher;
use letterbox_notmuch::{Notmuch, NotmuchError, ThreadSet};
#[cfg(feature = "tantivy")]
@@ -14,20 +17,14 @@ use letterbox_server::tantivy::TantivyConnection;
use letterbox_server::{
config::Config,
error::ServerError,
graphql::{Attachment, GraphqlSchema, Mutation, QueryRoot, Subscription},
graphql::{Attachment, GraphqlSchema, MutationRoot, QueryRoot, SubscriptionRoot},
nm::{attachment_bytes, cid_attachment_bytes},
};
use rocket::{
fairing::AdHoc,
http::{ContentType, Header},
request::Request,
response::{content, Debug, Responder},
serde::json::Json,
Response, State,
};
use rocket_cors::{AllowedHeaders, AllowedOrigins};
use sqlx::postgres::PgPool;
use tokio::net::TcpListener;
use tower_http::trace::TraceLayer;
/*
#[get("/show/<query>/pretty")]
async fn show_pretty(
nm: &State<Notmuch>,
@@ -243,3 +240,41 @@ async fn main() -> Result<(), Box<dyn Error>> {
rkt.launch().await?;
Ok(())
}
*/
async fn graphiql() -> impl IntoResponse {
response::Html(
GraphiQLSource::build()
.endpoint("/api/")
.subscription_endpoint("/api/ws")
.finish(),
)
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let _guard = xtracing::init(env!("CARGO_BIN_NAME"))?;
let schema = Schema::build(QueryRoot, MutationRoot, SubscriptionRoot)
//.data(Storage::default())
.finish();
let app = Router::new()
.route(
"/api/",
get(graphiql).post_service(GraphQL::new(schema.clone())),
)
.route_service("/api/ws", GraphQLSubscription::new(schema))
.layer(
TraceLayer::new_for_http()
.on_request(tower_http::trace::DefaultOnRequest::new().level(tracing::Level::INFO))
.on_response(
tower_http::trace::DefaultOnResponse::new().level(tracing::Level::INFO),
)
.on_failure(tower_http::trace::DefaultOnFailure::new().level(tracing::Level::WARN)),
);
axum::serve(TcpListener::bind("0.0.0.0:9345").await.unwrap(), app)
.await
.unwrap();
Ok(())
}

View File

@@ -3,7 +3,8 @@ use std::{fmt, str::FromStr};
use async_graphql::{
connection::{self, Connection, Edge, OpaqueCursor},
futures_util::{Stream, StreamExt},
Context, Enum, Error, FieldResult, InputObject, Object, Schema, SimpleObject, Union,
Context, Enum, Error, FieldResult, InputObject, Object, Schema, SimpleObject, Subscription,
Union,
};
use cacher::FilesystemCacher;
use futures::stream;
@@ -594,9 +595,9 @@ async fn tantivy_search(
.collect())
}
pub struct Mutation;
pub struct MutationRoot;
#[Object]
impl Mutation {
impl MutationRoot {
#[instrument(skip_all, fields(query=query, unread=unread, rid=request_id()))]
async fn set_read_status<'ctx>(
&self,
@@ -648,6 +649,7 @@ impl Mutation {
tantivy.drop_and_load_index()?;
tantivy.reindex_all(pool).await?;
println("hit");
Ok(true)
}
@@ -668,12 +670,12 @@ impl Mutation {
}
}
pub struct Subscription;
#[async_graphql::Subscription]
impl Subscription {
pub struct SubscriptionRoot;
#[Subscription]
impl SubscriptionRoot {
async fn values(&self, ctx: &Context<'_>) -> Result<impl Stream<Item = usize>, Error> {
Ok(stream::iter(0..10))
}
}
pub type GraphqlSchema = Schema<QueryRoot, Mutation, Subscription>;
pub type GraphqlSchema = Schema<QueryRoot, MutationRoot, SubscriptionRoot>;