Skip to content

Commit 211e837

Browse files
apollo_storage: impl SerializeConfig for storage reader ServerConfig
1 parent a63da00 commit 211e837

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

crates/apollo_storage/src/storage_reader_server.rs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
use std::collections::BTreeMap;
12
use std::io;
23
use std::marker::PhantomData;
34
use std::net::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;
@@ -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,34 @@ impl ServerConfig {
3537
}
3638
}
3739

40+
impl Default for ServerConfig {
41+
fn default() -> Self {
42+
Self {
43+
socket: "0.0.0.0:8080".parse().expect("Default socket address should be valid"),
44+
enable: false,
45+
}
46+
}
47+
}
48+
49+
impl SerializeConfig for ServerConfig {
50+
fn dump(&self) -> BTreeMap<ParamPath, SerializedParam> {
51+
BTreeMap::from_iter([
52+
ser_param(
53+
"socket",
54+
&self.socket.to_string(),
55+
"The socket address for the storage reader HTTP server.",
56+
ParamPrivacyInput::Public,
57+
),
58+
ser_param(
59+
"enable",
60+
&self.enable,
61+
"Whether to enable the storage reader HTTP server.",
62+
ParamPrivacyInput::Public,
63+
),
64+
])
65+
}
66+
}
67+
3868
#[async_trait]
3969
/// Handler trait for processing storage reader requests.
4070
pub trait StorageReaderServerHandler<Request, Response> {
@@ -157,3 +187,21 @@ impl IntoResponse for StorageServerError {
157187
(StatusCode::INTERNAL_SERVER_ERROR, error_message).into_response()
158188
}
159189
}
190+
191+
pub fn create_storage_reader_server<RequestHandler, Request, Response>(
192+
storage_reader: StorageReader,
193+
socket: SocketAddr,
194+
enable: bool,
195+
) -> Option<StorageReaderServer<RequestHandler, Request, Response>>
196+
where
197+
RequestHandler: StorageReaderServerHandler<Request, Response>,
198+
Request: Serialize + DeserializeOwned + Send + 'static,
199+
Response: Serialize + DeserializeOwned + Send + 'static,
200+
{
201+
if enable {
202+
let config = ServerConfig::new(socket, enable);
203+
Some(StorageReaderServer::new(storage_reader, config))
204+
} else {
205+
None
206+
}
207+
}

0 commit comments

Comments
 (0)