Skip to content

Commit 2c2984e

Browse files
apollo_storage: impl SerializeConfig for storage reader ServerConfig
1 parent 7785094 commit 2c2984e

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

crates/apollo_storage/src/storage_reader_server.rs

Lines changed: 54 additions & 5 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;
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;
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,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,22 @@ impl IntoResponse for StorageServerError {
157187
(StatusCode::INTERNAL_SERVER_ERROR, error_message).into_response()
158188
}
159189
}
190+
191+
/// Creates and returns an optional StorageReaderServer based on the enable flag.
192+
pub fn create_storage_reader_server<RequestHandler, Request, Response>(
193+
storage_reader: StorageReader,
194+
socket: SocketAddr,
195+
enable: bool,
196+
) -> Option<StorageReaderServer<RequestHandler, Request, Response>>
197+
where
198+
RequestHandler: StorageReaderServerHandler<Request, Response>,
199+
Request: Serialize + DeserializeOwned + Send + 'static,
200+
Response: Serialize + DeserializeOwned + Send + 'static,
201+
{
202+
if enable {
203+
let config = ServerConfig::new(socket, enable);
204+
Some(StorageReaderServer::new(storage_reader, config))
205+
} else {
206+
None
207+
}
208+
}

0 commit comments

Comments
 (0)