goconfig

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

README

Go Reference

ConfigClient

Config client is a go package that allows go developers to use spring-boot like config client to load configs exposed via config servers.

Installation

go get github.com/oodinga/goconfig

Usage

The following env variables are required when using configClient

app.name
app.config.profiles.active
app.config.server.url
app.config.optional

These can be set in two ways.

  1. Set environment variables where you are running your application
  2. Add .env file in the root of your go service
Setting env variables

Follow instructions for setting ENVIROMENT_VARIABLES for your OS.

Example For linux/MacOs

export app.name="example-service"
export app.config.profiles.active="dev"
export app.config.server.url="https://localhost:8080"
export app.config.optional="true"
Using .env file

Config client variables can also be loaded from .env file. This is an extension of godotenv

In your .env file set the following values.

app.name="example-service"
app.config.profiles.active="dev"
app.config.server.url="https://localhost:8080"
app.config.optional="true"

NOTE: Set values according to your application setting on your config server.

Once these variable are set, all you need to do is to import config client as shown.

package main

import (
    "log"
    "os"

    config "github.com/oodinga/goconfig"
)

func main(){
    config.Load()
    log.Print("Service port: ", os.GetEnv("server.port"))
}

This can be simplified by importing the autoload package as shown below.

import (
    "log"
    "os"

    _ "github.com/oodinga/goconfig/autoload"
)

func main(){
    log.Print("Service port: ", os.GetEnv("server.port"))
}

ConfigClient will autoload the configs from the set config server and set them as environment variables to be used by your application.

Example config file

When using sprin-boot config server, You can use a db or git as the source of the configuration. A sample yaml file can be set as below.

application.yaml

server:
    port: 8080
db:
    username: name
    password: ******
    url: localhost:3600

To use these configs Using this in your go app.

package main

import (
    "log"
    "os"

    _ "github.com/oodinga/goconfig/autoload"
)

func main(){
    log.Print("Service port: ", os.GetEnv("server.port"))
    log.Print("Database name: ", os.GetEnv("db.name"))
    log.Print("Database url: ", os.GetEnv("db.url"))
}

This should log your values to the terminal

Refer to spring documentation on how to set a spring config server.

Documentation

Overview

GoConfig is inspired by sprin-boot config module.It allows developers to use external configs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load()

Types

type Config

type Config struct {
	Name            string           `json:"name"`
	Profiles        []string         `json:"profiles"`
	Label           interface{}      `json:"label"`
	Version         string           `json:"version"`
	State           interface{}      `json:"state"`
	PropertySources []PropertySource `json:"propertySources"`
}

Config represents the configuration object

func UnmarshalConfig

func UnmarshalConfig(data []byte) (*Config, error)

Reds bytes and Unmarshals into Config object. The service makes use of json.Unmarshal package.

type ConfigSettings

type ConfigSettings struct {
	Profiles  []string
	ServerURL string
	AppName   string
	Optional  bool
}

type PropertySource

type PropertySource struct {
	Name   string                 `json:"name"`
	Source map[string]interface{} `json:"source"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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