-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
use std::{ | ||
future::{ | ||
Future, | ||
IntoFuture, | ||
}, | ||
marker::PhantomData, | ||
net::{ | ||
TcpListener, | ||
ToSocketAddrs, | ||
}, | ||
pin::Pin, | ||
}; | ||
|
||
use async_graphql::{ | ||
http::GraphiQLSource, | ||
EmptyMutation, | ||
EmptySubscription, | ||
Request, | ||
Response, | ||
}; | ||
use axum::{ | ||
response::{ | ||
ErrorResponse, | ||
Html, | ||
IntoResponse, | ||
}, | ||
routing, | ||
Extension, | ||
Json, | ||
Router, | ||
}; | ||
use fuel_core_services::{ | ||
RunnableService, | ||
RunnableTask, | ||
ServiceRunner, | ||
StateWatcher, | ||
TaskNextAction, | ||
}; | ||
|
||
use crate::{ | ||
ports::GetStateRoot, | ||
schema::{ | ||
Query, | ||
Schema, | ||
}, | ||
}; | ||
|
||
pub fn new_service<Storage, Addr>( | ||
storage: Storage, | ||
network_address: Addr, | ||
) -> anyhow::Result<ServiceRunner<StateRootApiService>> | ||
where | ||
Storage: GetStateRoot + Send + Sync + 'static, | ||
Addr: ToSocketAddrs, | ||
{ | ||
Ok(ServiceRunner::new(StateRootApiService::new( | ||
storage, | ||
network_address, | ||
)?)) | ||
} | ||
|
||
pub struct StateRootApiService { | ||
router: Router<hyper::Body>, | ||
listener: TcpListener, | ||
} | ||
|
||
impl StateRootApiService { | ||
#[tracing::instrument(skip(storage, network_address))] | ||
fn new<Storage, Addr>(storage: Storage, network_address: Addr) -> anyhow::Result<Self> | ||
where | ||
Storage: GetStateRoot + Send + Sync + 'static, | ||
Addr: ToSocketAddrs, | ||
{ | ||
let graphql_endpoint = "/graphql"; | ||
|
||
let graphql_playground = || render_graphql_playground(graphql_endpoint); | ||
|
||
let query = Query::new(storage); | ||
let schema = Schema::build(query, EmptyMutation, EmptySubscription).finish(); | ||
|
||
let router = Router::<hyper::Body>::new() | ||
.route("/playground", routing::get(graphql_playground)) | ||
.route(graphql_endpoint, routing::post(graphql_handler::<Storage>)) | ||
.layer(Extension(schema)); | ||
|
||
let listener = TcpListener::bind(network_address)?; | ||
|
||
Ok(Self { router, listener }) | ||
} | ||
} | ||
|
||
async fn render_graphql_playground(graphql_endpoint: &str) -> impl IntoResponse { | ||
Html( | ||
GraphiQLSource::build() | ||
.endpoint(graphql_endpoint) | ||
.title("Fuel Graphql Playground") | ||
.finish(), | ||
) | ||
} | ||
|
||
async fn graphql_handler<Storage>( | ||
schema: Extension<Schema<Storage>>, | ||
request: Json<Request>, | ||
) -> Json<Response> | ||
where | ||
Storage: GetStateRoot + Send + Sync + 'static, | ||
{ | ||
schema.execute(request.0).await.into() | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl RunnableService for StateRootApiService { | ||
const NAME: &'static str = "StateRootGraphQL"; | ||
|
||
type SharedData = (); | ||
type Task = StateRootApiTask; | ||
type TaskParams = (); | ||
|
||
fn shared_data(&self) -> Self::SharedData {} | ||
|
||
#[tracing::instrument(skip(self, state, _params))] | ||
async fn into_task( | ||
self, | ||
state: &StateWatcher, | ||
_params: Self::TaskParams, | ||
) -> anyhow::Result<Self::Task> { | ||
let mut state = state.clone(); | ||
|
||
let graceful_shutdown_signal = async move { | ||
state.while_started().await.expect("unexpected termination"); | ||
}; | ||
|
||
let bound_address = self.listener.local_addr()?; | ||
tracing::info!(%bound_address, "listening for GraphQL requests"); | ||
|
||
let server = Box::pin( | ||
axum::Server::from_tcp(self.listener)? | ||
.serve(self.router.into_make_service()) | ||
.with_graceful_shutdown(graceful_shutdown_signal), | ||
); | ||
|
||
Ok(StateRootApiTask { server }) | ||
} | ||
} | ||
|
||
pub struct StateRootApiTask { | ||
server: Pin<Box<dyn Future<Output = hyper::Result<()>> + Send + 'static>>, | ||
} | ||
|
||
impl RunnableTask for StateRootApiTask { | ||
async fn run( | ||
&mut self, | ||
watcher: &mut fuel_core_services::StateWatcher, | ||
) -> TaskNextAction { | ||
match self.server.as_mut().await { | ||
Ok(()) => { | ||
// The `axum::Server` has stopped, and so should we | ||
TaskNextAction::Stop | ||
} | ||
Err(error) => { | ||
tracing::error!(%error, "state root axum server returned error"); | ||
TaskNextAction::Stop | ||
} | ||
} | ||
} | ||
|
||
async fn shutdown(self) -> anyhow::Result<()> { | ||
Ok(()) | ||
} | ||
} |