goconfig

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2023 License: Apache-2.0 Imports: 10 Imported by: 4

README

goconfig

Build Status Go Report Card GoDoc Coverage Status

Golang config initialization module which provides simple functionality for the loading of configs based on structures description.

Example

config.go
package config

type serverConfig struct {
	HTTP struct {
		Listen       string        `default:":8080"  json:"listen" yaml:"listen" cli:"http-listen" env:"SERVER_HTTP_LISTEN"`
		ReadTimeout  time.Duration `default:"120s"   json:"read_timeout" yaml:"read_timeout" env:"SERVER_HTTP_READ_TIMEOUT"`
		WriteTimeout time.Duration `default:"120s"   json:"write_timeout" yaml:"write_timeout" env:"SERVER_HTTP_WRITE_TIMEOUT"`
	}
	GRPC struct {
		Listen  string        `default:"tcp://:8081" json:"listen" yaml:"listen" cli:"grpc-listen" env:"SERVER_GRPC_LISTEN"`
		Timeout time.Duration `default:"120s"        json:"timeout" yaml:"timeout" env:"SERVER_GRPC_TIMEOUT"`
	}
	Profile struct {
		Mode   string `json:"mode" yaml:"mode" default:"" env:"SERVER_PROFILE_MODE"`
		Listen string `json:"listen" yaml:"listen" default:"" env:"SERVER_PROFILE_LISTEN"`
	}
}

type ConfigType struct {
	ServiceName    string `json:"service_name" yaml:"service_name" env:"SERVICE_NAME" default:"disk"`
	DatacenterName string `json:"datacenter_name" yaml:"datacenter_name" env:"DC_NAME" default:"??"`
	Hostname       string `json:"hostname" yaml:"hostname" env:"HOSTNAME" default:""`
	Hostcode       string `json:"hostcode" yaml:"hostcode" env:"HOSTCODE" default:""`

	LogAddr  string `default:"" env:"LOG_ADDR"`
	LogLevel string `default:"debug" env:"LOG_LEVEL"`

	Server serverConfig `json:"server" yaml:"server"`
}

var Config ConfigType
main.go
package main

import (
  configLoader "github/demdxx/goconfig"
  "config"
)

func init() {
  if err := configLoader.Load(&config.Config); err != nil {
    panic(err)
  }
}

func main() {
  // ...
}

Dependencies

TODO

  • Add support of environment prefixes

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(cfg config) (err error)

Load data from file

Types

This section is empty.

Jump to

Keyboard shortcuts

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