config

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// RootServiceName is the name and location in the service directory of the service without a name.
	RootServiceName = "root"

	// RootOpenAPIName is the name and location of the OpenAPI service without a name.
	RootOpenAPIName = "openapi"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AppConfig

type AppConfig struct {
	Port                int           `yaml:"port" koanf:"port"`
	HomeURL             string        `yaml:"homeURL" koanf:"homeUrl"`
	ServiceURL          string        `yaml:"serviceURL" koanf:"serviceUrl"`
	SettingsURL         string        `yaml:"settingsURL" koanf:"settingsUrl"`
	ContextURL          string        `yaml:"contextUrl" koanf:"contextUrl"`
	ContextAreaPrefix   string        `yaml:"contextAreaPrefix" koanf:"contextAreaPrefix"`
	DisableUI           bool          `yaml:"disableUI" koanf:"disableUI"`
	DisableSwaggerUI    bool          `yaml:"disableSwaggerUI" koanf:"disableSwaggerUI"`
	Paths               *Paths        `koanf:"-"`
	CreateFileStructure bool          `koanf:"createFileStructure" yaml:"createFileStructure"`
	Editor              *EditorConfig `koanf:"editor" yaml:"editor"`
	HistoryDuration     time.Duration `koanf:"historyDuration" yaml:"historyDuration"`
}

AppConfig is the app configuration. Port is the port number to listen on. HomeURL is the URL for the UI home page. ServiceURL is the URL for the service and resources endpoints in the UI. SettingsURL is the URL for the settings endpoint in the UI. ContextURL is the URL for the context endpoint in the UI. ContextAreaPrefix sets sub-contexts for replacements in path, header or any other supported place.

for example: in-path:

user_id: "fake:ids.int8"

DisableUI is a flag whether to disable the UI. DisableSpec is a flag whether to disable the Swagger UI. Paths is the paths to various resource directories. CreateFileStructure is a flag whether to create the initial resources file structure: contexts, services, etc. It will also copy sample files from the samples directory into services. Default: true HistoryDuration is the duration to keep the history of requests in memory.

Plugins can access the history.

func NewDefaultAppConfig

func NewDefaultAppConfig(baseDir string) *AppConfig

NewDefaultAppConfig creates a new default app config in case the config file is missing, not found or any other error.

func (*AppConfig) IsValidPrefix

func (a *AppConfig) IsValidPrefix(prefix string) bool

IsValidPrefix returns true if the prefix is not a reserved URL.

type Config

type Config struct {
	App      *AppConfig                `koanf:"app" yaml:"app"`
	Services map[string]*ServiceConfig `koanf:"services" yaml:"services"`
	BaseDir  string                    `koanf:"-"`
	// contains filtered or unexported fields
}

Config is the main configuration struct. App is the app config. Services is a map of service name and the corresponding config. ServiceName is the first part of the path. e.g. /petstore/v1/pets -> petstore in case, there's no service name, the name "root" will be used.

func MustConfig

func MustConfig(baseDir string) *Config

MustConfig creates a new config from a YAML file path. In case it file does not exist or has incorrect YAML: - it creates a new default config

Koanf has a file watcher, but its easier to control the changes with a manual reload.

func NewConfigFromContent

func NewConfigFromContent(content []byte) (*Config, error)

NewConfigFromContent creates a new config from a YAML file content.

func NewDefaultConfig

func NewDefaultConfig(baseDir string) *Config

NewDefaultConfig creates a new default config in case the config file is missing, not found or any other error.

func (*Config) EnsureConfigValues

func (c *Config) EnsureConfigValues()

EnsureConfigValues ensures that all config values are set.

func (*Config) GetApp

func (c *Config) GetApp() *AppConfig

func (*Config) GetServiceConfig

func (c *Config) GetServiceConfig(service string) *ServiceConfig

GetServiceConfig returns the config for a service. If the service is not found, it returns a default config.

func (*Config) Reload

func (c *Config) Reload()

type EditorConfig

type EditorConfig struct {
	Theme    string `koanf:"theme" json:"theme" yaml:"theme"`
	FontSize int    `koanf:"fontSize" json:"fontSize" yaml:"fontSize"`
}

type HTTPStatusConfig

type HTTPStatusConfig struct {
	Exact int    `koanf:"exact" yaml:"exact"`
	Range string `koanf:"range" yaml:"range"`
}

func (*HTTPStatusConfig) Is

func (s *HTTPStatusConfig) Is(status int) bool

type HttpStatusFailOnConfig

type HttpStatusFailOnConfig []HTTPStatusConfig

func (HttpStatusFailOnConfig) Is

func (ss HttpStatusFailOnConfig) Is(status int) bool

type KeyValue

type KeyValue[K, V any] struct {
	Key   K
	Value V
}

type MiddlewareConfig added in v0.1.61

type MiddlewareConfig struct {
	BeforeHandler []string `koanf:"beforeHandler" yaml:"beforeHandler"`
	AfterHandler  []string `koanf:"afterHandler" yaml:"afterHandler"`
}

MiddlewareConfig defines the middleware configuration for a service. BeforeHandler is the list of middleware to run before the handler. AfterHandler is the list of middleware to run after the handler. If any of the middleware returns an error or response, the request will be stopped and the response will be returned.

type ParseConfig

type ParseConfig struct {
	MaxLevels          int  `koanf:"maxLevels" yaml:"maxLevels"`
	MaxRecursionLevels int  `koanf:"maxRecursionLevels" yaml:"maxRecursionLevels"`
	OnlyRequired       bool `koanf:"onlyRequired" yaml:"onlyRequired"`
}

ParseConfig defines the parsing configuration for a service. MaxLevels is the maximum level to parse. MaxRecursionLevels is the maximum level to parse recursively. 0 means no recursion: property will get nil value. OnlyRequired is a flag whether to include only required fields. If the spec contains deep references, this might significantly speed up parsing.

func NewParseConfig

func NewParseConfig() *ParseConfig

type Paths

type Paths struct {
	Base              string
	Resources         string
	Data              string
	Plugins           string
	Contexts          string
	Docs              string
	Samples           string
	Services          string
	ServicesOpenAPI   string
	ServicesFixedRoot string
	UI                string
	ConfigFile        string
}

Paths is a struct that holds all the paths used by the application.

func NewPaths

func NewPaths(baseDir string) *Paths

type ServiceCacheConfig

type ServiceCacheConfig struct {
	Schema      bool `koanf:"schema" yaml:"schema"`
	GetRequests bool `koanf:"getRequests" yaml:"getRequests"`
}

ServiceCacheConfig defines the cache configuration for a service. Avoids multiple schema parsing by caching the parsed schema. Default: true

func NewServiceCacheConfig

func NewServiceCacheConfig() *ServiceCacheConfig

NewServiceCacheConfig creates a new ServiceCacheConfig with default values.

type ServiceConfig

type ServiceConfig struct {
	Upstream    *UpstreamConfig          `koanf:"upstream" yaml:"upstream"`
	Latency     time.Duration            `koanf:"latency" yaml:"latency"`
	Latencies   map[string]time.Duration `koanf:"latencies" yaml:"latencies"`
	Errors      map[string]int           `koanf:"errors" yaml:"errors"`
	Contexts    []map[string]string      `koanf:"contexts" yaml:"contexts"`
	ParseConfig *ParseConfig             `koanf:"parseConfig" yaml:"parseConfig"`
	Validate    *ServiceValidateConfig   `koanf:"validate" yaml:"validate"`
	Middleware  *MiddlewareConfig        `koanf:"middleware" yaml:"middleware"`
	Cache       *ServiceCacheConfig      `koanf:"cache" yaml:"cache"`
	// contains filtered or unexported fields
}

ServiceConfig defines the configuration for a particular service. Latency is the single latency to add to the response. Latencies are the map of percentiles latencies. Latencies not used in the services API, only when endpoint queried directly:

p50: 20ms
p99: 100ms

If only 1 latency needed, set it with `p100` key. Errors is the error config with the percentiles as keys and http status codes as values. Contexts is the list of contexts to use for replacements. It is a map of context name defined either in the UI or filename without extension. You can refer to the name when building aliases. ParseConfig is the config for parsing the OpenAPI spec. Validate is the validation config. It is used to validate the request and/or response outside the Services API. Cache is the cache config.

func NewServiceConfig

func NewServiceConfig() *ServiceConfig

func (*ServiceConfig) GetError

func (s *ServiceConfig) GetError() int

GetError returns the error based on the percentiles:

random number is generated between 1 and 100 to simulate the percentile.

If no errors are defined, it returns 0.

func (*ServiceConfig) GetLatency

func (s *ServiceConfig) GetLatency() time.Duration

GetLatency returns the latency.

func (*ServiceConfig) ParseErrors

func (s *ServiceConfig) ParseErrors() []*KeyValue[int, int]

func (*ServiceConfig) ParseLatencies

func (s *ServiceConfig) ParseLatencies() []*KeyValue[int, time.Duration]

type ServiceValidateConfig

type ServiceValidateConfig struct {
	Request  bool `koanf:"request" yaml:"request"`
	Response bool `koanf:"response" yaml:"response"`
}

ServiceValidateConfig defines the validation configuration for a service. Request is a flag whether to validate the request. Default: true Response is a flag whether to validate the response. Default: false

func NewServiceValidateConfig

func NewServiceValidateConfig() *ServiceValidateConfig

type UpstreamConfig

type UpstreamConfig struct {
	URL     string                `koanf:"url" json:"url" yaml:"url"`
	Headers map[string]string     `koanf:"headers" json:"headers" yaml:"headers"`
	FailOn  *UpstreamFailOnConfig `koanf:"failOn" json:"failOn" yaml:"failOn"`
}

type UpstreamFailOnConfig

type UpstreamFailOnConfig struct {
	TimeOut    time.Duration          `koanf:"timeout" yaml:"timeout"`
	HTTPStatus HttpStatusFailOnConfig `koanf:"httpStatus" yaml:"httpStatus"`
}

Jump to

Keyboard shortcuts

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