Rename ServerData
to ServerConfig
This commit is contained in:
parent
d10d12b606
commit
3cd92601cd
2 changed files with 13 additions and 10 deletions
|
@ -44,22 +44,25 @@ struct Stats {
|
|||
num_documents: usize,
|
||||
}
|
||||
|
||||
/// Data that will be used to configure the server.
|
||||
/// Server configuration.
|
||||
#[derive(Debug)]
|
||||
pub struct ServerData {
|
||||
pub struct ServerConfig {
|
||||
/// Number of days to clean up documents after inactivity.
|
||||
pub expiry_days: u32,
|
||||
}
|
||||
|
||||
impl Default for ServerData {
|
||||
impl Default for ServerConfig {
|
||||
fn default() -> Self {
|
||||
Self { expiry_days: 1 }
|
||||
}
|
||||
}
|
||||
|
||||
/// A combined filter handling all server routes.
|
||||
pub fn server(data: ServerData) -> BoxedFilter<(impl Reply,)> {
|
||||
warp::path("api").and(backend(data)).or(frontend()).boxed()
|
||||
pub fn server(config: ServerConfig) -> BoxedFilter<(impl Reply,)> {
|
||||
warp::path("api")
|
||||
.and(backend(config))
|
||||
.or(frontend())
|
||||
.boxed()
|
||||
}
|
||||
|
||||
/// Construct routes for static files from React.
|
||||
|
@ -68,9 +71,9 @@ fn frontend() -> BoxedFilter<(impl Reply,)> {
|
|||
}
|
||||
|
||||
/// Construct backend routes, including WebSocket handlers.
|
||||
fn backend(data: ServerData) -> BoxedFilter<(impl Reply,)> {
|
||||
fn backend(config: ServerConfig) -> BoxedFilter<(impl Reply,)> {
|
||||
let state: Arc<DashMap<String, Document>> = Default::default();
|
||||
tokio::spawn(cleaner(Arc::clone(&state), data.expiry_days));
|
||||
tokio::spawn(cleaner(Arc::clone(&state), config.expiry_days));
|
||||
|
||||
let state_filter = warp::any().map(move || Arc::clone(&state));
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use rustpad_server::{server, ServerData};
|
||||
use rustpad_server::{server, ServerConfig};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
|
@ -10,12 +10,12 @@ async fn main() {
|
|||
.parse()
|
||||
.expect("Unable to parse PORT");
|
||||
|
||||
let data = ServerData {
|
||||
let config = ServerConfig {
|
||||
expiry_days: std::env::var("EXPIRY_DAYS")
|
||||
.unwrap_or_else(|_| String::from("1"))
|
||||
.parse()
|
||||
.expect("Unable to parse EXPIRY_DAYS"),
|
||||
};
|
||||
|
||||
warp::serve(server(data)).run(([0, 0, 0, 0], port)).await;
|
||||
warp::serve(server(config)).run(([0, 0, 0, 0], port)).await;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue