Coder Social home page Coder Social logo

go-app-env's Introduction

GO APP ENV

ci codecov Go Report Card GoDoc

Library to read JSON file and transform into flatten configuration map with ability to overwrite config by using specific environment file or environment variable.

Active environment

Active environment can configure from environment variable name GO_APP_ACTIVE_ENV or command line argument --env.

In case of found both environment variable and command line argument, AppEnv will use value from environment variable

If both not provide, default active environment as default

Config Directory

Config directory can configure from environment variable name GO_APP_CONFIG_DIR or command line argument --configDir.

In case of found both environment variable and command line argument, AppEnv will use value from environment variable

If both not provide, default active environment as ./resources/


Configuration Mapping

By default, App env will read JSON file name config.json under config directory. Will return empty map of configuration in case of file not found.

Flatten Configuration

App env will read JSON and transform into 1 level map by joining key with dot (.). For example, following JSON

{
    "app":{
        "version": 1,
        "name": "demo",
        "meta": {
            "something": "value"
        }
    },
    "active": true
}

will transform to key-value map

"app.version" = 1
"app.name" = "demo"
"app.meta.something" = "value"
"active" = true

active environment overwrite

In case of active environment is not default, App env will trying to read config-{activeEnv}.json under config directory. In case of file not found, will fallback to use configuration from config.json.

If config-{activeEnv}.json found, will merge configuration with config.json by take value from active env configuration as priority.

Ex.
// config.json
{
    "app":{
        "version": 1,
        "name": "demo",
        "meta": {
            "something": "value"
        }
    },
    "active": true
}

// config-{activeEnv}.json
{
    "app":{
        "version": 2
    },
    "active": true
    "provider": "internal"
}

// merge result
{
    "app":{
        "version": 2,
        "name": "demo",
        "meta": {
            "something": "value"
        }
    },
    "active": true
    "provider": "internal"
}

Environment variable overwrite

App env will try to read flatten configuration key, by replace dash (-) and dot (.) with underscore (_) and change all character to upper case. If no variable found will use active env config or default.

Ex.
app.version >> APP_VERSION
app.api.get-user >> APP_API_GET_USER

Usage

appEnv := NewAppEnv(os.DirFS("{desired directory}"))

Example

// in file main.go
...
app := appenv.NewAppEnv(os.DirFS("."))
fmt.Printf("env: %s, configDir:%s \n", app.ActiveEnv(), app.ConfigDir())
...

/*
run:
    go run main.go
result:
    env: default, configDir: ./resources/

run:
    go run main.go --env=test
result:
    env: test, configDir: ./resources/
    
run:
    go run main.go --env=test --configDir=./cfg
result:
    env: test, configDir: ./cfg
    
run:
    GO_APP_ACTIVE_ENV=local GO_APP_CONFIG_DIR=./tmp go run main.go --env=test --configDir=./cfg
result:
    env: local, configDir: ./tmp
*/

go-app-env's People

Contributors

natepboat avatar

Watchers

 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.