Hi!
I’ve embedded Caddy like so:
package main
import (
"io/ioutil"
"log"
"os"
"github.com/mholt/caddy"
_ "github.com/mholt/caddy/caddyhttp"
)
func init() {
// configure default caddyfile
caddy.SetDefaultCaddyfileLoader("default", caddy.LoaderFunc(defaultLoader))
}
func main() {
caddy.AppName = "Sprocketplus"
caddy.AppVersion = "1.2.3"
// load caddyfile
caddyfile, err := caddy.LoadCaddyfile("http")
if err != nil {
log.Fatal(err)
}
// start caddy server
instance, err := caddy.Start(caddyfile)
if err != nil {
log.Fatal(err)
}
instance.Wait()
}
// provide loader function
func defaultLoader(serverType string) (caddy.Input, error) {
contents, err := ioutil.ReadFile(caddy.DefaultConfigFile)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
return caddy.CaddyfileInput{
Contents: contents,
Filepath: caddy.DefaultConfigFile,
ServerTypeName: serverType,
}, nil
}
With the following Caddyfile:
:8080
zb a a a
And the following plugin (added to the appropriate places in the Caddy codebase)
package main
import(
"github.com/mholt/caddy"
"fmt"
)
func setup(c *caddy.Controller) error {
for c.Next() { // skip the directive name
if !c.NextArg() { // expect at least one value
return c.ArgErr() // otherwise it's an error
}
value := c.Val() // use the value
fmt.Printf(value)
}
return nil
}
func init() {
caddy.RegisterPlugin("zb", caddy.Plugin{
ServerType: "http",
Action: setup,
})
}
Upon running the built Go program (serv.go
):
2018/01/24 16:54:40 no action found for directive 'zb' with server type 'http' (missing a plugin?)
Any ideas?
I’ve added my plugin to:
…/…/…/github.com/mholt/caddy/caddy/caddymain/run.go
and
…/…/…/github.com/mholt/caddy/caddyhttp/httpserver/plugin.go
Thanks in advance all!