mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-23 09:06:29 +01:00
3c90e370a4
This commit goes a long way toward making automated documentation of Caddy config and Caddy modules possible. It's a broad, sweeping change, but mostly internal. It allows us to automatically generate docs for all Caddy modules (including future third-party ones) and make them viewable on a web page; it also doubles as godoc comments. As such, this commit makes significant progress in migrating the docs from our temporary wiki page toward our new website which is still under construction. With this change, all host modules will use ctx.LoadModule() and pass in both the struct pointer and the field name as a string. This allows the reflect package to read the struct tag from that field so that it can get the necessary information like the module namespace and the inline key. This has the nice side-effect of unifying the code and documentation. It also simplifies module loading, and handles several variations on field types for raw module fields (i.e. variations on json.RawMessage, such as arrays and maps). I also renamed ModuleInfo.Name -> ModuleInfo.ID, to make it clear that the ID is the "full name" which includes both the module namespace and the name. This clarity is helpful when describing module hierarchy. As of this change, Caddy modules are no longer an experimental design. I think the architecture is good enough to go forward.
165 lines
5.1 KiB
Go
165 lines
5.1 KiB
Go
package lib
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
"go.starlark.net/starlark"
|
|
)
|
|
|
|
// ResponderModule represents a module that satisfies the caddyhttp handler.
|
|
type ResponderModule struct {
|
|
Name string
|
|
Cfg json.RawMessage
|
|
Instance caddyhttp.Handler
|
|
}
|
|
|
|
func (r ResponderModule) Freeze() {}
|
|
func (r ResponderModule) Hash() (uint32, error) { return 0, fmt.Errorf("unhashable: responder module") }
|
|
func (r ResponderModule) String() string { return "responder module" }
|
|
func (r ResponderModule) Type() string { return "responder module" }
|
|
func (r ResponderModule) Truth() starlark.Bool { return true }
|
|
|
|
// Middleware represents a module that satisfies the starlark Value interface.
|
|
type Middleware struct {
|
|
Name string
|
|
Cfg json.RawMessage
|
|
Instance caddyhttp.MiddlewareHandler
|
|
}
|
|
|
|
func (r Middleware) Freeze() {}
|
|
func (r Middleware) Hash() (uint32, error) { return 0, fmt.Errorf("unhashable: middleware") }
|
|
func (r Middleware) String() string { return "middleware" }
|
|
func (r Middleware) Type() string { return "middleware" }
|
|
func (r Middleware) Truth() starlark.Bool { return true }
|
|
|
|
// LoadMiddleware represents the method exposed to starlark to load a Caddy module.
|
|
type LoadMiddleware struct {
|
|
Middleware Middleware
|
|
Ctx caddy.Context
|
|
}
|
|
|
|
func (r LoadMiddleware) Freeze() {}
|
|
func (r LoadMiddleware) Hash() (uint32, error) { return 0, fmt.Errorf("unhashable: loadMiddleware") }
|
|
func (r LoadMiddleware) String() string { return "loadMiddleware" }
|
|
func (r LoadMiddleware) Type() string { return "function: loadMiddleware" }
|
|
func (r LoadMiddleware) Truth() starlark.Bool { return true }
|
|
|
|
// Run is the method bound to the starlark loadMiddleware function.
|
|
func (r *LoadMiddleware) Run(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
|
|
var name string
|
|
var cfg *starlark.Dict
|
|
err := starlark.UnpackPositionalArgs(fn.Name(), args, kwargs, 2, &name, &cfg)
|
|
if err != nil {
|
|
return starlark.None, fmt.Errorf("unpacking arguments: %v", err.Error())
|
|
}
|
|
|
|
js := json.RawMessage(cfg.String())
|
|
|
|
if strings.Index(name, "http.handlers.") == -1 {
|
|
name = fmt.Sprintf("http.handlers.%s", name)
|
|
}
|
|
|
|
inst, err := r.Ctx.LoadModuleByID(name, js)
|
|
if err != nil {
|
|
return starlark.None, err
|
|
}
|
|
|
|
mid, ok := inst.(caddyhttp.MiddlewareHandler)
|
|
if !ok {
|
|
return starlark.None, fmt.Errorf("could not assert as middleware handler")
|
|
}
|
|
|
|
m := Middleware{
|
|
Name: name,
|
|
Cfg: js,
|
|
Instance: mid,
|
|
}
|
|
|
|
r.Middleware = m
|
|
|
|
return m, nil
|
|
}
|
|
|
|
// LoadResponder represents the method exposed to starlark to load a Caddy middleware responder.
|
|
type LoadResponder struct {
|
|
Module ResponderModule
|
|
Ctx caddy.Context
|
|
}
|
|
|
|
func (r LoadResponder) Freeze() {}
|
|
func (r LoadResponder) Hash() (uint32, error) { return 0, fmt.Errorf("unhashable: loadModule") }
|
|
func (r LoadResponder) String() string { return "loadModule" }
|
|
func (r LoadResponder) Type() string { return "function: loadModule" }
|
|
func (r LoadResponder) Truth() starlark.Bool { return true }
|
|
|
|
// Run is the method bound to the starlark loadResponder function.
|
|
func (r *LoadResponder) Run(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
|
|
var name string
|
|
var cfg *starlark.Dict
|
|
err := starlark.UnpackPositionalArgs(fn.Name(), args, kwargs, 2, &name, &cfg)
|
|
if err != nil {
|
|
return starlark.None, fmt.Errorf("unpacking arguments: %v", err.Error())
|
|
}
|
|
|
|
js := json.RawMessage(cfg.String())
|
|
|
|
if strings.Index(name, "http.handlers.") == -1 {
|
|
name = fmt.Sprintf("http.handlers.%s", name)
|
|
}
|
|
|
|
inst, err := r.Ctx.LoadModuleByID(name, js)
|
|
if err != nil {
|
|
return starlark.None, err
|
|
}
|
|
|
|
res, ok := inst.(caddyhttp.Handler)
|
|
if !ok {
|
|
return starlark.None, fmt.Errorf("could not assert as responder")
|
|
}
|
|
|
|
m := ResponderModule{
|
|
Name: name,
|
|
Cfg: js,
|
|
Instance: res,
|
|
}
|
|
|
|
r.Module = m
|
|
|
|
return m, nil
|
|
}
|
|
|
|
// Execute represents the method exposed to starlark to build a middleware chain.
|
|
type Execute struct {
|
|
Modules []Middleware
|
|
}
|
|
|
|
func (r Execute) Freeze() {}
|
|
func (r Execute) Hash() (uint32, error) { return 0, fmt.Errorf("unhashable: execute") }
|
|
func (r Execute) String() string { return "execute" }
|
|
func (r Execute) Type() string { return "function: execute" }
|
|
func (r Execute) Truth() starlark.Bool { return true }
|
|
|
|
// Run is the method bound to the starlark execute function.
|
|
func (r *Execute) Run(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
|
|
var mids *starlark.List
|
|
err := starlark.UnpackPositionalArgs(fn.Name(), args, kwargs, 1, &mids)
|
|
if err != nil {
|
|
return starlark.None, fmt.Errorf("unpacking arguments: %v", err.Error())
|
|
}
|
|
|
|
for i := 0; i < mids.Len(); i++ {
|
|
val, ok := mids.Index(i).(Middleware)
|
|
if !ok {
|
|
return starlark.None, fmt.Errorf("cannot get module from execute")
|
|
}
|
|
|
|
r.Modules = append(r.Modules, val)
|
|
}
|
|
|
|
return starlark.None, nil
|
|
}
|