# Crate actix_settings

source ·
Expand description

Easily manage Actix Web’s settings from a TOML file and environment variables.

To get started add a `Settings::parse_toml("./Server.toml")` call to the top of your main function. This will create a template file with descriptions of all the configurable settings. You can change or remove anything in that file and it will be picked up the next time you run your application.

Overriding parts of the file can be done from values using `Settings::override_field` or from the environment using `Settings::override_field_with_env_var`.

## §Examples

See examples folder on GitHub for complete example.

ⓘ

    use actix_settings::{ApplySettings as _, Mode, Settings};

    #[actix_web::main]
    async fn main() -> std::io::Result<()> {
        let mut settings = Settings::parse_toml("./Server.toml")
            .expect("Failed to parse `Settings` from Server.toml");

        // If the environment variable `$APPLICATION__HOSTS` is set,
        // have its value override the `settings.actix.hosts` setting:
        Settings::override_field_with_env_var(&mut settings.actix.hosts, "APPLICATION__HOSTS")?;

        init_logger(&settings);

        HttpServer::new({
            // clone settings into each worker thread
            let settings = settings.clone();

            move || {
                App::new()
                    // Include this `.wrap()` call for compression settings to take effect
                    .wrap(Condition::new(
                        settings.actix.enable_compression,
                        Compress::default(),
                    ))

                    // add request logger
                    .wrap(Logger::default())

                    // make `Settings` available to handlers
                    .app_data(web::Data::new(settings.clone()))

                    // add request handlers as normal
                    .service(index)
            }
        })
        // apply the `Settings` to Actix Web's `HttpServer`
        .apply_settings(&settings)
        .run()
        .await
    }

## Structs§

- ActixSettings
  Settings types for Actix Web.
- Address
  A host/port pair for the server to bind to.
- BasicSettings
  Wrapper for server and application-specific settings.
- NoSettings
  Marker type representing no defined application-specific settings.
- Tls
  TLS (HTTPS) configuration.

## Enums§

- Backlog
  The maximum number of pending connections.
- Error
  Errors that can be returned from methods in this crate.
- KeepAlive
  The server keep-alive preference.
- MaxConnectionRate
  The maximum per-worker concurrent TLS connection limit.
- MaxConnections
  The maximum per-worker number of concurrent connections.
- Mode
  Marker of intended deployment environment.
- NumWorkers
  The number of workers that the server should start.
- Timeout
  A timeout duration in milliseconds or seconds.

## Traits§

- ApplySettings
  Extension trait for applying parsed settings to the server object.
- Parse
  A specialized `FromStr` trait that returns `Error` errors

## Type Aliases§

- Settings
  Convenience type alias for `BasicSettings` with no defined application-specific settings.