diff --git a/src/config.rs b/src/config.rs index 319fec1..6cdcae4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -12,6 +12,7 @@ use toml; pub struct Config { pub config_version: u16, pub port: u16, + pub results_per_page: u16, pub logging_level: LoggingLevel, pub connection: String, pub pool: Option, @@ -122,6 +123,7 @@ impl TomlConfig { pub struct NanobotConfig { pub config_version: u16, pub port: Option, + pub results_per_page: Option, } impl Default for NanobotConfig { @@ -129,6 +131,7 @@ impl Default for NanobotConfig { NanobotConfig { config_version: DEFAULT_CONFIG_VERSION, port: Some(DEFAULT_PORT), + results_per_page: Some(DEFAULT_RESULTS_PER_PAGE), } } } @@ -280,6 +283,7 @@ pub type SerdeMap = serde_json::Map; pub const DEFAULT_CONFIG_VERSION: u16 = 1; pub const DEFAULT_PORT: u16 = 3000; +pub const DEFAULT_RESULTS_PER_PAGE: u16 = 20; lazy_static! { pub static ref DEFAULT_TOML: String = format!("[nanobot]\nconfig_version = {}", DEFAULT_CONFIG_VERSION); @@ -296,6 +300,10 @@ impl Config { let config = Config { config_version: user.nanobot.config_version, port: user.nanobot.port.unwrap_or(DEFAULT_PORT), + results_per_page: user + .nanobot + .results_per_page + .unwrap_or(DEFAULT_RESULTS_PER_PAGE), logging_level: user.logging.unwrap_or_default().level.unwrap_or_default(), connection: user .database @@ -381,6 +389,7 @@ pub fn to_toml(config: &Config) -> TomlConfig { nanobot: NanobotConfig { config_version: config.config_version.clone(), port: Some(config.port.clone()), + results_per_page: Some(config.results_per_page.clone()), }, logging: Some(LoggingConfig { level: Some(config.logging_level.clone()), diff --git a/src/get.rs b/src/get.rs index 3f82619..5dd3949 100644 --- a/src/get.rs +++ b/src/get.rs @@ -6,7 +6,7 @@ use crate::config::Config; use crate::error::GetError; use crate::sql::{ get_count_from_pool, get_message_counts_from_pool, get_table_from_pool, get_total_from_pool, - LIMIT_DEFAULT, LIMIT_MAX, + LIMIT_MAX, }; use chrono::prelude::{DateTime, Utc}; use csv::WriterBuilder; @@ -55,7 +55,7 @@ pub async fn get_table( ) -> Result { let table = unquote(table).unwrap_or(table.to_string()); let mut select = Select::new(format!("\"{}\"", table)); - select.limit(LIMIT_DEFAULT); + select.limit(usize::from(config.results_per_page)); get_rows(config, &select, shape, format).await } @@ -123,7 +123,7 @@ pub async fn get_rows( match select.limit { Some(l) if l > LIMIT_MAX => select.limit(LIMIT_MAX), Some(l) if l > 0 => select.limit(l), - _ => select.limit(LIMIT_DEFAULT), + _ => select.limit(usize::from(config.results_per_page)), }; match shape { diff --git a/src/serve.rs b/src/serve.rs index b3fc841..5c74f22 100644 --- a/src/serve.rs +++ b/src/serve.rs @@ -1,4 +1,4 @@ -use crate::{config::Config, get, ldtab, sql::LIMIT_DEFAULT, tree_view}; +use crate::{config::Config, get, ldtab, tree_view}; use ansi_to_html; use axum::{ extract::{Form, Path, Query, State}, @@ -859,8 +859,8 @@ async fn table( Ok(n) => n, Err(e) => return Err(e.to_string().into()), }; - let pages = row_number / LIMIT_DEFAULT as u32; - pages * LIMIT_DEFAULT as u32 + let pages = row_number / state.config.results_per_page as u32; + pages * state.config.results_per_page as u32 }; let html = format!( r#"