Update for LoadModule refactoring that uses struct tags

Matt Holt 2019-12-10 14:53:03 -07:00
parent e272ea0baa
commit 54ccf2f30c

@ -46,7 +46,7 @@ type Gizmo struct {
// CaddyModule returns the Caddy module information. // CaddyModule returns the Caddy module information.
func (Gizmo) CaddyModule() caddy.ModuleInfo { func (Gizmo) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{ return caddy.ModuleInfo{
Name: "foo.gizmo", ID: "foo.gizmo",
New: func() caddy.Module { return new(Gizmo) }, New: func() caddy.Module { return new(Gizmo) },
} }
} }
@ -72,11 +72,11 @@ We will show you how to satisfy these requirements in the next sections. It's re
# Module Names # Module Names
Each Caddy module has a unique name, consisting of a namespace and ID: Each Caddy module has a unique ID, consisting of a namespace and name:
- A name looks like `a.b.c.module_id` - A complete ID looks like `a.b.c.module_name`
- The namespace would be `a.b.c` - The namespace would be `a.b.c`
- The module ID would be `module_id` which must be unique in its namespace - The name would be `module_name` which must be unique in its namespace
_Host modules_ (or _parent modules_) are modules which load/initialize other modules. They typically define namespaces for guest modules. _Host modules_ (or _parent modules_) are modules which load/initialize other modules. They typically define namespaces for guest modules.
@ -241,11 +241,12 @@ A host module will need to load and use its guest modules. Caddy provides facili
First, host modules need a way to receive the guest modules' configuration. This is typically done by adding `json.RawMessage` fields to the struct, and then using the Provision method to load them into a non-JSON type. For example, if your module `Gizmo` has a `Gadget` guest module that implements a `Gadgeter` interface, you would add two fields, `GadgetRaw` and `Gadget`: First, host modules need a way to receive the guest modules' configuration. This is typically done by adding `json.RawMessage` fields to the struct, and then using the Provision method to load them into a non-JSON type. For example, if your module `Gizmo` has a `Gadget` guest module that implements a `Gadgeter` interface, you would add two fields, `GadgetRaw` and `Gadget`:
```go ```go
GadgetRaw json.RawMessage `json:"gadget,omitempty"` GadgetRaw json.RawMessage `json:"gadget,omitempty" caddy:"namespace=foo.gizmo.gadgets inline_key=gadgeter"`
// the decoded value of the guest module will be // the decoded value of the guest module will be
// stored here, but it doesn't get used for JSON // stored here, but it doesn't get used for JSON
// so make sure to exclude it with "-" // so make sure to either exclude it with "-" or
// unexport it
Gadget Gadgeter `json:"-"` Gadget Gadgeter `json:"-"`
``` ```
@ -262,12 +263,11 @@ Here's an example of using `LoadModuleInline()` to get a Gadgeter for our Gizmo
// Provision sets up g and loads its gadget. // Provision sets up g and loads its gadget.
func (g *Gizmo) Provision(ctx caddy.Context) error { func (g *Gizmo) Provision(ctx caddy.Context) error {
if g.GadgetRaw != nil { if g.GadgetRaw != nil {
val, err := ctx.LoadModuleInline("gadgeter", "foo.gizmo.gadgets", g.GadgetRaw) val, err := ctx.LoadModule(g, "GadgetRaw")
if err != nil { if err != nil {
return fmt.Errorf("loading gadget module: %v", err) return fmt.Errorf("loading gadget module: %v", err)
} }
g.Gadget = val.(Gadgeter) g.Gadget = val.(Gadgeter)
g.GadgetRaw = nil // allow GC to deallocate
} }
return nil return nil
} }
@ -316,8 +316,8 @@ type Middleware struct {
// CaddyModule returns the Caddy module information. // CaddyModule returns the Caddy module information.
func (Middleware) CaddyModule() caddy.ModuleInfo { func (Middleware) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{ return caddy.ModuleInfo{
Name: "http.handlers.visitor_ip", ID: "http.handlers.visitor_ip",
New: func() caddy.Module { return new(Middleware) }, New: func() caddy.Module { return new(Middleware) },
} }
} }