Skip to content

Commit 6796109

Browse files
apollo_storage: impl SerializeConfig for storage reader ServerConfig
1 parent 213edc2 commit 6796109

File tree

1 file changed

+52
-6
lines changed

1 file changed

+52
-6
lines changed

crates/apollo_storage/src/storage_reader_server.rs

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
use std::collections::BTreeMap;
12
use std::io;
23
use std::marker::PhantomData;
3-
use std::net::SocketAddr;
4+
use std::net::{Ipv4Addr, SocketAddr};
45

6+
use apollo_config::dumping::{ser_param, SerializeConfig};
7+
use apollo_config::{ParamPath, ParamPrivacyInput, SerializedParam};
58
use async_trait::async_trait;
69
use axum::extract::State;
710
use axum::http::StatusCode;
811
use axum::response::{IntoResponse, Response};
912
use axum::routing::post;
1013
use axum::{Json, Router};
1114
use serde::de::DeserializeOwned;
12-
use serde::Serialize;
15+
use serde::{Deserialize, Serialize};
1316
use tracing::{error, info};
1417

1518
use crate::{StorageError, StorageReader};
@@ -18,14 +21,13 @@ use crate::{StorageError, StorageReader};
1821
#[path = "storage_reader_server_test.rs"]
1922
mod storage_reader_server_test;
2023

21-
// TODO(Nadin): Remove #[allow(dead_code)] once the fields are used in the implementation.
22-
#[allow(dead_code)]
2324
/// Configuration for the storage reader server.
25+
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
2426
pub struct ServerConfig {
2527
/// The socket address to bind the server to.
26-
socket: SocketAddr,
28+
pub socket: SocketAddr,
2729
/// Whether the server is enabled.
28-
enable: bool,
30+
pub enable: bool,
2931
}
3032

3133
impl ServerConfig {
@@ -35,6 +37,31 @@ impl ServerConfig {
3537
}
3638
}
3739

40+
impl Default for ServerConfig {
41+
fn default() -> Self {
42+
Self { socket: (Ipv4Addr::UNSPECIFIED, 8080).into(), enable: false }
43+
}
44+
}
45+
46+
impl SerializeConfig for ServerConfig {
47+
fn dump(&self) -> BTreeMap<ParamPath, SerializedParam> {
48+
BTreeMap::from_iter([
49+
ser_param(
50+
"socket",
51+
&self.socket.to_string(),
52+
"The socket address for the storage reader HTTP server.",
53+
ParamPrivacyInput::Public,
54+
),
55+
ser_param(
56+
"enable",
57+
&self.enable,
58+
"Whether to enable the storage reader HTTP server.",
59+
ParamPrivacyInput::Public,
60+
),
61+
])
62+
}
63+
}
64+
3865
#[async_trait]
3966
/// Handler trait for processing storage reader requests.
4067
pub trait StorageReaderServerHandler<Request, Response> {
@@ -157,3 +184,22 @@ impl IntoResponse for StorageServerError {
157184
(StatusCode::INTERNAL_SERVER_ERROR, error_message).into_response()
158185
}
159186
}
187+
188+
/// Creates and returns an optional StorageReaderServer based on the enable flag.
189+
pub fn create_storage_reader_server<RequestHandler, Request, Response>(
190+
storage_reader: StorageReader,
191+
socket: SocketAddr,
192+
enable: bool,
193+
) -> Option<StorageReaderServer<RequestHandler, Request, Response>>
194+
where
195+
RequestHandler: StorageReaderServerHandler<Request, Response>,
196+
Request: Serialize + DeserializeOwned + Send + 'static,
197+
Response: Serialize + DeserializeOwned + Send + 'static,
198+
{
199+
if enable {
200+
let config = ServerConfig::new(socket, enable);
201+
Some(StorageReaderServer::new(storage_reader, config))
202+
} else {
203+
None
204+
}
205+
}

0 commit comments

Comments
 (0)