Embedding Caddy and using a plugin


(Zack) #1

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! :slight_smile:


(Matt Holt) #2

It doesn’t look like you’re importing your plugin anywhere. If it’s not imported, its code will never be run (including its registration function). :slight_smile:


(Zack) #3

Hi Matt!

Sorry, forgot to say I’ve imported the plugin on caddymain/run.go:

import (
	"errors"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"runtime"
	"strconv"
	"strings"

	"gopkg.in/natefinch/lumberjack.v2"

	"github.com/xenolf/lego/acme"

	"github.com/mholt/caddy"
	// plug in the HTTP server type
	_ "github.com/mholt/caddy/caddyhttp"


	"github.com/mholt/caddy/caddytls"
	_ "g.apertron.net/Xorkle/zb"
	// This is where other plugins get plugged in (imported)
)

(Matt Holt) #4

Hmmm, in that case I would double check to be absolutely certain your build environment is correct and that your build contains what you think it does.


(Zack) #5

Hi Matt,

I’ve just rm -rvf'd my go folder, tried doing the steps mentioned and I’m getting the same issue.

I wonder maybe it’s when building serv.go (the first code snippet).

All I do is go build serv.go and run it with the aforementioned Caddyfile, and get the error. Maybe I’m missing a special step.

Thanks


(Zack) #6

Hello Matt,

In true Zack manner I’ve got it working 3 minutes after posting.

I added

	_ "g.apertron.net/Xorkle/zb"

To the top of serv.go

Thanks!


(Kane York) #7

The best way to do this would be to import the package from main, and have that be your list of enabled plugins :slight_smile: