Skip to content

Commit

Permalink
adopt actix-settings crate (#270)
Browse files Browse the repository at this point in the history
* adopt actix-settings crate

* add licenses and readme addition

* revamp readme

* delete temp prettier file
  • Loading branch information
robjtede authored Jul 31, 2022
1 parent 6e79465 commit e13b62f
Show file tree
Hide file tree
Showing 14 changed files with 1,604 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ guide/build/
*.sock
*~
.DS_Store

Server.toml
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ members = [
"actix-protobuf",
"actix-redis",
"actix-session",
"actix-settings",
"actix-web-httpauth",
]

Expand All @@ -17,6 +18,7 @@ actix-limitation = { path = "./actix-limitation" }
actix-protobuf = { path = "./actix-protobuf" }
actix-redis = { path = "./actix-redis" }
actix-session = { path = "./actix-session" }
actix-settings = { path = "./actix-settings" }
actix-web-httpauth = { path = "./actix-web-httpauth" }

# uncomment to quickly test against local actix-web repo
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
| [actix-protobuf] | [![crates.io](https://img.shields.io/crates/v/actix-protobuf?label=latest)](https://crates.io/crates/actix-protobuf) [![dependency status](https://deps.rs/crate/actix-protobuf/0.8.0/status.svg)](https://deps.rs/crate/actix-protobuf/0.8.0) | Protobuf payload extractor. |
| [actix-redis] | [![crates.io](https://img.shields.io/crates/v/actix-redis?label=latest)](https://crates.io/crates/actix-redis) [![dependency status](https://deps.rs/crate/actix-redis/0.12.0/status.svg)](https://deps.rs/crate/actix-redis/0.12.0) | Actor-based Redis client. |
| [actix-session] | [![crates.io](https://img.shields.io/crates/v/actix-session?label=latest)](https://crates.io/crates/actix-session) [![dependency status](https://deps.rs/crate/actix-session/0.7.1/status.svg)](https://deps.rs/crate/actix-session/0.7.1) | Session management. |
| [actix-settings] | [![crates.io](https://img.shields.io/crates/v/actix-settings?label=latest)](https://crates.io/crates/actix-settings) [![dependency status](https://deps.rs/crate/actix-settings/0.5.2/status.svg)](https://deps.rs/crate/actix-settings/0.5.2) | Easily manage Actix Web's settings from a TOML file and environment variables. |
| [actix-web-httpauth] | [![crates.io](https://img.shields.io/crates/v/actix-web-httpauth?label=latest)](https://crates.io/crates/actix-web-httpauth) [![dependency status](https://deps.rs/crate/actix-web-httpauth/0.8.0/status.svg)](https://deps.rs/crate/actix-web-httpauth/0.8.0) | HTTP authentication schemes. |

---
Expand Down Expand Up @@ -55,6 +56,7 @@ To add a crate to this list, submit a pull request.
[actix-protobuf]: ./actix-protobuf
[actix-redis]: ./actix-redis
[actix-session]: ./actix-session
[actix-settings]: ./actix-settings
[actix-web-httpauth]: ./actix-web-httpauth
[actix-web-lab]: https://crates.io/crates/actix-web-lab
[actix-multipart-extract]: https://crates.io/crates/actix-multipart-extract
Expand Down
9 changes: 9 additions & 0 deletions actix-settings/CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Changes

## Unreleased - 2022-xx-xx
- Update Actix Web dependencies to v4 ecosystem.
- Rename `actix.ssl` settings object to `actix.tls`.
- `NoSettings` is now marked `#[non_exhaustive]`.

## 0.5.2 - 2022-07-31
- Adopted into @actix org from <https://github.com/jjpe/actix-settings>.
24 changes: 24 additions & 0 deletions actix-settings/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "actix-settings"
version = "0.5.2"
authors = [
"Joey Ezechiels <[email protected]>",
"Rob Ede <[email protected]>",
]
edition = "2018"
description = "Easily manage Actix Web's settings from a TOML file and environment variables"
license = "MIT OR Apache-2.0"

[dependencies]
actix-http = "3"
actix-service = "2"
actix-web = "4"

ioe = "0.5"
once_cell = "1.13"
regex = "1.5.5"
serde = { version = "1", features = ["derive"] }
toml = "0.5"

[dev-dependencies]
env_logger = "0.9"
1 change: 1 addition & 0 deletions actix-settings/LICENSE-APACHE
1 change: 1 addition & 0 deletions actix-settings/LICENSE-MIT
31 changes: 31 additions & 0 deletions actix-settings/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# actix-settings

> Easily manage Actix Web's settings from a TOML file and environment variables.
[![crates.io](https://img.shields.io/crates/v/actix-settings?label=latest)](https://crates.io/crates/actix-settings)
[![Documentation](https://docs.rs/actix-settings/badge.svg?version=0.5.2)](https://docs.rs/actix-settings/0.5.2)
![Apache 2.0 or MIT licensed](https://img.shields.io/crates/l/actix-settings)
[![Dependency Status](https://deps.rs/crate/actix-settings/0.5.2/status.svg)](https://deps.rs/crate/actix-settings/0.5.2)

## Documentation & Resources

- [API Documentation](https://docs.rs/actix-settings)
- [Usage Example][usage]
- Minimum Supported Rust Version (MSRV): 1.57

### Custom Settings

There is a way to extend the available settings. This can be used to combine the settings provided by Actix Web and those provided by application server built using `actix`.

Have a look at [the usage example][usage] to see how.

## WIP

Configuration options for TLS set up are not yet implemented.

## Special Thanks

This crate was made possible by support from Accept B.V and [@jjpe].

[usage]: https://github.com/actix/actix-extras/blob/master/actix-settings/examples/actix.rs
[@jjpe]: https://github.com/jjpe
82 changes: 82 additions & 0 deletions actix-settings/examples/actix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use actix_settings::{ApplySettings as _, Mode, Settings};
use actix_web::{
get,
middleware::{Compress, Condition, Logger},
web, App, HttpServer, Responder,
};

#[get("/")]
async fn index(settings: web::Data<Settings>) -> impl Responder {
format!(
r#"{{
"mode": "{}",
"hosts": ["{}"]
}}"#,
match settings.actix.mode {
Mode::Development => "development",
Mode::Production => "production",
},
settings
.actix
.hosts
.iter()
.map(|addr| { format!("{}:{}", addr.host, addr.port) })
.collect::<Vec<_>>()
.join(", "),
)
.customize()
.insert_header(("content-type", "application/json"))
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
let mut settings = Settings::parse_toml("./examples/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(),
))
.wrap(Logger::default())
// make `Settings` available to handlers
.app_data(web::Data::new(settings.clone()))
.service(index)
}
})
// apply the `Settings` to Actix Web's `HttpServer`
.apply_settings(&settings)
.run()
.await
}

/// Initialize the logging infrastructure
fn init_logger(settings: &Settings) {
if !settings.actix.enable_log {
return;
}

std::env::set_var(
"RUST_LOG",
match settings.actix.mode {
Mode::Development => "actix_web=debug",
Mode::Production => "actix_web=info",
},
);

std::env::set_var("RUST_BACKTRACE", "1");

env_logger::init();
}
Loading

0 comments on commit e13b62f

Please sign in to comment.