#configuration #http #derive

choices-derive

通过定义一个结构体来创建 HTTP 配置服务的 crate

6 个版本 (1 个稳定版本)

1.0.0 2022年5月26日
0.4.0 2021年5月8日
0.3.0 2021年2月27日
0.2.0 2021年2月18日
0.1.1 2021年2月11日

#547#config

每月下载量:30
choices 中使用

MIT 许可证

34KB
799 代码行

choices

works badge Released API docs License: MIT

你喜欢 structopsclap 吗?你写 microservices 吗?继续阅读!

choices 是一个库,让你可以通过简单的结构体来暴露你的应用程序配置。

看看,这很简单

给定以下代码

use choices::Choices;
use lazy_static::lazy_static;
use std::sync::{Arc, Mutex};

#[derive(Choices)]
struct Config {
    debug: bool,
    id: Option<i32>,
    log_file: String,
}

lazy_static! {
    static ref CONFIG: Arc<Mutex<Config>> = {
        Arc::new(Mutex::new(Config {
            debug: false,
            id: Some(3),
            log_file: "log.txt".to_string()
        }))
    };
}

#[tokio::main]
async fn main() {
    CONFIG.run((std::net::Ipv4Addr::LOCALHOST, 8081)).await;
}

你可以在 localhost:8081/config 上看到所有配置字段,以及在 localhost:8081/config/<field name> 上看到各个字段的值。
可以通过 PUT 来更改字段的值,例如 curl -X PUT localhost:8081/config/debug -d "true".

更多示例请参阅 示例

还可以查看 文档

功能

  • 显示所有配置字段
  • 获取配置字段
  • PUT 配置字段
  • 用户定义类型
  • JSON 支持
  • 自定义验证器
  • 设置回调

谢谢

特别感谢 structops 的作者。它为学习过程式宏提供了灵感。


lib.rs:

choices crate 的过程式宏。

依赖项

~1.2–1.7MB
~39K SLoC