Skip to content

sower-proxy/feconf

Repository files navigation

feconf

Go Version License: MPL 2.0 Build Status Test Coverage Go Report Card Documentation

A flexible and comfortable, URI-based configuration management library for Go with real-time updates.

Features

  • Multi-protocol: File, HTTP, WebSocket, Redis, Kubernetes
  • Multi-format: JSON, YAML, INI, TOML, XML
  • Real-time updates: Subscribe to configuration changes
  • Type-safe: Strong struct mapping with mapstructure
  • Extensible: Plugin-based architecture
  • Production-ready: TLS, retries, connection pooling

Quick Start

package main

import (
    "fmt"
    "log"

    "github.com/sower-proxy/feconf"
    _ "github.com/sower-proxy/feconf/decoder/json"
    _ "github.com/sower-proxy/feconf/reader/file"
)

type Config struct {
    Host string `json:"host"`
    Port int    `json:"port"`
}

func main() {
    loader := feconf.New[Config]("file://./config.json")
    defer loader.Close()

    config, err := loader.Load()
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Server: %s:%d\n", config.Host, config.Port)
}

Supported Protocols

// File system
loader := feconf.New[Config]("file:///path/to/config.yaml")

// HTTP/HTTPS
loader := feconf.New[Config]("https://api.example.com/config.json")

// Redis
loader := feconf.New[Config]("redis://localhost:6379/config-key")

// WebSocket
loader := feconf.New[Config]("wss://realtime.example.com/config")

// Kubernetes
loader := feconf.New[Config]("k8s://configmap/default/app-config")

Supported Formats

  • JSON: .json or content-type=application/json
  • YAML: .yaml, .yml or content-type=application/yaml
  • INI: .ini or content-type=text/ini
  • TOML: .toml or content-type=application/toml
  • XML: .xml or content-type=application/xml

Real-time Updates

eventChan, _ := loader.Subscribe()
for event := range eventChan {
    if event.IsValid() {
        fmt.Printf("Config updated: %v\n", event.Config)
    }
}

Command-line Flags

// Read from -config flag with fallback
loader := feconf.NewWithFlags[Config]("file://./default-config.json")

Architecture

The library follows a modular plugin-based architecture:

┌─────────────────┐    ┌──────────────────┐    ┌─────────────────┐
│   Application   │    │   Configuration  │    │   Decoder       │
│                 │───▶│   Loader (feconf)│───▶│   (json/yaml/   │
│   Your Code     │    │                  │    │    ini/etc)     │
└─────────────────┘    └──────────────────┘    └─────────────────┘
                                │
                                ▼
                       ┌──────────────────┐
                       │   Reader         │
                       │   (file/http/    │
                       │   redis/ws)      │
                       └──────────────────┘

Examples

See the examples/ directory for complete working examples:

  • file-json/ - Basic file-based configuration
  • http-yaml/ - HTTP with authentication
  • redis-ini/ - Redis configuration
  • flags/ - Command-line flag integration
  • ws-xml/ - WebSocket real-time updates
  • k8s-yaml/ - Kubernetes ConfigMap/Secret

Run examples:

cd examples/file-json && go run .
cd examples/http-yaml && go run .

Advanced Configuration

loader := feconf.New[Config](uri)

// Custom parser settings
loader.ParserConf.TagName = "config"
loader.ParserConf.ErrorUnused = true

// Custom decode hooks
loader.ParserConf.DecodeHook = mapstructure.ComposeDecodeHookFunc(
    feconf.HookFuncDefault(),
    feconf.HookFuncEnvRender(),
    feconf.HookFuncStringToBool(),
)

URI Parameters

Common parameters:

  • content-type - Override format detection
  • timeout - Request/operation timeout
  • retry_attempts - Number of retries

Protocol-specific parameters available for HTTP, Redis, and WebSocket connections.

Installation

go get github.com/sower-proxy/feconf

License

Mozilla Public License 2.0 - See LICENSE for details.

About

Flag and Environment Configuration comfortable loader

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages