#[non_exhaustive]pub struct ProjectConfig {
pub debug: bool,
pub register_panic_hook: bool,
pub secret_key: SecretKey,
pub fallback_secret_keys: Vec<SecretKey>,
pub auth_backend: AuthBackendConfig,
pub database: DatabaseConfig,
pub cache: CacheConfig,
pub static_files: StaticFilesConfig,
pub middlewares: MiddlewareConfig,
pub client_ip: ClientIpConfig,
pub email: EmailConfig,
pub extra: Table,
}Expand description
The configuration for a project.
This is all the project-specific configuration data that can (and makes sense to) be expressed in a TOML configuration file.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.debug: boolDebug mode flag.
This enables some expensive operations that are useful for debugging, such as logging additional information, and collecting some extra diagnostics for generating error pages. The debug flag also controls whether Cot displays nice error pages. All of this hurts the performance, so it should be disabled for production.
ProjectConfig::default() returns true here when the application is
compiled in debug mode, and false when it is compiled in release
mode.
§Examples
use cot::config::{AuthBackendConfig, ProjectConfig, SecretKey};
let config = ProjectConfig::from_toml(
r#"
debug = true
"#,
)?;
assert_eq!(config.debug, true);register_panic_hook: boolWhether to register a panic hook.
The panic hook is used to display information about panics in the Cot error pages that are displayed in debug mode.
§Examples
use cot::config::{AuthBackendConfig, ProjectConfig, SecretKey};
let config = ProjectConfig::from_toml(
r#"
register_panic_hook = false
"#,
)?;
assert_eq!(config.register_panic_hook, false);secret_key: SecretKeyThe secret key used for signing cookies and other sensitive data.
This is a cryptographic key, should be kept secret, and should be set to a random and unique value for each project.
When you want to rotate the secret key, you can move the current key to
the fallback_secret_keys list, and set a new key here. Eventually, you
can remove the old key from the list.
§Examples
use cot::config::{AuthBackendConfig, ProjectConfig, SecretKey};
let config = ProjectConfig::from_toml(
r#"
secret_key = "123abc"
"#,
)?;
assert_eq!(config.secret_key, SecretKey::from("123abc"));fallback_secret_keys: Vec<SecretKey>Fallback secret keys that can be used to verify old sessions.
This is useful for key rotation, where you can add a new key, gradually migrate sessions to the new key, and then remove the old key.
§Examples
use cot::config::{AuthBackendConfig, ProjectConfig, SecretKey};
let config = ProjectConfig::from_toml(
r#"
fallback_secret_keys = ["123abc"]
"#,
)?;
assert_eq!(config.fallback_secret_keys, vec![SecretKey::from("123abc")]);auth_backend: AuthBackendConfigThe authentication backend to use.
§Examples
use cot::config::{AuthBackendConfig, ProjectConfig};
let config = ProjectConfig::from_toml(
r#"
[auth_backend]
type = "none"
"#,
)?;
assert_eq!(config.auth_backend, AuthBackendConfig::None);database: DatabaseConfigdb only.Database configuration.
§Examples
use cot::config::{AuthBackendConfig, DatabaseUrl, ProjectConfig};
let config = ProjectConfig::from_toml(
r#"
[database]
url = "sqlite::memory:"
"#,
)?;
assert_eq!(
config.database.url,
Some(DatabaseUrl::from("sqlite::memory:"))
);cache: CacheConfigcache only.Cache subsystem configuration.
§Examples
use std::time::Duration;
use cot::config::{CacheConfig, CacheStoreTypeConfig, ProjectConfig, Timeout};
let config = ProjectConfig::from_toml(
r#"
[cache]
prefix = "myapp"
max_retries = 3
timeout = "1h"
[cache.store]
type = "memory"
"#,
)?;
assert_eq!(config.cache.prefix, Some("myapp".to_string()));
assert_eq!(config.cache.max_retries, 3);
assert_eq!(
config.cache.timeout,
Timeout::After(Duration::from_secs(3600))
);
assert_eq!(config.cache.store.store_type, CacheStoreTypeConfig::Memory);
Ok::<(), cot::Error>(())static_files: StaticFilesConfigStatic files configuration.
§Examples
use std::time::Duration;
use cot::config::{AuthBackendConfig, DatabaseUrl, ProjectConfig, StaticFilesPathRewriteMode};
let config = ProjectConfig::from_toml(
r#"
[static_files]
url = "/assets/"
rewrite = "query_param"
cache_timeout = "1h"
"#,
)?;
assert_eq!(config.static_files.url, "/assets/");
assert_eq!(
config.static_files.rewrite,
StaticFilesPathRewriteMode::QueryParam,
);
assert_eq!(
config.static_files.cache_timeout,
Some(Duration::from_secs(3600)),
);middlewares: MiddlewareConfigMiddleware configuration.
§Examples
use cot::config::{MiddlewareConfig, ProjectConfig};
let config = ProjectConfig::from_toml(
r#"
[middlewares]
live_reload.enabled = true
"#,
)?;
assert_eq!(config.middlewares.live_reload.enabled, true);client_ip: ClientIpConfigClient IP address detection configuration.
§Examples
use cot::config::ProjectConfig;
let config = ProjectConfig::from_toml(
r#"
[client_ip]
proxies = ["127.0.0.1/32"]
"#,
)?;
assert_eq!(
config.client_ip.proxies,
vec!["127.0.0.1/32".parse().unwrap()]
);email: EmailConfigemail only.Email backend configuration.
§Examples
use cot::config::{EmailConfig, ProjectConfig};
let config = ProjectConfig::from_toml(
r#"
[email.transport]
type = "console"
"#,
)?;
assert_eq!(config.email, EmailConfig::default());extra: TableAll the config that was not recognized.
This is useful for parsing project-specific config that is not part of
any of the predefined fields in ProjectConfig.
§Examples
use cot::config::ProjectConfig;
use serde::Deserialize;
let config = ProjectConfig::from_toml(
r#"
[my_cot_project]
foo = "bar"
"#,
)?;
#[derive(Deserialize)]
struct MyCotProjectConfig {
foo: String,
}
let my_config: MyCotProjectConfig = config
.extra
.get("my_cot_project")
.ok_or("could not find the project config")?
.clone()
.try_into()?;
assert_eq!(my_config.foo, "bar");Implementations§
Source§impl ProjectConfig
impl ProjectConfig
Sourcepub fn builder() -> ProjectConfigBuilder
pub fn builder() -> ProjectConfigBuilder
Create a new ProjectConfigBuilder to build a ProjectConfig.
§Examples
use cot::config::ProjectConfig;
let config = ProjectConfig::builder().build();Sourcepub fn dev_default() -> ProjectConfig
pub fn dev_default() -> ProjectConfig
Create a new ProjectConfig with the default values for development.
This is useful for development purposes, where you want to have a configuration that you can just run as quickly as possible. This is mainly useful for tests and other local development activities.
Note that what this function returns exactly is not guaranteed to be the same across different versions of Cot. It’s meant to be used as a starting point for your development configuration and is subject to change.
§Examples
use cot::config::ProjectConfig;
let config = ProjectConfig::dev_default();Sourcepub fn from_toml(toml_content: &str) -> Result<ProjectConfig>
pub fn from_toml(toml_content: &str) -> Result<ProjectConfig>
Create a new ProjectConfig from a TOML string.
§Errors
This function will return an error if the TOML fails to parse as a
ProjectConfig.
§Examples
use cot::config::ProjectConfig;
let toml = r#"
secret_key = "123abc"
"#;
let config = ProjectConfig::from_toml(toml)?;Trait Implementations§
Source§impl Clone for ProjectConfig
impl Clone for ProjectConfig
Source§impl Debug for ProjectConfig
impl Debug for ProjectConfig
Source§impl Default for ProjectConfig
impl Default for ProjectConfig
Source§impl<'de> Deserialize<'de> for ProjectConfigwhere
ProjectConfig: Default,
impl<'de> Deserialize<'de> for ProjectConfigwhere
ProjectConfig: Default,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl JsonSchema for ProjectConfig
Available on crate feature _internal_config-docs only.
impl JsonSchema for ProjectConfig
_internal_config-docs only.Source§fn schema_id() -> Cow<'static, str>
fn schema_id() -> Cow<'static, str>
Source§fn json_schema(generator: &mut SchemaGenerator) -> Schema
fn json_schema(generator: &mut SchemaGenerator) -> Schema
Source§fn inline_schema() -> bool
fn inline_schema() -> bool
$ref keyword. Read moreAuto Trait Implementations§
impl Forget for ProjectConfig
impl Freeze for ProjectConfig
impl RefUnwindSafe for ProjectConfig
impl Send for ProjectConfig
impl Sync for ProjectConfig
impl Unpin for ProjectConfig
impl UnsafeUnpin for ProjectConfig
impl UnwindSafe for ProjectConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoField<Auto<T>> for T
impl<T> IntoField<Auto<T>> for T
Source§fn into_field(self) -> Auto<T>
fn into_field(self) -> Auto<T>
db only.