zaal

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 29, 2025 License: MIT Imports: 14 Imported by: 9

README

Zaal

Go Reference

Named after Zāl (زال), the legendary Persian hero and father of Rostam in Ferdowsi's Shahnameh.

Overview

Zaal is a robust configuration management package for Go applications that combines the power of CUE with environment variable handling. It allows you to:

  1. Load structured configuration from CUE files
  2. Override configuration values with environment variables
  3. Access your configuration in a type-safe manner

⚠️ Development Status

This package is in heavy development and is not ready for production use yet.

Features and APIs may change significantly before the first stable release.

Installation

go get github.com/47monad/zaal

Basic Usage

package main

import (
 "fmt"
 "log"

 "github.com/your-repo/zaal"
)

func main() {
 // Load configuration from CUE file
 var cfg zaal.Config
 
 // Option 1: Load config file directly
 if err := zaal.Build("config.cue", ".env"); err != nil {
  log.Fatalf("Failed to load config: %v", err)
 }
 
 // Use your configuration
 fmt.Printf("Application: %s v%s\n", cfg.Name, cfg.Version)
 fmt.Printf("Environment: %s\n", cfg.Env)
 
 if cfg.Mongodb != nil {
  fmt.Printf("MongoDB URI: %s\n", cfg.Mongodb.URI)
 }
}

Configuration Structure

Zaal supports a flexible configuration structure with nested objects, optional components, and automatic environment variable binding:

type Config struct {
 Name       string            `json:"name"`
 Title      string            `json:"title"`
 Version    string            `json:"version"`
 Env        string            `json:"env" env:"env"`
 Mode       string            `json:"mode" env:"mode"`
 Host       string            `json:"host" env:"host"`
 Logging    LoggingConfig     `json:"logging"`
 Mongodb    *MongodbConfig    `json:"mongodb,omitempty"`
 RabbiMQ    *RabbitMQConfig   `json:"rabbitmq,omitempty"`
 Prometheus *PrometheusConfig `json:"prometheus,omitempty"`
 GRPC       *GRPCConfig       `json:"grpc,omitempty"`
 HTTP       *HTTPConfig       `json:"http,omitempty"`
}

Environment Variable Binding

Zaal automatically binds environment variables to your configuration based on the env struct tags:

// Config struct field with env tag
Env string `json:"env" env:"env"`

// Environment variable: ENV="production"
// Result: cfg.Env == "production"

For nested fields, environment variables are bound in a flattened structure:

// Config struct
type Config struct {
 Mongodb *MongodbConfig `json:"mongodb,omitempty"`
}

// MongodbConfig struct
type MongodbConfig struct {
 URI string `json:"uri" env:"mongodb_uri"`
}

// Environment variable: MONGODB_URI="mongodb://localhost:27017"
// Result: cfg.Mongodb.URI == "mongodb://localhost:27017"

Features

  • Type Safety: Strongly typed configuration with Go structs
  • Validation: CUE schema validation ensures your configuration is correct
  • Environment Overlay: Override configuration with environment variables
  • Optional Components: Use pointer fields for optional configuration sections
  • Flexible Loading: Load from files, environment, or both

Roadmap

  • Comprehensive documentation
  • Schema validation using CUE
  • Configuration merging from multiple sources
  • Command-line argument support
  • Secrets management integration
  • Hot reloading support

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadEnvFile

func LoadEnvFile(path string) error

func LoadEnvVars

func LoadEnvVars(cfg *Config) error

func WriteToJson

func WriteToJson(c *Config, outputPath string) error

Types

type Config

type Config struct {
	Name       string            `json:"name"`
	Title      string            `json:"title"`
	Version    string            `json:"version"`
	Env        string            `json:"env" env:"env"`
	Mode       string            `json:"mode" env:"mode"`
	Host       string            `json:"host" env:"host"`
	Logging    LoggingConfig     `json:"logging"`
	Mongodb    *MongodbConfig    `json:"mongodb,omitempty"`
	Etcd       *EtcdConfig       `json:"etcd,omitempty"`
	RabbiMQ    *RabbitMQConfig   `json:"rabbitmq,omitempty"`
	Prometheus *PrometheusConfig `json:"prometheus,omitempty"`
	GRPC       *GRPCConfig       `json:"grpc,omitempty"`
	HTTP       *HTTPConfig       `json:"http,omitempty"`
}

func Build

func Build(configPath, envPath string) (*Config, error)

type EtcdConfig

type EtcdConfig struct {
	Endpoints string `json:"endpoints" env:"etcd_endpoints"`
	Username  string `json:"username" env:"etcd_username"`
	Password  string `json:"password" env:"etcd_password"`
	Timeout   int    `json:"timeout" env:"etcd_timeout"`
}

type GRPCClientConfig

type GRPCClientConfig struct {
	Address string `json:"address" env:"grpc_client_address"`
}

type GRPCConfig

type GRPCConfig struct {
	Clients map[string]GRPCClientConfig `json:"clients"`
	Servers map[string]GRPCServerConfig `json:"servers"`
}

type GRPCFeatures

type GRPCFeatures struct {
	Reflection  bool `json:"reflection"`
	HealthCheck bool `json:"healthCheck"`
	Logging     bool `json:"logging"`
}

type GRPCServerConfig

type GRPCServerConfig struct {
	Port     int          `json:"port" env:"grpc_port"`
	Features GRPCFeatures `json:"features"`
}

type HTTPConfig

type HTTPConfig struct {
	Servers map[string]HTTPServerConfig `json:"servers"`
}

type HTTPServerConfig

type HTTPServerConfig struct {
	Port int `json:"port" env:"http_port"`
}

type LoggingConfig

type LoggingConfig struct {
	Level string `json:"level" env:"log_level"`
}

type MongodbConfig

type MongodbConfig struct {
	URI      string         `json:"uri" env:"mongodb_uri"`
	Username string         `json:"username" env:"mongodb_username"`
	Password string         `json:"password" env:"mongodb_password"`
	DbName   string         `json:"dbName" env:"mongodb_dbname"`
	Hosts    []string       `json:"hosts"`
	Options  MongodbOptions `json:"options"`
}

type MongodbOptions

type MongodbOptions struct {
	ReplicaSet string `json:"replicaSet"`
}

type PrometheusConfig

type PrometheusConfig struct {
	GRPCMetrics bool `json:"grpcMetrics"`
}

type RabbitMQConfig

type RabbitMQConfig struct {
	URI string `json:"uri" env:"rabbitmq_uri"`
}

Jump to

Keyboard shortcuts

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