Documentation ¶
Index ¶
- Constants
- Variables
- func AppConfigDir() string
- func AppDataDir() string
- func Exiting() bool
- func GetModuleID(instance any) string
- func GetModuleName(instance any) string
- func HomeDir() string
- func InstanceID() (uuid.UUID, error)
- func IsUnixNetwork(netw string) bool
- func JoinNetworkAddress(network, host, port string) string
- func Listen(network, addr string) (net.Listener, error)
- func ListenPacket(network, addr string) (net.PacketConn, error)
- func ListenQUIC(ln net.PacketConn, tlsConf *tls.Config, activeRequests *int64) (quic.EarlyListener, error)
- func ListenTimeout(network, addr string, keepalivePeriod time.Duration) (net.Listener, error)
- func ListenerUsage(network, addr string) int
- func Load(cfgJSON []byte, forceReload bool) error
- func Log() *zap.Logger
- func Modules() []string
- func PIDFile(filename string) error
- func ParseDuration(s string) (time.Duration, error)
- func ParseStructTag(tag string) (map[string]string, error)
- func RegisterModule(instance Module)
- func RegisterNetwork(network string, getListener ListenerFunc)
- func RemoveMetaFields(rawJSON []byte) []byte
- func Run(cfg *Config) error
- func SplitNetworkAddress(a string) (network, host, port string, err error)
- func Stop() error
- func StrictUnmarshalJSON(data []byte, v any) error
- func ToString(val any) string
- func TrapSignals()
- func Validate(cfg *Config) error
- func Version() (simple, full string)
- type APIError
- type AdminAccess
- type AdminConfig
- type AdminHandler
- type AdminHandlerFunc
- type AdminPermissions
- type AdminRoute
- type AdminRouter
- type App
- type BaseLog
- type CleanerUpper
- type Config
- type ConfigLoader
- type ConfigSettings
- type Constructor
- type Context
- func (ctx Context) App(name string) (any, error)
- func (ctx Context) AppIfConfigured(name string) (any, error)
- func (ctx Context) IdentityCredentials(logger *zap.Logger) ([]tls.Certificate, error)
- func (ctx Context) LoadModule(structPointer any, fieldName string) (any, error)
- func (ctx Context) LoadModuleByID(id string, rawMsg json.RawMessage) (any, error)
- func (ctx Context) Logger(module ...Module) *zap.Logger
- func (ctx Context) Module() Module
- func (ctx Context) Modules() []Module
- func (ctx *Context) OnCancel(f func())
- func (ctx Context) Storage() certmagic.Storage
- type CtxKey
- type CustomLog
- type Destructor
- type DiscardWriter
- type Duration
- type IdentityConfig
- type ListenerFunc
- type ListenerWrapper
- type LogSampling
- type Logging
- type Module
- type ModuleID
- type ModuleInfo
- type ModuleMap
- type NetworkAddress
- func (na NetworkAddress) At(portOffset uint) NetworkAddress
- func (na NetworkAddress) Expand() []NetworkAddress
- func (na NetworkAddress) IsUnixNetwork() bool
- func (na NetworkAddress) JoinHostPort(offset uint) string
- func (na NetworkAddress) Listen(ctx context.Context, portOffset uint, config net.ListenConfig) (any, error)
- func (na NetworkAddress) ListenAll(ctx context.Context, config net.ListenConfig) ([]any, error)
- func (na NetworkAddress) PortRangeSize() uint
- func (na NetworkAddress) String() string
- type Provisioner
- type RemoteAdmin
- type ReplacementFunc
- type Replacer
- func (r *Replacer) Delete(variable string)
- func (r *Replacer) Get(variable string) (any, bool)
- func (r *Replacer) GetString(variable string) (string, bool)
- func (r *Replacer) Map(mapFunc ReplacerFunc)
- func (r *Replacer) ReplaceAll(input, empty string) string
- func (r *Replacer) ReplaceFunc(input string, f ReplacementFunc) (string, error)
- func (r *Replacer) ReplaceKnown(input, empty string) string
- func (r *Replacer) ReplaceOrErr(input string, errOnEmpty, errOnUnknown bool) (string, error)
- func (r *Replacer) Set(variable string, value any)
- type ReplacerFunc
- type SinkLog
- type StderrWriter
- type StdoutWriter
- type StorageConverter
- type UsagePool
- func (up *UsagePool) Delete(key any) (deleted bool, err error)
- func (up *UsagePool) LoadOrNew(key any, construct Constructor) (value any, loaded bool, err error)
- func (up *UsagePool) LoadOrStore(key, val any) (value any, loaded bool)
- func (up *UsagePool) Range(f func(key, value any) bool)
- func (up *UsagePool) References(key any) (int, bool)
- type Validator
- type WriterOpener
Examples ¶
Constants ¶
const ( ExitCodeSuccess = iota ExitCodeFailedStartup ExitCodeForceQuit ExitCodeFailedQuit )
Exit codes. Generally, you should NOT automatically restart the process if the exit code is ExitCodeFailedStartup (1).
const DefaultLoggerName = "default"
const ImportPath = "github.com/bhaswanth88/caddy/v2"
ImportPath is the package import path for Caddy core. This identifier may be removed in the future.
Variables ¶
var ( // DefaultAdminListen is the address for the local admin // listener, if none is specified at startup. DefaultAdminListen = "localhost:2019" // DefaultRemoteAdminListen is the address for the remote // (TLS-authenticated) admin listener, if enabled and not // specified otherwise. DefaultRemoteAdminListen = ":2021" )
var ConfigAutosavePath = filepath.Join(AppConfigDir(), "autosave.json")
ConfigAutosavePath is the default path to which the last config will be persisted.
var CustomVersion string
CustomVersion is an optional string that overrides Caddy's reported version. It can be helpful when downstream packagers need to manually set Caddy's version. If no other version information is available, the short form version (see Version()) will be set to CustomVersion, and the full version will include CustomVersion at the beginning.
Set this variable during `go build` with `-ldflags`:
-ldflags '-X github.com/bhaswanth88/caddy/v2.CustomVersion=v2.6.2'
for example.
var DefaultStorage = &certmagic.FileStorage{Path: AppDataDir()}
DefaultStorage is Caddy's default storage module.
Functions ¶
func AppConfigDir ¶
func AppConfigDir() string
AppConfigDir returns the directory where to store user's config.
If XDG_CONFIG_HOME is set, it returns: $XDG_CONFIG_HOME/caddy. Otherwise, os.UserConfigDir() is used; if successful, it appends "Caddy" (Windows & Mac) or "caddy" (every other OS) to the path. If it returns an error, the fallback path "./caddy" is returned.
The config directory is not guaranteed to be different from AppDataDir().
Unlike os.UserConfigDir(), this function prefers the XDG_CONFIG_HOME env var on all platforms, not just Unix.
Ref: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
func AppDataDir ¶
func AppDataDir() string
AppDataDir returns a directory path that is suitable for storing application data on disk. It uses the environment for finding the best place to store data, and appends a "caddy" or "Caddy" (depending on OS and environment) subdirectory.
For a base directory path: If XDG_DATA_HOME is set, it returns: $XDG_DATA_HOME/caddy; otherwise, on Windows it returns: %AppData%/Caddy, on Mac: $HOME/Library/Application Support/Caddy, on Plan9: $home/lib/caddy, on Android: $HOME/caddy, and on everything else: $HOME/.local/share/caddy.
If a data directory cannot be determined, it returns "./caddy" (this is not ideal, and the environment should be fixed).
The data directory is not guaranteed to be different from AppConfigDir().
Ref: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
func Exiting ¶
func Exiting() bool
Exiting returns true if the process is exiting. EXPERIMENTAL API: subject to change or removal.
func GetModuleID ¶
GetModuleID returns a module's ID from an instance of its value. If the value is not a module, an empty string will be returned.
func GetModuleName ¶
GetModuleName returns a module's name (the last label of its ID) from an instance of its value. If the value is not a module, an empty string will be returned.
func HomeDir ¶
func HomeDir() string
HomeDir returns the best guess of the current user's home directory from environment variables. If unknown, "." (the current directory) is returned instead, except GOOS=android, which returns "/sdcard".
func InstanceID ¶
InstanceID returns the UUID for this instance, and generates one if it does not already exist. The UUID is stored in the local data directory, regardless of storage configuration, since each instance is intended to have its own unique ID.
func IsUnixNetwork ¶
IsUnixNetwork returns true if the netw is a unix network.
func JoinNetworkAddress ¶
JoinNetworkAddress combines network, host, and port into a single address string of the form accepted by ParseNetworkAddress(). For unix sockets, the network should be "unix" (or "unixgram" or "unixpacket") and the path to the socket should be given as the host parameter.
func Listen ¶
DEPRECATED: Use NetworkAddress.Listen instead. This function will likely be changed or removed in the future.
func ListenPacket ¶
func ListenPacket(network, addr string) (net.PacketConn, error)
DEPRECATED: Use NetworkAddress.Listen instead. This function will likely be changed or removed in the future.
func ListenQUIC ¶
func ListenQUIC(ln net.PacketConn, tlsConf *tls.Config, activeRequests *int64) (quic.EarlyListener, error)
ListenQUIC returns a quic.EarlyListener suitable for use in a Caddy module. The network will be transformed into a QUIC-compatible type (if unix, then unixgram will be used; otherwise, udp will be used).
NOTE: This API is EXPERIMENTAL and may be changed or removed.
TODO: See if we can find a more elegant solution closer to the new NetworkAddress.Listen API.
func ListenTimeout ¶
DEPRECATED: Use NetworkAddress.Listen instead. This function will likely be changed or removed in the future.
func ListenerUsage ¶
ListenerUsage returns the current usage count of the given listener address.
func Load ¶
Load loads the given config JSON and runs it only if it is different from the current config or forceReload is true.
func Modules ¶
func Modules() []string
Modules returns the names of all registered modules in ascending lexicographical order.
func PIDFile ¶
PIDFile writes a pidfile to the file at filename. It will get deleted before the process gracefully exits.
func ParseDuration ¶
ParseDuration parses a duration string, adding support for the "d" unit meaning number of days, where a day is assumed to be 24h. The maximum input string length is 1024.
func ParseStructTag ¶
ParseStructTag parses a caddy struct tag into its keys and values. It is very simple. The expected syntax is: `caddy:"key1=val1 key2=val2 ..."`
func RegisterModule ¶
func RegisterModule(instance Module)
RegisterModule registers a module by receiving a plain/empty value of the module. For registration to be properly recorded, this should be called in the init phase of runtime. Typically, the module package will do this as a side-effect of being imported. This function panics if the module's info is incomplete or invalid, or if the module is already registered.
func RegisterNetwork ¶
func RegisterNetwork(network string, getListener ListenerFunc)
RegisterNetwork registers a network type with Caddy so that if a listener is created for that network type, getListener will be invoked to get the listener. This should be called during init() and will panic if the network type is standard or reserved, or if it is already registered. EXPERIMENTAL and subject to change.
func RemoveMetaFields ¶
RemoveMetaFields removes meta fields like "@id" from a JSON message by using a simple regular expression. (An alternate way to do this would be to delete them from the raw, map[string]any representation as they are indexed, then iterate the index we made and add them back after encoding as JSON, but this is simpler.)
func SplitNetworkAddress ¶
SplitNetworkAddress splits a into its network, host, and port components. Note that port may be a port range (:X-Y), or omitted for unix sockets.
func Stop ¶
func Stop() error
Stop stops running the current configuration. It is the antithesis of Run(). This function will log any errors that occur during the stopping of individual apps and continue to stop the others. Stop should only be called if not replacing with a new config.
func StrictUnmarshalJSON ¶
StrictUnmarshalJSON is like json.Unmarshal but returns an error if any of the fields are unrecognized. Useful when decoding module configurations, where you want to be more sure they're correct.
func ToString ¶
ToString returns val as a string, as efficiently as possible. EXPERIMENTAL: may be changed or removed later.
func TrapSignals ¶
func TrapSignals()
TrapSignals create signal/interrupt handlers as best it can for the current OS. This is a rather invasive function to call in a Go program that captures signals already, so in that case it would be better to implement these handlers yourself.
func Version ¶
func Version() (simple, full string)
Version returns the Caddy version in a simple/short form, and a full version string. The short form will not have spaces and is intended for User-Agent strings and similar, but may be omitting valuable information. Note that Caddy must be compiled in a special way to properly embed complete version information. First this function tries to get the version from the embedded build info provided by go.mod dependencies; then it tries to get info from embedded VCS information, which requires having built Caddy from a git repository. If no version is available, this function returns "(devel)" because Go uses that, but for the simple form we change it to "unknown". If still no version is available (e.g. no VCS repo), then it will use CustomVersion; CustomVersion is always prepended to the full version string.
See relevant Go issues: https://github.com/golang/go/issues/29228 and https://github.com/golang/go/issues/50603.
This function is experimental and subject to change or removal.
Types ¶
type APIError ¶
type APIError struct { HTTPStatus int `json:"-"` Err error `json:"-"` Message string `json:"error"` }
APIError is a structured error that every API handler should return for consistency in logging and client responses. If Message is unset, then Err.Error() will be serialized in its place.
type AdminAccess ¶
type AdminAccess struct { // Base64-encoded DER certificates containing public keys to accept. // (The contents of PEM certificate blocks are base64-encoded DER.) // Any of these public keys can appear in any part of a verified chain. PublicKeys []string `json:"public_keys,omitempty"` // Limits what the associated identities are allowed to do. // If unspecified, all permissions are granted. Permissions []AdminPermissions `json:"permissions,omitempty"` // contains filtered or unexported fields }
AdminAccess specifies what permissions an identity or group of identities are granted.
type AdminConfig ¶
type AdminConfig struct { // If true, the admin endpoint will be completely disabled. // Note that this makes any runtime changes to the config // impossible, since the interface to do so is through the // admin endpoint. Disabled bool `json:"disabled,omitempty"` // The address to which the admin endpoint's listener should // bind itself. Can be any single network address that can be // parsed by Caddy. Accepts placeholders. // Default: the value of the `CADDY_ADMIN` environment variable, // or `localhost:2019` otherwise. Listen string `json:"listen,omitempty"` // If true, CORS headers will be emitted, and requests to the // API will be rejected if their `Host` and `Origin` headers // do not match the expected value(s). Use `origins` to // customize which origins/hosts are allowed. If `origins` is // not set, the listen address is the only value allowed by // default. Enforced only on local (plaintext) endpoint. EnforceOrigin bool `json:"enforce_origin,omitempty"` // The list of allowed origins/hosts for API requests. Only needed // if accessing the admin endpoint from a host different from the // socket's network interface or if `enforce_origin` is true. If not // set, the listener address will be the default value. If set but // empty, no origins will be allowed. Enforced only on local // (plaintext) endpoint. Origins []string `json:"origins,omitempty"` // Options pertaining to configuration management. Config *ConfigSettings `json:"config,omitempty"` // Options that establish this server's identity. Identity refers to // credentials which can be used to uniquely identify and authenticate // this server instance. This is required if remote administration is // enabled (but does not require remote administration to be enabled). // Default: no identity management. Identity *IdentityConfig `json:"identity,omitempty"` // Options pertaining to remote administration. By default, remote // administration is disabled. If enabled, identity management must // also be configured, as that is how the endpoint is secured. // See the neighboring "identity" object. // // EXPERIMENTAL: This feature is subject to change. Remote *RemoteAdmin `json:"remote,omitempty"` // contains filtered or unexported fields }
AdminConfig configures Caddy's API endpoint, which is used to manage Caddy while it is running.
type AdminHandler ¶
type AdminHandler interface {
ServeHTTP(http.ResponseWriter, *http.Request) error
}
AdminHandler is like http.Handler except ServeHTTP may return an error.
If any handler encounters an error, it should be returned for proper handling.
type AdminHandlerFunc ¶
type AdminHandlerFunc func(http.ResponseWriter, *http.Request) error
AdminHandlerFunc is a convenience type like http.HandlerFunc.
func (AdminHandlerFunc) ServeHTTP ¶
func (f AdminHandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) error
ServeHTTP implements the Handler interface.
type AdminPermissions ¶
type AdminPermissions struct { // The API paths allowed. Paths are simple prefix matches. // Any subpath of the specified paths will be allowed. Paths []string `json:"paths,omitempty"` // The HTTP methods allowed for the given paths. Methods []string `json:"methods,omitempty"` }
AdminPermissions specifies what kinds of requests are allowed to be made to the admin endpoint.
type AdminRoute ¶
type AdminRoute struct { Pattern string Handler AdminHandler }
AdminRoute represents a route for the admin endpoint.
type AdminRouter ¶
type AdminRouter interface {
Routes() []AdminRoute
}
AdminRouter is a type which can return routes for the admin API.
type BaseLog ¶
type BaseLog struct { // The module that writes out log entries for the sink. WriterRaw json.RawMessage `json:"writer,omitempty" caddy:"namespace=caddy.logging.writers inline_key=output"` // The encoder is how the log entries are formatted or encoded. EncoderRaw json.RawMessage `json:"encoder,omitempty" caddy:"namespace=caddy.logging.encoders inline_key=format"` // Level is the minimum level to emit, and is inclusive. // Possible levels: DEBUG, INFO, WARN, ERROR, PANIC, and FATAL Level string `json:"level,omitempty"` // Sampling configures log entry sampling. If enabled, // only some log entries will be emitted. This is useful // for improving performance on extremely high-pressure // servers. Sampling *LogSampling `json:"sampling,omitempty"` // contains filtered or unexported fields }
BaseLog contains the common logging parameters for logging.
type CleanerUpper ¶
type CleanerUpper interface {
Cleanup() error
}
CleanerUpper is implemented by modules which may have side-effects such as opened files, spawned goroutines, or allocated some sort of non-stack state when they were provisioned. This method should deallocate/cleanup those resources to prevent memory leaks. Cleanup should be fast and efficient. Cleanup should work even if Provision returns an error, to allow cleaning up from partial provisionings.
type Config ¶
type Config struct { Admin *AdminConfig `json:"admin,omitempty"` Logging *Logging `json:"logging,omitempty"` // StorageRaw is a storage module that defines how/where Caddy // stores assets (such as TLS certificates). The default storage // module is `caddy.storage.file_system` (the local file system), // and the default path // [depends on the OS and environment](/docs/conventions#data-directory). StorageRaw json.RawMessage `json:"storage,omitempty" caddy:"namespace=caddy.storage inline_key=module"` // AppsRaw are the apps that Caddy will load and run. The // app module name is the key, and the app's config is the // associated value. AppsRaw ModuleMap `json:"apps,omitempty" caddy:"namespace="` // contains filtered or unexported fields }
Config is the top (or beginning) of the Caddy configuration structure. Caddy config is expressed natively as a JSON document. If you prefer not to work with JSON directly, there are [many config adapters](/docs/config-adapters) available that can convert various inputs into Caddy JSON.
Many parts of this config are extensible through the use of Caddy modules. Fields which have a json.RawMessage type and which appear as dots (•••) in the online docs can be fulfilled by modules in a certain module namespace. The docs show which modules can be used in a given place.
Whenever a module is used, its name must be given either inline as part of the module, or as the key to the module's value. The docs will make it clear which to use.
Generally, all config settings are optional, as it is Caddy convention to have good, documented default values. If a parameter is required, the docs should say so.
Go programs which are directly building a Config struct value should take care to populate the JSON-encodable fields of the struct (i.e. the fields with `json` struct tags) if employing the module lifecycle (e.g. Provision method calls).
type ConfigLoader ¶
ConfigLoader is a type that can load a Caddy config. If the return value is non-nil, it must be valid Caddy JSON; if nil or with non-nil error, it is considered to be a no-op load and may be retried later.
type ConfigSettings ¶
type ConfigSettings struct { // Whether to keep a copy of the active config on disk. Default is true. // Note that "pulled" dynamic configs (using the neighboring "load" module) // are not persisted; only configs that are pushed to Caddy get persisted. Persist *bool `json:"persist,omitempty"` // Loads a new configuration. This is helpful if your configs are // managed elsewhere and you want Caddy to pull its config dynamically // when it starts. The pulled config completely replaces the current // one, just like any other config load. It is an error if a pulled // config is configured to pull another config without a load_delay, // as this creates a tight loop. // // EXPERIMENTAL: Subject to change. LoadRaw json.RawMessage `json:"load,omitempty" caddy:"namespace=caddy.config_loaders inline_key=module"` // The duration after which to load config. If set, config will be pulled // from the config loader after this duration. A delay is required if a // dynamically-loaded config is configured to load yet another config. To // load configs on a regular interval, ensure this value is set the same // on all loaded configs; it can also be variable if needed, and to stop // the loop, simply remove dynamic config loading from the next-loaded // config. // // EXPERIMENTAL: Subject to change. LoadDelay Duration `json:"load_delay,omitempty"` }
ConfigSettings configures the management of configuration.
type Constructor ¶
type Constructor func() (Destructor, error)
Constructor is a function that returns a new value that can destruct itself when it is no longer needed.
type Context ¶
Context is a type which defines the lifetime of modules that are loaded and provides access to the parent configuration that spawned the modules which are loaded. It should be used with care and wrapped with derivation functions from the standard context package only if you don't need the Caddy specific features. These contexts are canceled when the lifetime of the modules loaded from it is over.
Use NewContext() to get a valid value (but most modules will not actually need to do this).
func ActiveContext ¶
func ActiveContext() Context
ActiveContext returns the currently-active context. This function is experimental and might be changed or removed in the future.
func NewContext ¶
func NewContext(ctx Context) (Context, context.CancelFunc)
NewContext provides a new context derived from the given context ctx. Normally, you will not need to call this function unless you are loading modules which have a different lifespan than the ones for the context the module was provisioned with. Be sure to call the cancel func when the context is to be cleaned up so that modules which are loaded will be properly unloaded. See standard library context package's documentation.
func (Context) App ¶
App returns the configured app named name. If that app has not yet been loaded and provisioned, it will be immediately loaded and provisioned. If no app with that name is configured, a new empty one will be instantiated instead. (The app module must still be registered.) This must not be called during the Provision/Validate phase to reference a module's own host app (since the parent app module is still in the process of being provisioned, it is not yet ready).
func (Context) AppIfConfigured ¶
AppIfConfigured returns an app by its name if it has been configured. Can be called instead of App() to avoid instantiating an empty app when that's not desirable.
func (Context) IdentityCredentials ¶
IdentityCredentials returns this instance's configured, managed identity credentials that can be used in TLS client authentication.
func (Context) LoadModule ¶
LoadModule loads the Caddy module(s) from the specified field of the parent struct pointer and returns the loaded module(s). The struct pointer and its field name as a string are necessary so that reflection can be used to read the struct tag on the field to get the module namespace and inline module name key (if specified).
The field can be any one of the supported raw module types: json.RawMessage, []json.RawMessage, map[string]json.RawMessage, or []map[string]json.RawMessage. ModuleMap may be used in place of map[string]json.RawMessage. The return value's underlying type mirrors the input field's type:
json.RawMessage => any []json.RawMessage => []any [][]json.RawMessage => [][]any map[string]json.RawMessage => map[string]any []map[string]json.RawMessage => []map[string]any
The field must have a "caddy" struct tag in this format:
caddy:"key1=val1 key2=val2"
To load modules, a "namespace" key is required. For example, to load modules in the "http.handlers" namespace, you'd put: `namespace=http.handlers` in the Caddy struct tag.
The module name must also be available. If the field type is a map or slice of maps, then key is assumed to be the module name if an "inline_key" is NOT specified in the caddy struct tag. In this case, the module name does NOT need to be specified in-line with the module itself.
If not a map, or if inline_key is non-empty, then the module name must be embedded into the values, which must be objects; then there must be a key in those objects where its associated value is the module name. This is called the "inline key", meaning the key containing the module's name that is defined inline with the module itself. You must specify the inline key in a struct tag, along with the namespace:
caddy:"namespace=http.handlers inline_key=handler"
This will look for a key/value pair like `"handler": "..."` in the json.RawMessage in order to know the module name.
To make use of the loaded module(s) (the return value), you will probably want to type-assert each 'any' value(s) to the types that are useful to you and store them on the same struct. Storing them on the same struct makes for easy garbage collection when your host module is no longer needed.
Loaded modules have already been provisioned and validated. Upon returning successfully, this method clears the json.RawMessage(s) in the field since the raw JSON is no longer needed, and this allows the GC to free up memory.
Example ¶
// this whole first part is just setting up for the example; // note the struct tags - very important; we specify inline_key // because that is the only way to know the module name var ctx Context myStruct := &struct { // This godoc comment will appear in module documentation. GuestModuleRaw json.RawMessage `json:"guest_module,omitempty" caddy:"namespace=example inline_key=name"` // this is where the decoded module will be stored; in this // example, we pretend we need an io.Writer but it can be // any interface type that is useful to you guestModule io.Writer }{ GuestModuleRaw: json.RawMessage(`{"name":"module_name","foo":"bar"}`), } // if a guest module is provided, we can load it easily if myStruct.GuestModuleRaw != nil { mod, err := ctx.LoadModule(myStruct, "GuestModuleRaw") if err != nil { // you'd want to actually handle the error here // return fmt.Errorf("loading guest module: %v", err) } // mod contains the loaded and provisioned module, // it is now ready for us to use myStruct.guestModule = mod.(io.Writer) } // use myStruct.guestModule from now on
Output:
Example (Array) ¶
// this whole first part is just setting up for the example; // note the struct tags - very important; we specify inline_key // because that is the only way to know the module name var ctx Context myStruct := &struct { // This godoc comment will appear in module documentation. GuestModulesRaw []json.RawMessage `json:"guest_modules,omitempty" caddy:"namespace=example inline_key=name"` // this is where the decoded module will be stored; in this // example, we pretend we need an io.Writer but it can be // any interface type that is useful to you guestModules []io.Writer }{ GuestModulesRaw: []json.RawMessage{ json.RawMessage(`{"name":"module1_name","foo":"bar1"}`), json.RawMessage(`{"name":"module2_name","foo":"bar2"}`), }, } // since our input is []json.RawMessage, the output will be []any mods, err := ctx.LoadModule(myStruct, "GuestModulesRaw") if err != nil { // you'd want to actually handle the error here // return fmt.Errorf("loading guest modules: %v", err) } for _, mod := range mods.([]any) { myStruct.guestModules = append(myStruct.guestModules, mod.(io.Writer)) } // use myStruct.guestModules from now on
Output:
Example (Map) ¶
// this whole first part is just setting up for the example; // note the struct tags - very important; we don't specify // inline_key because the map key is the module name var ctx Context myStruct := &struct { // This godoc comment will appear in module documentation. GuestModulesRaw ModuleMap `json:"guest_modules,omitempty" caddy:"namespace=example"` // this is where the decoded module will be stored; in this // example, we pretend we need an io.Writer but it can be // any interface type that is useful to you guestModules map[string]io.Writer }{ GuestModulesRaw: ModuleMap{ "module1_name": json.RawMessage(`{"foo":"bar1"}`), "module2_name": json.RawMessage(`{"foo":"bar2"}`), }, } // since our input is map[string]json.RawMessage, the output will be map[string]any mods, err := ctx.LoadModule(myStruct, "GuestModulesRaw") if err != nil { // you'd want to actually handle the error here // return fmt.Errorf("loading guest modules: %v", err) } for modName, mod := range mods.(map[string]any) { myStruct.guestModules[modName] = mod.(io.Writer) } // use myStruct.guestModules from now on
Output:
func (Context) LoadModuleByID ¶
LoadModuleByID decodes rawMsg into a new instance of mod and returns the value. If mod.New is nil, an error is returned. If the module implements Validator or Provisioner interfaces, those methods are invoked to ensure the module is fully configured and valid before being used.
This is a lower-level method and will usually not be called directly by most modules. However, this method is useful when dynamically loading/unloading modules in their own context, like from embedded scripts, etc.
func (Context) Logger ¶
Logger returns a logger that is intended for use by the most recent module associated with the context. Callers should not pass in any arguments unless they want to associate with a different module; it panics if more than 1 value is passed in.
Originally, this method's signature was `Logger(mod Module)`, requiring that an instance of a Caddy module be passed in. However, that is no longer necessary, as the closest module most recently associated with the context will be automatically assumed. To prevent a sudden breaking change, this method's signature has been changed to be variadic, but we may remove the parameter altogether in the future. Callers should not pass in any argument. If there is valid need to specify a different module, please open an issue to discuss.
PARTIALLY DEPRECATED: The Logger(module) form is deprecated and may be removed in the future. Do not pass in any arguments.
func (Context) Module ¶
Module returns the current module, or the most recent one provisioned by the context.
func (Context) Modules ¶
Modules returns the lineage of modules that this context provisioned, with the most recent/current module being last in the list.
type CtxKey ¶
type CtxKey string
CtxKey is a value type for use with context.WithValue.
const ReplacerCtxKey CtxKey = "replacer"
ReplacerCtxKey is the context key for a replacer.
type CustomLog ¶
type CustomLog struct { BaseLog // Include defines the names of loggers to emit in this // log. For example, to include only logs emitted by the // admin API, you would include "admin.api". Include []string `json:"include,omitempty"` // Exclude defines the names of loggers that should be // skipped by this log. For example, to exclude only // HTTP access logs, you would exclude "http.log.access". Exclude []string `json:"exclude,omitempty"` }
CustomLog represents a custom logger configuration.
By default, a log will emit all log entries. Some entries will be skipped if sampling is enabled. Further, the Include and Exclude parameters define which loggers (by name) are allowed or rejected from emitting in this log. If both Include and Exclude are populated, their values must be mutually exclusive, and longer namespaces have priority. If neither are populated, all logs are emitted.
type Destructor ¶
type Destructor interface {
Destruct() error
}
Destructor is a value that can clean itself up when it is deallocated.
type DiscardWriter ¶
type DiscardWriter struct{}
DiscardWriter discards all writes.
func (DiscardWriter) CaddyModule ¶
func (DiscardWriter) CaddyModule() ModuleInfo
CaddyModule returns the Caddy module information.
func (DiscardWriter) OpenWriter ¶
func (DiscardWriter) OpenWriter() (io.WriteCloser, error)
OpenWriter returns io.Discard that can't be closed.
func (DiscardWriter) String ¶
func (DiscardWriter) String() string
func (DiscardWriter) WriterKey ¶
func (DiscardWriter) WriterKey() string
WriterKey returns a unique key representing discard.
type Duration ¶
Duration can be an integer or a string. An integer is interpreted as nanoseconds. If a string, it is a Go time.Duration value such as `300ms`, `1.5h`, or `2h45m`; valid units are `ns`, `us`/`µs`, `ms`, `s`, `m`, `h`, and `d`.
func (*Duration) UnmarshalJSON ¶
UnmarshalJSON satisfies json.Unmarshaler.
type IdentityConfig ¶
type IdentityConfig struct { // List of names or IP addresses which refer to this server. // Certificates will be obtained for these identifiers so // secure TLS connections can be made using them. Identifiers []string `json:"identifiers,omitempty"` // Issuers that can provide this admin endpoint its identity // certificate(s). Default: ACME issuers configured for // ZeroSSL and Let's Encrypt. Be sure to change this if you // require credentials for private identifiers. IssuersRaw []json.RawMessage `json:"issuers,omitempty" caddy:"namespace=tls.issuance inline_key=module"` // contains filtered or unexported fields }
IdentityConfig configures management of this server's identity. An identity consists of credentials that uniquely verify this instance; for example, TLS certificates (public + private key pairs).
type ListenerFunc ¶
type ListenerFunc func(ctx context.Context, network, addr string, cfg net.ListenConfig) (any, error)
ListenerFunc is a function that can return a listener given a network and address. The listeners must be capable of overlapping: with Caddy, new configs are loaded before old ones are unloaded, so listeners may overlap briefly if the configs both need the same listener. EXPERIMENTAL and subject to change.
type ListenerWrapper ¶
ListenerWrapper is a type that wraps a listener so it can modify the input listener's methods. Modules that implement this interface are found in the caddy.listeners namespace. Usually, to wrap a listener, you will define your own struct type that embeds the input listener, then implement your own methods that you want to wrap, calling the underlying listener's methods where appropriate.
type LogSampling ¶
type LogSampling struct { // The window over which to conduct sampling. Interval time.Duration `json:"interval,omitempty"` // Log this many entries within a given level and // message for each interval. First int `json:"first,omitempty"` // If more entries with the same level and message // are seen during the same interval, keep one in // this many entries until the end of the interval. Thereafter int `json:"thereafter,omitempty"` }
LogSampling configures log entry sampling.
type Logging ¶
type Logging struct { // Sink is the destination for all unstructured logs emitted // from Go's standard library logger. These logs are common // in dependencies that are not designed specifically for use // in Caddy. Because it is global and unstructured, the sink // lacks most advanced features and customizations. Sink *SinkLog `json:"sink,omitempty"` // Logs are your logs, keyed by an arbitrary name of your // choosing. The default log can be customized by defining // a log called "default". You can further define other logs // and filter what kinds of entries they accept. Logs map[string]*CustomLog `json:"logs,omitempty"` // contains filtered or unexported fields }
Logging facilitates logging within Caddy. The default log is called "default" and you can customize it. You can also define additional logs.
By default, all logs at INFO level and higher are written to standard error ("stderr" writer) in a human-readable format ("console" encoder if stdout is an interactive terminal, "json" encoder otherwise).
All defined logs accept all log entries by default, but you can filter by level and module/logger names. A logger's name is the same as the module's name, but a module may append to logger names for more specificity. For example, you can filter logs emitted only by HTTP handlers using the name "http.handlers", because all HTTP handler module names have that prefix.
Caddy logs (except the sink) are zero-allocation, so they are very high-performing in terms of memory and CPU time. Enabling sampling can further increase throughput on extremely high-load servers.
type Module ¶
type Module interface { // This method indicates that the type is a Caddy // module. The returned ModuleInfo must have both // a name and a constructor function. This method // must not have any side-effects. CaddyModule() ModuleInfo }
Module is a type that is used as a Caddy module. In addition to this interface, most modules will implement some interface expected by their host module in order to be useful. To learn which interface(s) to implement, see the documentation for the host module. At a bare minimum, this interface, when implemented, only provides the module's ID and constructor function.
Modules will often implement additional interfaces including Provisioner, Validator, and CleanerUpper. If a module implements these interfaces, their methods are called during the module's lifespan.
When a module is loaded by a host module, the following happens: 1) ModuleInfo.New() is called to get a new instance of the module. 2) The module's configuration is unmarshaled into that instance. 3) If the module is a Provisioner, the Provision() method is called. 4) If the module is a Validator, the Validate() method is called. 5) The module will probably be type-asserted from 'any' to some other, more useful interface expected by the host module. For example, HTTP handler modules are type-asserted as caddyhttp.MiddlewareHandler values. 6) When a module's containing Context is canceled, if it is a CleanerUpper, its Cleanup() method is called.
type ModuleID ¶
type ModuleID string
ModuleID is a string that uniquely identifies a Caddy module. A module ID is lightly structured. It consists of dot-separated labels which form a simple hierarchy from left to right. The last label is the module name, and the labels before that constitute the namespace (or scope).
Thus, a module ID has the form: <namespace>.<name>
An ID with no dot has the empty namespace, which is appropriate for app modules (these are "top-level" modules that Caddy core loads and runs).
Module IDs should be lowercase and use underscores (_) instead of spaces.
Examples of valid IDs: - http - http.handlers.file_server - caddy.logging.encoders.json
type ModuleInfo ¶
type ModuleInfo struct { // ID is the "full name" of the module. It // must be unique and properly namespaced. ID ModuleID // New returns a pointer to a new, empty // instance of the module's type. This // method must not have any side-effects, // and no other initialization should // occur within it. Any initialization // of the returned value should be done // in a Provision() method (see the // Provisioner interface). New func() Module }
ModuleInfo represents a registered Caddy module.
func GetModule ¶
func GetModule(name string) (ModuleInfo, error)
GetModule returns module information from its ID (full name).
func GetModules ¶
func GetModules(scope string) []ModuleInfo
GetModules returns all modules in the given scope/namespace. For example, a scope of "foo" returns modules named "foo.bar", "foo.loo", but not "bar", "foo.bar.loo", etc. An empty scope returns top-level modules, for example "foo" or "bar". Partial scopes are not matched (i.e. scope "foo.ba" does not match name "foo.bar").
Because modules are registered to a map under the hood, the returned slice will be sorted to keep it deterministic.
func (ModuleInfo) String ¶
func (mi ModuleInfo) String() string
type ModuleMap ¶
type ModuleMap map[string]json.RawMessage
ModuleMap is a map that can contain multiple modules, where the map key is the module's name. (The namespace is usually read from an associated field's struct tag.) Because the module's name is given as the key in a module map, the name does not have to be given in the json.RawMessage.
type NetworkAddress ¶
type NetworkAddress struct { // Should be a network value accepted by Go's net package or // by a plugin providing a listener for that network type. Network string // The "main" part of the network address is the host, which // often takes the form of a hostname, DNS name, IP address, // or socket path. Host string // For addresses that contain a port, ranges are given by // [StartPort, EndPort]; i.e. for a single port, StartPort // and EndPort are the same. For no port, they are 0. StartPort uint EndPort uint }
NetworkAddress represents one or more network addresses. It contains the individual components for a parsed network address of the form accepted by ParseNetworkAddress().
func ParseNetworkAddress ¶
func ParseNetworkAddress(addr string) (NetworkAddress, error)
ParseNetworkAddress parses addr into its individual components. The input string is expected to be of the form "network/host:port-range" where any part is optional. The default network, if unspecified, is tcp. Port ranges are inclusive.
Network addresses are distinct from URLs and do not use URL syntax.
func (NetworkAddress) At ¶
func (na NetworkAddress) At(portOffset uint) NetworkAddress
At returns a NetworkAddress with a port range of just 1 at the given port offset; i.e. a NetworkAddress that represents precisely 1 address only.
func (NetworkAddress) Expand ¶
func (na NetworkAddress) Expand() []NetworkAddress
Expand returns one NetworkAddress for each port in the port range.
This is EXPERIMENTAL and subject to change or removal.
func (NetworkAddress) IsUnixNetwork ¶
func (na NetworkAddress) IsUnixNetwork() bool
IsUnixNetwork returns true if na.Network is unix, unixgram, or unixpacket.
func (NetworkAddress) JoinHostPort ¶
func (na NetworkAddress) JoinHostPort(offset uint) string
JoinHostPort is like net.JoinHostPort, but where the port is StartPort + offset.
func (NetworkAddress) Listen ¶
func (na NetworkAddress) Listen(ctx context.Context, portOffset uint, config net.ListenConfig) (any, error)
Listen is similar to net.Listen, with a few differences:
Listen announces on the network address using the port calculated by adding portOffset to the start port. (For network types that do not use ports, the portOffset is ignored.)
The provided ListenConfig is used to create the listener. Its Control function, if set, may be wrapped by an internally-used Control function. The provided context may be used to cancel long operations early. The context is not used to close the listener after it has been created.
Caddy's listeners can overlap each other: multiple listeners may be created on the same socket at the same time. This is useful because during config changes, the new config is started while the old config is still running. How this is accomplished varies by platform and network type. For example, on Unix, SO_REUSEPORT is set except on Unix sockets, for which the file descriptor is duplicated and reused; on Windows, the close logic is virtualized using timeouts. Like normal listeners, be sure to Close() them when you are done.
This method returns any type, as the implementations of listeners for various network types are not interchangeable. The type of listener returned is switched on the network type. Stream-based networks ("tcp", "unix", "unixpacket", etc.) return a net.Listener; datagram-based networks ("udp", "unixgram", etc.) return a net.PacketConn; and so forth. The actual concrete types are not guaranteed to be standard, exported types (wrapping is necessary to provide graceful reloads).
Unix sockets will be unlinked before being created, to ensure we can bind to it even if the previous program using it exited uncleanly; it will also be unlinked upon a graceful exit (or when a new config does not use that socket).
TODO: Experimental API: subject to change or removal.
func (NetworkAddress) ListenAll ¶
func (na NetworkAddress) ListenAll(ctx context.Context, config net.ListenConfig) ([]any, error)
ListenAll calls Listen() for all addresses represented by this struct, i.e. all ports in the range. (If the address doesn't use ports or has 1 port only, then only 1 listener will be created.) It returns an error if any listener failed to bind, and closes any listeners opened up to that point.
TODO: Experimental API: subject to change or removal.
func (NetworkAddress) PortRangeSize ¶
func (na NetworkAddress) PortRangeSize() uint
PortRangeSize returns how many ports are in pa's port range. Port ranges are inclusive, so the size is the difference of start and end ports plus one.
func (NetworkAddress) String ¶
func (na NetworkAddress) String() string
String reconstructs the address string for human display. The output can be parsed by ParseNetworkAddress(). If the address is a unix socket, any non-zero port will be dropped.
type Provisioner ¶
Provisioner is implemented by modules which may need to perform some additional "setup" steps immediately after being loaded. Provisioning should be fast (imperceptible running time). If any side-effects result in the execution of this function (e.g. creating global state, any other allocations which require garbage collection, opening files, starting goroutines etc.), be sure to clean up properly by implementing the CleanerUpper interface to avoid leaking resources.
type RemoteAdmin ¶
type RemoteAdmin struct { // The address on which to start the secure listener. Accepts placeholders. // Default: :2021 Listen string `json:"listen,omitempty"` // List of access controls for this secure admin endpoint. // This configures TLS mutual authentication (i.e. authorized // client certificates), but also application-layer permissions // like which paths and methods each identity is authorized for. AccessControl []*AdminAccess `json:"access_control,omitempty"` }
RemoteAdmin enables and configures remote administration. If enabled, a secure listener enforcing mutual TLS authentication will be started on a different port from the standard plaintext admin server.
This endpoint is secured using identity management, which must be configured separately (because identity management does not depend on remote administration). See the admin/identity config struct.
EXPERIMENTAL: Subject to change.
type ReplacementFunc ¶
ReplacementFunc is a function that is called when a replacement is being performed. It receives the variable (i.e. placeholder name) and the value that will be the replacement, and returns the value that will actually be the replacement, or an error. Note that errors are sometimes ignored by replacers.
type Replacer ¶
type Replacer struct {
// contains filtered or unexported fields
}
Replacer can replace values in strings. A default/empty Replacer is not valid; use NewReplacer to make one.
func NewEmptyReplacer ¶
func NewEmptyReplacer() *Replacer
NewEmptyReplacer returns a new Replacer, without the global default replacements.
func (*Replacer) Get ¶
Get gets a value from the replacer. It returns the value and whether the variable was known.
func (*Replacer) GetString ¶
GetString is the same as Get, but coerces the value to a string representation as efficiently as possible.
func (*Replacer) Map ¶
func (r *Replacer) Map(mapFunc ReplacerFunc)
Map adds mapFunc to the list of value providers. mapFunc will be executed only at replace-time.
func (*Replacer) ReplaceAll ¶
ReplaceAll efficiently replaces placeholders in input with their values. All placeholders are replaced in the output whether they are recognized or not. Values that are empty string will be substituted with empty.
func (*Replacer) ReplaceFunc ¶
func (r *Replacer) ReplaceFunc(input string, f ReplacementFunc) (string, error)
ReplaceFunc is the same as ReplaceAll, but calls f for every replacement to be made, in case f wants to change or inspect the replacement.
func (*Replacer) ReplaceKnown ¶
ReplaceKnown is like ReplaceAll but only replaces placeholders that are known (recognized). Unrecognized placeholders will remain in the output.
func (*Replacer) ReplaceOrErr ¶
ReplaceOrErr is like ReplaceAll, but any placeholders that are empty or not recognized will cause an error to be returned.
type ReplacerFunc ¶
ReplacerFunc is a function that returns a replacement for the given key along with true if the function is able to service that key (even if the value is blank). If the function does not recognize the key, false should be returned.
type SinkLog ¶
type SinkLog struct {
BaseLog
}
SinkLog configures the default Go standard library global logger in the log package. This is necessary because module dependencies which are not built specifically for Caddy will use the standard logger. This is also known as the "sink" logger.
type StderrWriter ¶
type StderrWriter struct{}
StderrWriter writes logs to standard error.
func (StderrWriter) CaddyModule ¶
func (StderrWriter) CaddyModule() ModuleInfo
CaddyModule returns the Caddy module information.
func (StderrWriter) OpenWriter ¶
func (StderrWriter) OpenWriter() (io.WriteCloser, error)
OpenWriter returns os.Stderr that can't be closed.
func (StderrWriter) String ¶
func (StderrWriter) String() string
func (StderrWriter) WriterKey ¶
func (StderrWriter) WriterKey() string
WriterKey returns a unique key representing stderr.
type StdoutWriter ¶
type StdoutWriter struct{}
StdoutWriter writes logs to standard out.
func (StdoutWriter) CaddyModule ¶
func (StdoutWriter) CaddyModule() ModuleInfo
CaddyModule returns the Caddy module information.
func (StdoutWriter) OpenWriter ¶
func (StdoutWriter) OpenWriter() (io.WriteCloser, error)
OpenWriter returns os.Stdout that can't be closed.
func (StdoutWriter) String ¶
func (StdoutWriter) String() string
func (StdoutWriter) WriterKey ¶
func (StdoutWriter) WriterKey() string
WriterKey returns a unique key representing stdout.
type StorageConverter ¶
StorageConverter is a type that can convert itself to a valid, usable certmagic.Storage value. (The value might be short-lived.) This interface allows us to adapt any CertMagic storage implementation into a consistent API for Caddy configuration.
type UsagePool ¶
UsagePool is a thread-safe map that pools values based on usage (reference counting). Values are only inserted if they do not already exist. There are two ways to add values to the pool:
- LoadOrStore will increment usage and store the value immediately if it does not already exist.
- LoadOrNew will atomically check for existence and construct the value immediately if it does not already exist, or increment the usage otherwise, then store that value in the pool. When the constructed value is finally deleted from the pool (when its usage reaches 0), it will be cleaned up by calling Destruct().
The use of LoadOrNew allows values to be created and reused and finally cleaned up only once, even though they may have many references throughout their lifespan. This is helpful, for example, when sharing thread-safe io.Writers that you only want to open and close once.
There is no way to overwrite existing keys in the pool without first deleting it as many times as it was stored. Deleting too many times will panic.
The implementation does not use a sync.Pool because UsagePool needs additional atomicity to run the constructor functions when creating a new value when LoadOrNew is used. (We could probably use sync.Pool but we'd still have to layer our own additional locks on top.)
An empty UsagePool is NOT safe to use; always call NewUsagePool() to make a new one.
func NewUsagePool ¶
func NewUsagePool() *UsagePool
NewUsagePool returns a new usage pool that is ready to use.
func (*UsagePool) Delete ¶
Delete decrements the usage count for key and removes the value from the underlying map if the usage is 0. It returns true if the usage count reached 0 and the value was deleted. It panics if the usage count drops below 0; always call Delete precisely as many times as LoadOrStore.
func (*UsagePool) LoadOrNew ¶
LoadOrNew loads the value associated with key from the pool if it already exists. If the key doesn't exist, it will call construct to create a new value and then stores that in the pool. An error is only returned if the constructor returns an error. The loaded or constructed value is returned. The loaded return value is true if the value already existed and was loaded, or false if it was newly constructed.
func (*UsagePool) LoadOrStore ¶
LoadOrStore loads the value associated with key from the pool if it already exists, or stores it if it does not exist. It returns the value that was either loaded or stored, and true if the value already existed and was
func (*UsagePool) Range ¶
Range iterates the pool similarly to how sync.Map.Range() does: it calls f for every key in the pool, and if f returns false, iteration is stopped. Ranging does not affect usage counts.
This method is somewhat naive and acquires a read lock on the entire pool during iteration, so do your best to make f() really fast, m'kay?
type Validator ¶
type Validator interface {
Validate() error
}
Validator is implemented by modules which can verify that their configurations are valid. This method will be called after Provision() (if implemented). Validation should always be fast (imperceptible running time) and an error must be returned if the module's configuration is invalid.
type WriterOpener ¶
type WriterOpener interface { fmt.Stringer // WriterKey is a string that uniquely identifies this // writer configuration. It is not shown to humans. WriterKey() string // OpenWriter opens a log for writing. The writer // should be safe for concurrent use but need not // be synchronous. OpenWriter() (io.WriteCloser, error) }
WriterOpener is a module that can open a log writer. It can return a human-readable string representation of itself so that operators can understand where the logs are going.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
caddy
Package main is the entry point of the Caddy application.
|
Package main is the entry point of the Caddy application. |
internal
|
|
modules
|
|
caddyevents/eventsconfig
Package eventsconfig is for configuring caddyevents.App with the Caddyfile.
|
Package eventsconfig is for configuring caddyevents.App with the Caddyfile. |
caddyhttp/encode
Package encode implements an encoder middleware for Caddy.
|
Package encode implements an encoder middleware for Caddy. |
caddytls/distributedstek
Package distributedstek provides TLS session ticket ephemeral keys (STEKs) in a distributed fashion by utilizing configured storage for locking and key sharing.
|
Package distributedstek provides TLS session ticket ephemeral keys (STEKs) in a distributed fashion by utilizing configured storage for locking and key sharing. |
Package notify provides facilities for notifying process managers of state changes, mainly for when running as a system service.
|
Package notify provides facilities for notifying process managers of state changes, mainly for when running as a system service. |