44//
55// Copyright (c) DUSK NETWORK. All rights reserved.
66
7+ use std:: time:: Duration ;
8+
79use tokio:: sync:: broadcast;
810use tracing:: info;
911
10- use crate :: http:: { DataSources , HttpServer , HttpServerConfig } ;
12+ use crate :: http:: { DataSources , HandleRequest , HttpServer , HttpServerConfig } ;
1113
12- #[ derive( Default ) ]
1314pub struct RuskHttpBuilder {
1415 http : Option < HttpServerConfig > ,
16+ data_sources : DataSources ,
17+ shutdown_timeout : Duration ,
1518}
1619
1720impl RuskHttpBuilder {
21+ pub fn new ( ) -> Self {
22+ Self {
23+ http : None ,
24+ data_sources : DataSources :: default ( ) ,
25+ shutdown_timeout : Duration :: from_secs ( 30 ) ,
26+ }
27+ }
28+
1829 pub fn with_http ( mut self , http : HttpServerConfig ) -> Self {
1930 self . http = Some ( http) ;
2031 self
2132 }
2233
23- pub async fn build_and_run ( self ) -> anyhow:: Result < ( ) > {
34+ pub fn with_data_source ( mut self , source : Box < dyn HandleRequest > ) -> Self {
35+ self . data_sources . sources . push ( source) ;
36+ self
37+ }
38+
39+ pub fn with_shutdown_timeout ( mut self , timeout : Duration ) -> Self {
40+ self . shutdown_timeout = timeout;
41+ self
42+ }
43+
44+ pub async fn build ( self ) -> anyhow:: Result < RuskHttp > {
2445 let ( _rues_sender, rues_receiver) = broadcast:: channel ( 1 ) ;
2546
26- let mut _ws_server = None ;
47+ let mut server = None ;
2748 if let Some ( http) = self . http {
2849 info ! ( "Configuring HTTP" ) ;
2950
3051 #[ allow( unused_mut) ]
31- let mut handler = DataSources :: default ( ) ;
52+ let mut handler = self . data_sources ;
3253
3354 #[ cfg( feature = "prover" ) ]
3455 handler. sources . push ( Box :: new ( rusk_prover:: LocalProver ) ) ;
@@ -38,7 +59,7 @@ impl RuskHttpBuilder {
3859 _ => None ,
3960 } ;
4061
41- let ( server , _) = HttpServer :: bind (
62+ let ( http_server , _) = HttpServer :: bind (
4263 handler,
4364 rues_receiver,
4465 http. ws_event_channel_cap ,
@@ -48,13 +69,51 @@ impl RuskHttpBuilder {
4869 )
4970 . await ?;
5071
51- _ws_server = Some ( server ) ;
72+ server = Some ( http_server ) ;
5273 }
5374
54- if let Some ( s) = _ws_server {
55- s. wait ( ) . await ?;
75+ Ok ( RuskHttp {
76+ server,
77+ shutdown_timeout : self . shutdown_timeout ,
78+ } )
79+ }
80+
81+ pub async fn build_and_run ( self ) -> anyhow:: Result < ( ) > {
82+ let mut http = self . build ( ) . await ?;
83+ http. run ( ) . await
84+ }
85+ }
86+
87+ impl Default for RuskHttpBuilder {
88+ fn default ( ) -> Self {
89+ Self :: new ( )
90+ }
91+ }
92+
93+ pub struct RuskHttp {
94+ server : Option < HttpServer > ,
95+ shutdown_timeout : Duration ,
96+ }
97+
98+ impl RuskHttp {
99+ pub async fn run ( & mut self ) -> anyhow:: Result < ( ) > {
100+ if let Some ( server) = & mut self . server {
101+ server. wait ( ) . await ?;
56102 }
103+ Ok ( ( ) )
104+ }
57105
106+ pub async fn shutdown ( & mut self ) -> anyhow:: Result < ( ) > {
107+ if let Some ( server) = & mut self . server {
108+ tokio:: time:: timeout ( self . shutdown_timeout , server. shutdown ( ) )
109+ . await
110+ . map_err ( |_| {
111+ anyhow:: anyhow!(
112+ "HTTP server failed to shut down within {} seconds" ,
113+ self . shutdown_timeout. as_secs( )
114+ )
115+ } ) ??;
116+ }
58117 Ok ( ( ) )
59118 }
60119}
0 commit comments