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

@@ -15,8 +15,9 @@ version.workspace = true
ammonia = "4.0.0"
anyhow = "1.0.79"
async-graphql = { version = "7", features = ["log"] }
async-graphql-rocket = "7"
async-graphql-axum = "7.0.15"
async-trait = "0.1.81"
axum = "0.8.1"
build-info = "0.0.40"
cacher = { version = "0.2.0", registry = "xinu" }
chrono = "0.4.39"
@@ -24,6 +25,8 @@ clap = { version = "4.5.23", features = ["derive"] }
css-inline = "0.14.0"
futures = "0.3.31"
html-escape = "0.2.13"
letterbox-notmuch = { version = "0.12.1", path = "../notmuch", registry = "xinu" }
letterbox-shared = { version = "0.12.1", path = "../shared", registry = "xinu" }
linkify = "0.10.0"
log = "0.4.17"
lol_html = "2.0.0"
@@ -32,8 +35,6 @@ maplit = "1.0.2"
memmap = "0.7.0"
regex = "1.11.1"
reqwest = { version = "0.12.7", features = ["blocking"] }
rocket = { version = "0.5.0-rc.2", features = ["json"] }
rocket_cors = "0.6.0"
scraper = "0.23.0"
serde = { version = "1.0.147", features = ["derive"] }
serde_json = "1.0.87"
@@ -41,14 +42,13 @@ sqlx = { version = "0.8.2", features = ["postgres", "runtime-tokio", "time"] }
tantivy = { version = "0.24.0", optional = true }
thiserror = "2.0.0"
tokio = "1.26.0"
tower-http = { version = "0.6.2", features = ["trace"] }
tracing = "0.1.41"
url = "2.5.2"
urlencoding = "2.1.3"
#xtracing = { path = "../../xtracing" }
#xtracing = { git = "http://git-private.h.xinu.tv/wathiede/xtracing.git" }
#xtracing = { path = "../../xtracing" }
xtracing = { version = "0.3.0", registry = "xinu" }
letterbox-notmuch = { version = "0.12.1", path = "../notmuch", registry = "xinu" }
letterbox-shared = { version = "0.12.1", path = "../shared", registry = "xinu" }
[build-dependencies]
build-info-build = "0.0.40"

View File

@@ -5,7 +5,6 @@ newsreader_database_url = "postgres://newsreader@nixos-07.h.xinu.tv/newsreader"
newsreader_tantivy_db_path = "../target/database/newsreader"
[debug]
address = "0.0.0.0"
port = 9345
# Uncomment to make it production like.
#log_level = "critical"

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>;