structviewer

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2024 License: MPL-2.0 Imports: 10 Imported by: 0

README

struct_viewer

The struct_viewer package is a Go library designed to simplify the management and visualization of application configurations using Go structs and environment variables. It allows you to define your configuration as a struct, populate it with values from environment variables, and expose both the configuration and its corresponding environment variables via HTTP endpoints for easy inspection and debugging.

Features

  • Parse struct-based configurations.
  • Expose configuration fields and environment variables as JSON.
  • Simple HTTP handlers to expose config and envs.
  • Secure: Automatically obfuscate sensitive data.

Installation

To install the struct_viewer package, use the go get command:

go get -u github.com/TykTechnologies/struct-viewer

Usage

Below is a simple example of how to use struct_viewer in your application:

package main

import (
	"log"
	"net/http"

	"github.com/TykTechnologies/structviewer"
)

type Config struct {
	
	Field1       string `json:"field1"`
	Field2       int    `json:"field2"`

	// This field can be obfuscated with the structviewer tag
	Field3secret string `json:"field3_secret" structviewer:"obfuscate"` 
}

func main() {

	  // Define configuration structure
  	config := &structviewer.Config{
               Object: &YourConfigStruct{
   		       Field1: "value1",
  		       Field2: 123,
   	       },

        }

	// prefix is added to each env var
	v, err := structviewer.New(config, "PREFIX")
	if err != nil {
		panic(err)
	}

	http.HandleFunc("/config", v.JSONHandler)
	http.HandleFunc("/envs", v.EnvsHandler)
	err = http.ListenAndServe(":8080", nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}

This will expose two endpoints:

  • /config: returns a json representation of the config struct.
    • Example: curl -s 'http://localhost:8080/config' | jq .
  • /envs: returns a json representation of the environment variables of the config struct. You can pass query parameters env to the endpoints to retrieve specific config fields or specific environment variables.
    • Example:
      • curl -s 'http://localhost:8080/env' | jq .
      • curl -s 'http://localhost:8080/env?env=PREFIX_FIELD1' | jq .

HTTP Handlers

/config: Exposes the entire config object. /detailed-config: Exposes detailed configuration fields with descriptions. /envs: Exposes environment variables mapped from the config object.

Error Handling

The library provides several error types:

ErrNilConfig: Returned when a nil configuration struct is provided. ErrEmptyStruct: Returned when an empty struct is provided. ErrInvalidObjectType: Returned when the object is not of struct type.

Contributing

We welcome contributions! Please see our contribution guidelines for details.

Limitations

  • Only exported fields in Go struct are parsed
  • Only struct fields can have comments in them
  • Single file parsing
  • No obfuscation

Contributing

Contributing We welcome contributions! Please see our contribution guidelines for details. If you find any issues or have suggestions for improvement, please open an issue or submit a pull request on GitHub.

Documentation

Index

Constants

View Source
const (
	// JSONQueryKey is the query key for JSONHandler
	JSONQueryKey = "field"
	// EnvQueryKey is the query key for EnvsHandler
	EnvQueryKey = "env"
)
View Source
const StructViewerTag = "structviewer"

Variables

View Source
var (
	// ErrNilConfig is returned when the Config struct is nil.
	ErrNilConfig = errors.New("invalid Config structure provided")
	// ErrEmptyStruct is returned when config.Object is nil
	ErrEmptyStruct = errors.New("empty Struct in configuration")
	// ErrInvalidObjectType is returned when config.Object is not a struct.
	ErrInvalidObjectType = errors.New("invalid object type")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	// Object represents the config struct that is going to be parsed.
	Object interface{}

	// ParseComments decides parsing comments of given Object or not. If it is set to false,
	// the comment parser skips parsing comments of given Object.
	ParseComments bool

	// Path is the file path of the Object. Needed for comment parser.
	// Default value is "./config.go".
	Path string
}

Config represents configuration structure.

type EnvVar

type EnvVar struct {

	// ConfigField represents a JSON notation of the given struct fields.
	ConfigField string `json:"config_field,omitempty"`
	// Env represents an environment variable notation of the given struct fields.
	Env string `json:"env,omitempty"`
	// Description represents the comment of the given struct fields.
	Description string `json:"description,omitempty"`
	// Value represents the value of the given struct fields.
	Value interface{} `json:"value"`
	// Obfuscated represents whether the given struct field is obfuscated or not.
	// This is a pointer to a boolean value to distinguish between the zero value
	// and the actual value (because of the 'omitempty' tag).
	Obfuscated *bool `json:"obfuscated,omitempty"`
	// contains filtered or unexported fields
}

EnvVar is a key:value string struct for environment variables representation

func (EnvVar) String

func (ev EnvVar) String() string

String returns a key:value string from EnvVar

type Viewer

type Viewer struct {
	// contains filtered or unexported fields
}

Viewer is the pkg control structure where the prefix and env vars are stored.

func New

func New(config *Config, prefix string) (*Viewer, error)

New receives a configuration structure and a prefix and returns a Viewer struct to manipulate this library.

func (*Viewer) ConfigHandler

func (v *Viewer) ConfigHandler(rw http.ResponseWriter, r *http.Request)

ConfigHandler exposes the configuration struct as JSON fields

func (*Viewer) DetailedConfigHandler

func (v *Viewer) DetailedConfigHandler(rw http.ResponseWriter, r *http.Request)

DetailedConfigHandler exposes the detailed configuration struct as JSON fields

func (*Viewer) EnvNotation

func (v *Viewer) EnvNotation(jsonField string) EnvVar

EnvNotation takes JSON notation of a configuration field (e.g, 'listen_port') and returns EnvVar object of the given notation.

func (*Viewer) Envs

func (v *Viewer) Envs() []*EnvVar

Envs returns environment variables parsed by struct-viewer.

func (*Viewer) EnvsHandler

func (v *Viewer) EnvsHandler(rw http.ResponseWriter, r *http.Request)

EnvsHandler expose the environment variables of the configuration struct

func (*Viewer) JSONNotation

func (v *Viewer) JSONNotation(envVarNotation string) EnvVar

JSONNotation takes environment variable and returns EnvVars object of the given environment variable.

func (*Viewer) ParseEnvs

func (v *Viewer) ParseEnvs() []string

ParseEnvs parse Viewer config field, generating a string slice of prefix+key:value of each config field

Directories

Path Synopsis
internal
test command

Jump to

Keyboard shortcuts

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