go-envschema

module
v0.0.0-...-fe92b25 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 18, 2019 License: MIT

README

Build Status MIT License GoDoc

go-envschema

Validate the runtime environment against given JSON schema.

Synopsis

import (
	"github.com/aereal/go-envschema/loader"
	"github.com/xeipuuv/gojsonschema"
)

type config struct {
	Addr string `json:"LISTEN_ADDR"`
}

func run() error {
	// LISTEN_ADDR=localhost:8000

	loader, err := loader.New(gojsonschema.NewReferenceLoader("file://./config-schema.json"))
	if err != nil {
		return err
	}
	cfg := config{}
	if err := loader.Load(&cfg); err != nil {
		return err
	}
	// cfg.Addr // => "localhost:8000"

	return nil
}

Motivation

The Twelve-Factor app that is container-aware application pattern mainly written by Heroku says the twelve-factor app stores config in environment variables.

That pattern is almost alive to this days but that is lacking in validation system in comparison with common config file pattern.

So all we need is validating the environment variables against the schema and mapping it into runtime structure mechanism.

refs. https://this.aereal.org/entry/container-apps-and-json-schema (Blog entry in Japanese)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL