GithubHelp home page GithubHelp logo

c0dearm / envconf Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 16 KB

Boilerplate free initialization of structs from environment variables

License: MIT License

Rust 100.00%
rust settings environment-variables

envconf's Introduction

envconf

Rust Crates Docs License

Boilerplate free initialization of structs from environment variables

Usage

It is usual that programs rely on environment variables to define their behavior, especially for cloud and microservies applications. Imagine you need to setup a database connection by reading the following environment variables:

  • DB_HOST
  • DB_PORT
  • DB_USER
  • DB_PASSWORD
  • DB_NAME

With this library crate, it is as easy as this:

use envconf::{Setting, Error};

#[derive(Setting)]
struct DBSettings {
    #[conf(env = "DB_HOST", default = "localhost")]
    host: String,
    #[conf(env = "DB_PORT", default = 5432)]
    port: usize,
    #[conf(default = "myuser")]  // hardcoded setting
    user: String,
    #[conf(env = "DB_PASSWORD")] // env variable required
    password: String,
    #[conf(env = "DB_NAME", default = "mydb")]
    name: String,
}

fn main() -> Result<(), Error<'static>> {
    // This env is mandatory, so it needs to be set!
    std::env::set_var("DB_PASSWORD", "secret");

    // Initialize config from environment variables
    // Read the crate docs to check the possible Error variants
    let db_settings = DBSettings::init()?;

    assert_eq!(db_settings.host, "localhost");
    assert_eq!(db_settings.port, 5432);
    assert_eq!(db_settings.user, "myuser");
    assert_eq!(db_settings.password, "secret");
    assert_eq!(db_settings.name, "mydb");

    Ok(())
}

Contributing

If you find a vulnerability, bug or would like a new feature, open a new issue.

To introduce your changes into the codebase, submit a Pull Request.

Many thanks!

License

envconf is distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE and LICENSE-MIT, and COPYRIGHT for details.

envconf's People

Contributors

c0dearm avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.