1
1
use std:: {
2
2
collections:: HashMap ,
3
3
net:: { IpAddr , Ipv4Addr } ,
4
- sync:: { mpsc:: Sender , Arc , Mutex , RwLock } ,
4
+ sync:: { mpsc:: Sender , Arc , Mutex } ,
5
5
} ;
6
6
7
7
use chainhook_event_observer:: {
@@ -20,10 +20,10 @@ use std::error::Error;
20
20
21
21
use crate :: config:: PredicatesApiConfig ;
22
22
23
- use super :: { open_readwrite_predicates_db_conn_or_panic , PredicateStatus } ;
23
+ use super :: { open_readwrite_predicates_db_conn , PredicateStatus } ;
24
24
25
25
pub async fn start_predicate_api_server (
26
- api_config : & PredicatesApiConfig ,
26
+ api_config : PredicatesApiConfig ,
27
27
observer_commands_tx : Sender < ObserverCommand > ,
28
28
ctx : Context ,
29
29
) -> Result < ( ) , Box < dyn Error > > {
@@ -56,16 +56,13 @@ pub async fn start_predicate_api_server(
56
56
] ;
57
57
58
58
let background_job_tx_mutex = Arc :: new ( Mutex :: new ( observer_commands_tx. clone ( ) ) ) ;
59
- let redis_con_rw_lock = Arc :: new ( RwLock :: new ( open_readwrite_predicates_db_conn_or_panic (
60
- api_config, & ctx,
61
- ) ) ) ;
62
59
63
60
let ctx_cloned = ctx. clone ( ) ;
64
61
65
62
let ignite = rocket:: custom ( control_config)
66
63
. manage ( background_job_tx_mutex)
64
+ . manage ( api_config)
67
65
. manage ( ctx_cloned)
68
- . manage ( redis_con_rw_lock)
69
66
. mount ( "/" , routes)
70
67
. ignite ( )
71
68
. await ?;
@@ -89,44 +86,45 @@ fn handle_ping(ctx: &State<Context>) -> Json<JsonValue> {
89
86
#[ openapi( tag = "Chainhooks" ) ]
90
87
#[ get( "/v1/chainhooks" , format = "application/json" ) ]
91
88
fn handle_get_predicates (
92
- predicate_db : & State < Arc < RwLock < Connection > > > ,
89
+ api_config : & State < PredicatesApiConfig > ,
93
90
ctx : & State < Context > ,
94
91
) -> Json < JsonValue > {
95
92
ctx. try_log ( |logger| slog:: info!( logger, "Handling HTTP GET /v1/chainhooks" ) ) ;
96
- if let Ok ( mut predicates_db_conn) = predicate_db. inner ( ) . write ( ) {
97
- let predicates = match get_entries_from_predicates_db ( & mut predicates_db_conn, & ctx) {
98
- Ok ( predicates) => predicates,
99
- Err ( e) => {
100
- ctx. try_log ( |logger| slog:: warn!( logger, "unable to retrieve predicates: {e}" ) ) ;
101
- return Json ( json ! ( {
102
- "status" : 500 ,
103
- "message" : "unable to retrieve predicates" ,
104
- } ) ) ;
105
- }
106
- } ;
107
-
108
- let serialized_predicates = predicates
109
- . iter ( )
110
- . map ( |( p, _) | p. into_serialized_json ( ) )
111
- . collect :: < Vec < _ > > ( ) ;
112
-
113
- Json ( json ! ( {
114
- "status" : 200 ,
115
- "result" : serialized_predicates
116
- } ) )
117
- } else {
118
- Json ( json ! ( {
93
+ match open_readwrite_predicates_db_conn ( api_config) {
94
+ Ok ( mut predicates_db_conn) => {
95
+ let predicates = match get_entries_from_predicates_db ( & mut predicates_db_conn, & ctx) {
96
+ Ok ( predicates) => predicates,
97
+ Err ( e) => {
98
+ ctx. try_log ( |logger| slog:: warn!( logger, "unable to retrieve predicates: {e}" ) ) ;
99
+ return Json ( json ! ( {
100
+ "status" : 500 ,
101
+ "message" : "unable to retrieve predicates" ,
102
+ } ) ) ;
103
+ }
104
+ } ;
105
+
106
+ let serialized_predicates = predicates
107
+ . iter ( )
108
+ . map ( |( p, _) | p. into_serialized_json ( ) )
109
+ . collect :: < Vec < _ > > ( ) ;
110
+
111
+ Json ( json ! ( {
112
+ "status" : 200 ,
113
+ "result" : serialized_predicates
114
+ } ) )
115
+ }
116
+ Err ( e) => Json ( json ! ( {
119
117
"status" : 500 ,
120
- "message" : "too many requests" ,
121
- } ) )
118
+ "message" : e ,
119
+ } ) ) ,
122
120
}
123
121
}
124
122
125
123
#[ openapi( tag = "Chainhooks" ) ]
126
124
#[ post( "/v1/chainhooks" , format = "application/json" , data = "<predicate>" ) ]
127
125
fn handle_create_predicate (
128
126
predicate : Json < ChainhookFullSpecification > ,
129
- predicate_db : & State < Arc < RwLock < Connection > > > ,
127
+ api_config : & State < PredicatesApiConfig > ,
130
128
background_job_tx : & State < Arc < Mutex < Sender < ObserverCommand > > > > ,
131
129
ctx : & State < Context > ,
132
130
) -> Json < JsonValue > {
@@ -141,7 +139,7 @@ fn handle_create_predicate(
141
139
142
140
let predicate_uuid = predicate. get_uuid ( ) . to_string ( ) ;
143
141
144
- if let Ok ( mut predicates_db_conn) = predicate_db . inner ( ) . write ( ) {
142
+ if let Ok ( mut predicates_db_conn) = open_readwrite_predicates_db_conn ( api_config ) {
145
143
match get_entry_from_predicates_db (
146
144
& ChainhookSpecification :: either_stx_or_btc_key ( & predicate_uuid) ,
147
145
& mut predicates_db_conn,
@@ -175,7 +173,7 @@ fn handle_create_predicate(
175
173
#[ get( "/v1/chainhooks/<predicate_uuid>" , format = "application/json" ) ]
176
174
fn handle_get_predicate (
177
175
predicate_uuid : String ,
178
- predicate_db : & State < Arc < RwLock < Connection > > > ,
176
+ api_config : & State < PredicatesApiConfig > ,
179
177
ctx : & State < Context > ,
180
178
) -> Json < JsonValue > {
181
179
ctx. try_log ( |logger| {
@@ -186,43 +184,44 @@ fn handle_get_predicate(
186
184
)
187
185
} ) ;
188
186
189
- if let Ok ( mut predicates_db_conn) = predicate_db. inner ( ) . write ( ) {
190
- let entry = match get_entry_from_predicates_db (
191
- & ChainhookSpecification :: either_stx_or_btc_key ( & predicate_uuid) ,
192
- & mut predicates_db_conn,
193
- & ctx,
194
- ) {
195
- Ok ( Some ( ( ChainhookSpecification :: Stacks ( spec) , status) ) ) => json ! ( {
196
- "chain" : "stacks" ,
197
- "uuid" : spec. uuid,
198
- "network" : spec. network,
199
- "predicate" : spec. predicate,
200
- "status" : status,
201
- "enabled" : spec. enabled,
202
- } ) ,
203
- Ok ( Some ( ( ChainhookSpecification :: Bitcoin ( spec) , status) ) ) => json ! ( {
204
- "chain" : "bitcoin" ,
205
- "uuid" : spec. uuid,
206
- "network" : spec. network,
207
- "predicate" : spec. predicate,
208
- "status" : status,
209
- "enabled" : spec. enabled,
210
- } ) ,
211
- _ => {
212
- return Json ( json ! ( {
213
- "status" : 404 ,
214
- } ) )
215
- }
216
- } ;
217
- Json ( json ! ( {
218
- "status" : 200 ,
219
- "result" : entry
220
- } ) )
221
- } else {
222
- Json ( json ! ( {
187
+ match open_readwrite_predicates_db_conn ( api_config) {
188
+ Ok ( mut predicates_db_conn) => {
189
+ let entry = match get_entry_from_predicates_db (
190
+ & ChainhookSpecification :: either_stx_or_btc_key ( & predicate_uuid) ,
191
+ & mut predicates_db_conn,
192
+ & ctx,
193
+ ) {
194
+ Ok ( Some ( ( ChainhookSpecification :: Stacks ( spec) , status) ) ) => json ! ( {
195
+ "chain" : "stacks" ,
196
+ "uuid" : spec. uuid,
197
+ "network" : spec. network,
198
+ "predicate" : spec. predicate,
199
+ "status" : status,
200
+ "enabled" : spec. enabled,
201
+ } ) ,
202
+ Ok ( Some ( ( ChainhookSpecification :: Bitcoin ( spec) , status) ) ) => json ! ( {
203
+ "chain" : "bitcoin" ,
204
+ "uuid" : spec. uuid,
205
+ "network" : spec. network,
206
+ "predicate" : spec. predicate,
207
+ "status" : status,
208
+ "enabled" : spec. enabled,
209
+ } ) ,
210
+ _ => {
211
+ return Json ( json ! ( {
212
+ "status" : 404 ,
213
+ } ) )
214
+ }
215
+ } ;
216
+ Json ( json ! ( {
217
+ "status" : 200 ,
218
+ "result" : entry
219
+ } ) )
220
+ }
221
+ Err ( e) => Json ( json ! ( {
223
222
"status" : 500 ,
224
- "message" : "too many requests" ,
225
- } ) )
223
+ "message" : e ,
224
+ } ) ) ,
226
225
}
227
226
}
228
227
0 commit comments