@@ -21,6 +21,7 @@ use storage::engine::{
2121use storage:: persistance:: { cold_save, load_all_tables} ;
2222use network:: broadcaster:: { membership_sync, heartbeat, get_membership, update_membership} ;
2323use crate :: storage:: snapshoting:: start_snapshot_task;
24+ use crate :: storage:: statistics:: { get_stats, MetricsCollector , MetricsMiddleware } ;
2425use crate :: storage:: subscription:: SubscriptionManager ;
2526
2627/// Declare APP_STATE globally so that it’s available throughout the module.
@@ -176,6 +177,10 @@ async fn main() -> std::io::Result<()> {
176177 tokio:: spawn ( membership_sync ( cluster_clone, current_clone, 60 ) ) ;
177178 let subscription_manager = web:: Data :: new ( SubscriptionManager :: new ( ) ) ;
178179
180+ // Initialize metrics collector and middleware
181+ let metrics_collector = web:: Data :: new ( MetricsCollector :: new ( ) ) ;
182+ // extract a clonable handle for the middleware
183+ let mw_col = metrics_collector. get_ref ( ) . clone ( ) ;
179184
180185 let use_https = match env:: var ( "DYNA_MODE" ) . unwrap_or_default ( ) . as_str ( ) {
181186 "https" => true ,
@@ -199,6 +204,8 @@ async fn main() -> std::io::Result<()> {
199204 // Build and run the HTTP server.
200205 HttpServer :: new ( move || {
201206 App :: new ( )
207+ . wrap ( MetricsMiddleware :: new ( mw_col. clone ( ) ) )
208+ . app_data ( metrics_collector. clone ( ) )
202209 . app_data ( subscription_manager. clone ( ) )
203210 . app_data ( state. clone ( ) )
204211 . app_data ( cluster_data. clone ( ) )
@@ -223,6 +230,7 @@ async fn main() -> std::io::Result<()> {
223230 storage:: subscription:: subscribe_to_key
224231 ) )
225232 . route ( "/auth/{user}" , web:: post ( ) . to ( security:: authentication:: access) )
233+ . service ( get_stats)
226234 } )
227235 . bind_openssl ( bind_addr. as_str ( ) , builder) ?
228236 . run ( )
@@ -232,37 +240,40 @@ async fn main() -> std::io::Result<()> {
232240 println ! ( "Starting distributed DB engine at http://{}" , current_node) ;
233241
234242 // Build and run the HTTP server.
235- HttpServer :: new ( move || {
236- App :: new ( )
237- . app_data ( subscription_manager. clone ( ) )
238- . app_data ( state. clone ( ) )
239- . app_data ( cluster_data. clone ( ) )
240- . app_data ( web:: Data :: new ( current_node. clone ( ) ) )
241- // Cluster management endpoints.
242- . route ( "/join" , web:: post ( ) . to ( join_cluster) )
243- . route ( "/membership" , web:: get ( ) . to ( get_membership) )
244- . route ( "/update_membership" , web:: post ( ) . to ( update_membership) )
245- . route ( "/heartbeat" , web:: get ( ) . to ( heartbeat) )
246- // Key–value endpoints with multi‑table support.
247- . route ( "/{table}/key/{key}" , web:: get ( ) . to ( get_value) )
248- . route ( "/{table}/key/{key}" , web:: put ( ) . to ( put_value) )
249- . route ( "/{table}/key/{key}" , web:: delete ( ) . to ( delete_value) )
250- // Endpoint to fetch a table’s entire in‑memory store.
251- . route ( "/{table}/store" , web:: get ( ) . to ( get_table_store) )
252- // Global endpoint returning the entire in‑memory store.
253- . route ( "/store" , web:: get ( ) . to ( get_global_store) )
254- // Endpoints to get keys from a table.
255- . route ( "/{table}/keys" , web:: get ( ) . to ( get_all_keys) )
256- . route ( "/{table}/keys" , web:: post ( ) . to ( get_multiple_keys) )
257- . route ( "/{table}/subscribe/{key}" , web:: get ( ) . to (
258- storage:: subscription:: subscribe_to_key
259- ) )
260- . route ( "/auth/{user}" , web:: post ( ) . to ( security:: authentication:: access) )
261- } )
262- . bind ( bind_addr. as_str ( ) ) ?
263- . run ( )
264- . await
243+ HttpServer :: new ( move || {
244+ App :: new ( )
245+ . wrap ( MetricsMiddleware :: new ( mw_col. clone ( ) ) )
246+ . app_data ( metrics_collector. clone ( ) )
247+ . app_data ( subscription_manager. clone ( ) )
248+ . app_data ( state. clone ( ) )
249+ . app_data ( cluster_data. clone ( ) )
250+ . app_data ( web:: Data :: new ( current_node. clone ( ) ) )
251+ // Cluster management endpoints.
252+ . route ( "/join" , web:: post ( ) . to ( join_cluster) )
253+ . route ( "/membership" , web:: get ( ) . to ( get_membership) )
254+ . route ( "/update_membership" , web:: post ( ) . to ( update_membership) )
255+ . route ( "/heartbeat" , web:: get ( ) . to ( heartbeat) )
256+ // Key–value endpoints with multi‑table support.
257+ . route ( "/{table}/key/{key}" , web:: get ( ) . to ( get_value) )
258+ . route ( "/{table}/key/{key}" , web:: put ( ) . to ( put_value) )
259+ . route ( "/{table}/key/{key}" , web:: delete ( ) . to ( delete_value) )
260+ // Endpoint to fetch a table’s entire in‑memory store.
261+ . route ( "/{table}/store" , web:: get ( ) . to ( get_table_store) )
262+ // Global endpoint returning the entire in‑memory store.
263+ . route ( "/store" , web:: get ( ) . to ( get_global_store) )
264+ // Endpoints to get keys from a table.
265+ . route ( "/{table}/keys" , web:: get ( ) . to ( get_all_keys) )
266+ . route ( "/{table}/keys" , web:: post ( ) . to ( get_multiple_keys) )
267+ . route ( "/{table}/subscribe/{key}" , web:: get ( ) . to (
268+ storage:: subscription:: subscribe_to_key
269+ ) )
270+ . route ( "/auth/{user}" , web:: post ( ) . to ( security:: authentication:: access) )
271+ . service ( get_stats)
272+ } )
273+ . bind ( bind_addr. as_str ( ) ) ?
274+ . run ( )
275+ . await
265276 }
266277 }
267278
268- }
279+ }
0 commit comments