config

package
v0.1.0-alpha.3 Latest Latest
Warning

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

Go to latest
Published: May 24, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package config loads, validates, and manages the configuration for the LRUCache.

Index

Constants

View Source
const (
	DefaultCapacity uint64 = 5
	DefaultHostname        = "localhost"
	DefaultPort     uint   = 8080
)

Variables

View Source
var (
	// AllMethods is a slice of all valid http methods to use in the cache configuration for busting.
	// 	{"GET", "HEAD", "POST", "PUT", "DELETE", "PATCH", "TRACE", "CONNECT", "OPTIONS"}
	AllMethods = []string{"GET", "HEAD", "POST", "PUT", "DELETE", "PATCH", "TRACE", "CONNECT", "OPTIONS"}

	// CacheableMethods is a slice of all cacheable http methods that can be used in the cache configuration for caching.
	// 	{"GET", "HEAD"}
	CacheableMethods = AllMethods[0:2]
)

Functions

This section is empty.

Types

type BustMap

type BustMap map[string]map[string][]string

BustMap represents a map of http methods with maps of endpoints with slices of patterns to match to cache entries to bust.

type CacheMap

type CacheMap map[string][]string

CacheMap represents a map of http methods with slices of endpoints to which requests should be cached.

type Config

type Config struct {
	// Default is 500, it represents the limit to how much data can be stored in the cache.
	Capacity uint64 `json:"capacity"`

	/*
		CapacityUnit represents the unit of measurement for the capacity.
		If omitted, the cache Capacity will be measured in entries.
		Set CapacityUnit to a string to use memory as the unit of measurement, e.g. "mb".
	*/
	CapacityUnit string `json:"capacityUnit"`

	// Default is "localhost", it represents the hostname where the server application can be accessed.
	Hostname string `json:"hostname"`

	//Default is 8080, it represents the port where the server application can be accessed. E.g.:
	Port uint `json:"port"`

	// ApiUrl is required, it represents the url of the API to which all requests are proxied and cached from.
	ApiUrl string `json:"apiUrl"`

	// LogFilePath is the path to an optional log file to use instead of stdout (terminal mode).
	LogFilePath string `json:"logFilePath"`

	Cache CacheMap `json:"cache"` // required (either GET or HEAD)

	/*
		Bust is a map of http methods with maps of endpoints with slices of patterns to match to cache entries to bust. E.g.:
			{
				"POST": {
					"/posts": [ "/posts" ]
				},
				"PUT": {
					"/posts": [ "^GET:/posts", "^HEAD:/posts" ],
					"/posts/:id": [ "/posts/:id" ]
				}
			}
	*/
	Bust BustMap `json:"bust"`
}

Config represents the configuration for the cache-me-ousside application.

func LoadJSON

func LoadJSON(configPath string) *Config

LoadJSON returns a Config created from unmarshaling the json5 file at configPath. It will also validate the props of the configuration and trim invalid http methods in the configuration as well as trailing slashes in the ApiUrl.

func New

func New() *Config

New returns a Config where Bust and Cache are initialized to empty BustMap and CacheMap respectively. This is done to avoid nil pointers when accessing the nested map properties.

func (Config) Address

func (conf Config) Address() string

Address returns the server address in the format hostname:port. This is where the server application can be accessed.

func (Config) CapacityParsed

func (conf Config) CapacityParsed() (size uint64, byteMode bool)

CapacityParsed returns size in bytes or entries as first value and a byteMode bool indicating if the capacity unit is bytes or entries. If CapacityUnit is a valid memory size string, the size is converted from the memory unit to bytes (e.g., mb -> bytes).

func (Config) CapacityString

func (conf Config) CapacityString() string

CapacityString returns a human-readable string representation of the cache capacity. If capacity is measured in entries, just return the number of entries. Otherwise return the amount of memory the cache can use with the unit appended.

func (Config) LogModeString

func (conf Config) LogModeString() string

LogModeString returns a human-readable string representation of how logging is configured. It will be either a log file path or "terminal mode"

func (*Config) RemoveInvalidMethods

func (conf *Config) RemoveInvalidMethods()

RemoveInvalidMethods removes all map keys that are not valid http methods and prints a warning to let the user know, that they might have mistyped the configuration.

func (Config) String

func (conf Config) String() string

String returns a human-readable table-formatted representation of the configuration.

func (*Config) TrimTrailingSlash

func (conf *Config) TrimTrailingSlash()

TrimTrailingSlash mutates the ApiUrl to remove any trailing slashes. This is useful so all specified endpoints and patterns can begin with a slash.

func (*Config) ValidateProps

func (conf *Config) ValidateProps() error

ValidateProps makes sure required configuration props are set and follow the correct format. TODO: use https://github.com/go-playground/validator.

Jump to

Keyboard shortcuts

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