goconfig

package module
v0.4.4 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

GoConfig

GoConfig is a Go package that enables developers to seamlessly load configurations from Spring Boot-style config servers. It simplifies the process of managing environment-specific configurations for Go applications.


Installation

Install the package using go get:

go get github.com/oodinga/goconfig

Getting Started

Required Environment Variables

GoConfig requires the following environment variables to be set for proper functionality:

  • app.name – Name of your application.
  • app.config.profiles.active – Comma separated list of active profiles (e.g., dev, prod, etc.).
  • app.config.server.url – URL of the configuration server.
  • app.config.optional – Whether loading configurations is optional (true or false).

These variables can be set in two ways:

  1. Directly as environment variables.
  2. Using a .env file.

Setting Environment Variables

Set the environment variables according to your operating system.

Example (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 a .env File

Alternatively, you can store environment variables in a .env file in the root of your Go application. GoConfig extends godotenv for loading .env files.

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

Note: Customize these values based on your application's configuration on the config server.


Loading Configuration in Your Go Application
Standard Import
package main

import (
    "log"
    "os"

    config "github.com/oodinga/goconfig"
)

func main() {
    config.Load()
    log.Println("Service port:", os.Getenv("server.port"))
}
Auto-Loading Configuration

For a simplified approach, import the autoload package. This eliminates the need for explicitly calling config.Load():

package main

import (
    "log"
    "os"

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

func main() {
    log.Println("Service port:", os.Getenv("server.port"))
}

GoConfig will automatically load the configuration from the server and set them as environment variables for your application.


Example Configuration File

When using a Spring Boot configuration server, configurations can be stored in a database or a Git repository. A sample application.yaml file might look like this:

application.yaml:
server:
    port: 8080
db:
    username: user
    password: ******
    url: localhost:3306

This configuration file can be hosted on your Spring Boot config server. It includes values such as the server port and database connection details.

Accessing Configuration in Go

Once the config server is set up, you can access the loaded configuration values in your Go application like this:

package main

import (
    "log"
    "os"

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

func main() {
    log.Println("Service port:", os.Getenv("server.port"))
    log.Println("Database username:", os.Getenv("db.username"))
    log.Println("Database URL:", os.Getenv("db.url"))
}

This code will retrieve and print configuration values that have been loaded from the Spring Boot config server and set as environment variables.

The output will display the values retrieved from the configuration server, such as:

Service port: 8080
Database username: user
Database URL: localhost:3306

This allows your Go application to dynamically load configuration values without the need to manually set them within your code.


Additional Resources

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()

The Load function loads configs from the remote config server. It should be called as early as possible in your application. The call is also done by the autoload package. This is to allow ease of use.

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