@@ -4,7 +4,7 @@ use axum::{
4
4
Json , Router ,
5
5
body:: Body ,
6
6
extract:: State ,
7
- response:: IntoResponse ,
7
+ response:: Result ,
8
8
routing:: { get, post} ,
9
9
} ;
10
10
use bon:: Builder ;
@@ -33,11 +33,11 @@ pub struct ApiServer {
33
33
impl ApiServer {
34
34
pub fn router ( & self ) -> Router {
35
35
Router :: new ( )
36
- . route ( "/v1/submit/priority" , post ( Self :: submit_priority) )
37
- . route ( "/v1/submit/regular" , post ( Self :: submit_regular) )
38
- . route ( "/v1/encryption-key" , get ( Self :: encryption_key) )
39
- . route ( "/i/health" , get ( Self :: health) )
40
- . route ( "/i/metrics" , get ( Self :: metrics) )
36
+ . route ( "/v1/submit/priority" , post ( submit_priority) )
37
+ . route ( "/v1/submit/regular" , post ( submit_regular) )
38
+ . route ( "/v1/encryption-key" , get ( encryption_key) )
39
+ . route ( "/i/health" , get ( health) )
40
+ . route ( "/i/metrics" , get ( metrics) )
41
41
. with_state ( self . clone ( ) )
42
42
. layer (
43
43
ServiceBuilder :: new ( )
@@ -58,7 +58,7 @@ impl ApiServer {
58
58
. on_request ( |_r : & Request < Body > , _s : & Span | {
59
59
debug ! ( "request received" )
60
60
} )
61
- . on_response ( |r : & Response < Body > , d : Duration , _span : & Span | {
61
+ . on_response ( |r : & Response < Body > , d : Duration , _s : & Span | {
62
62
debug ! ( status = %r. status( ) . as_u16( ) , duration = ?d, "response created" )
63
63
} )
64
64
)
@@ -70,58 +70,41 @@ impl ApiServer {
70
70
axum:: serve ( listener, self . router ( ) ) . await
71
71
}
72
72
73
- async fn health ( this : State < Self > ) -> StatusCode {
74
- if this. bundles . is_closed ( ) {
75
- StatusCode :: INTERNAL_SERVER_ERROR
76
- } else {
77
- StatusCode :: OK
73
+ async fn submit_bundle ( & self , bundle : BundleVariant ) -> Result < ( ) > {
74
+ if self . bundles . send ( bundle) . await . is_err ( ) {
75
+ error ! ( "bundle channel is closed" ) ;
76
+ return Err ( StatusCode :: INTERNAL_SERVER_ERROR . into ( ) ) ;
78
77
}
78
+ Ok ( ( ) )
79
79
}
80
+ }
80
81
81
- async fn submit_priority (
82
- this : State < Self > ,
83
- bundle : Json < SignedPriorityBundle > ,
84
- ) -> ( StatusCode , & ' static str ) {
85
- match this. bundles . send ( BundleVariant :: Priority ( bundle. 0 ) ) . await {
86
- Ok ( ( ) ) => ( StatusCode :: OK , "priority bundle enqueued" ) ,
87
- Err ( _) => {
88
- error ! ( "bundle channel is closed" ) ;
89
- (
90
- StatusCode :: INTERNAL_SERVER_ERROR ,
91
- "failed to enqueue priority bundle" ,
92
- )
93
- }
94
- }
95
- }
82
+ async fn submit_priority ( server : State < ApiServer > , json : Json < SignedPriorityBundle > ) -> Result < ( ) > {
83
+ server. submit_bundle ( BundleVariant :: Priority ( json. 0 ) ) . await
84
+ }
96
85
97
- async fn submit_regular ( this : State < Self > , bundle : Json < Bundle > ) -> ( StatusCode , & ' static str ) {
98
- match this. bundles . send ( BundleVariant :: Regular ( bundle. 0 ) ) . await {
99
- Ok ( ( ) ) => ( StatusCode :: OK , "bundle enqueued" ) ,
100
- Err ( _) => {
101
- error ! ( "bundle channel is closed" ) ;
102
- (
103
- StatusCode :: INTERNAL_SERVER_ERROR ,
104
- "failed to enqueue bundle" ,
105
- )
106
- }
107
- }
108
- }
86
+ async fn submit_regular ( server : State < ApiServer > , json : Json < Bundle > ) -> Result < ( ) > {
87
+ server. submit_bundle ( BundleVariant :: Regular ( json. 0 ) ) . await
88
+ }
109
89
110
- async fn encryption_key ( this : State < Self > ) -> Json < ThresholdEncKey > {
111
- Json ( this . enc_key . read ( ) . await )
112
- }
90
+ async fn encryption_key ( server : State < ApiServer > ) -> Json < ThresholdEncKey > {
91
+ Json ( server . enc_key . read ( ) . await )
92
+ }
113
93
114
- async fn metrics ( this : State < Self > ) -> impl IntoResponse {
115
- match this. metrics . export ( ) {
116
- Ok ( output) => ( StatusCode :: OK , output) . into_response ( ) ,
117
- Err ( err) => {
118
- error ! ( %err, "metrics export error" ) ;
119
- (
120
- StatusCode :: INTERNAL_SERVER_ERROR ,
121
- "failed to export metrics" ,
122
- )
123
- . into_response ( )
124
- }
94
+ async fn metrics ( server : State < ApiServer > ) -> Result < String > {
95
+ match server. metrics . export ( ) {
96
+ Ok ( output) => Ok ( output) ,
97
+ Err ( err) => {
98
+ error ! ( %err, "metrics export error" ) ;
99
+ Err ( StatusCode :: INTERNAL_SERVER_ERROR . into ( ) )
125
100
}
126
101
}
127
102
}
103
+
104
+ async fn health ( server : State < ApiServer > ) -> Result < ( ) > {
105
+ if server. bundles . is_closed ( ) {
106
+ error ! ( "bundle channel is closed" ) ;
107
+ return Err ( StatusCode :: INTERNAL_SERVER_ERROR . into ( ) ) ;
108
+ }
109
+ Ok ( ( ) )
110
+ }
0 commit comments