conf

package module
v0.0.0-...-7c6ad56 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2024 License: MIT Imports: 9 Imported by: 0

README

go-conf

A straightforward, distributed, file-based configuration solution for Go.

Install

go get -u github.com/acrazing/go-conf

Usage

  1. In any module, register config node and read it with conf.Registry:

    package my
    
    import conf "github.com/acrazing/go-conf"
    
    type Config struct {
        User string `json:"user"`
    }
    
    func init() {
        conf.Register[Config]("my.path")
    }
    
    func New(r *conf.Registry) {
        config, err := conf.Get[Config](r)
        if err != nil {
            panic(err)
        }
        println(config.User)
    }
    
  2. Load configuration file and get conf registry in your app entry:

    package main
    
    import conf "github.com/acrazing/go-conf"
    
    func main() {
        r, err := conf.Load("config.json")
        if err != nil {
            panic(err.Error())
        }
        my.New(r)
    }
    
  3. Helper: print default config file structure:

    package main
    
    import conf "github.com/acrazing/go-conf"
    import "encoding/json"
    
    func main() {
        c := conf.Default()
        jv, _ := json.MarshalIndent(c, "", "  ")
        println(string(jv))
    }
    

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Default

func Default() any

Default returns registered config nodes default value structure

Example
package main

import (
	"encoding/json"
	"github.com/acrazing/go-conf"
)

func main() {
	type ServerConfig struct {
		Host string `json:"host"`
		Port int    `json:"port"`
	}
	// Register first
	conf.Register[ServerConfig]("server")

	d := conf.Default()
	txt, _ := json.MarshalIndent(d, "", "  ")
	println(txt)
}

func Get

func Get[T any](registry *Registry) (*T, error)

Get a configuration node from registry

Example
package main

import (
	"github.com/acrazing/go-conf"
)

func main() {
	type ServerConfig struct {
		Host string `json:"host"`
		Port int    `json:"port"`
	}
	// Register first
	conf.Register[ServerConfig]("server")

	// Init registry
	r, err := conf.Load("config.json")
	if err != nil {
		panic(err.Error())
	}

	// Get config from registry
	serverConfig, err := conf.Get[ServerConfig](r)
	if err != nil {
		panic(err)
	}
	println(serverConfig.Host, serverConfig.Port)
}

func Register

func Register[T any](path string)

Register is used for register a struct to a path in a config file. It should be called in init function.

Types

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry is the root config registry

func Load

func Load(filename string) (*Registry, error)

Load config from a file

func New

func New(data any) *Registry

New create a registry with specified config data

Jump to

Keyboard shortcuts

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