Caddy 2.8.4 unrecognized directive

Folks, I’m trying to test a simple custom plugin, I created a module that reads a config file and gets an access token from azure. I want to call this module then an endpoint is invoked. I wrote a test program in go that tests the module and it does return the token OK. My problem is calling the module from my caddyfile. The module is registered as http.handlers.azureauth and in my caddyfile I call azureauth but I get Caddyfile:11: unrecognized directive: azureauth

My caddyfile is this:

{
# Optional global options block
}

:8080 {
@somePath {
path /test
}

# Use the AzureAuth handler to retrieve the token
azureauth {
    token {
        # This section could be empty if you are loading from config
        # It will use the AzureToken struct's Provision method to load the token info
    }
}

# Handle requests to the /test endpoint
handle @somePath {
    # Call the next handler after setting the token in the header
    respond "Token retrieved successfully" 200
}

# Add other handlers as needed

}

1 Like

How are you running Caddy? Make sure that your plugin is compiled in first. Typically, xcaddy run is what you’ll use for testing while developing your plugin. It does this for you.

Also, where’s your plugin code? Make sure to register your directive.

1 Like

Hi @matt , to be honest I’ve tried many different ways but I’ve gone simple following the tutorial and I get the same error. Writing a Caddy Plugin Part II | 萌え豚's Blog
The error is
C:\dev\caddy\caddy-hello>dir
Volume in drive C is Windows-SSD
Volume Serial Number is A224-C1F2

Directory of C:\dev\caddy\caddy-hello

28/09/2024 01:50 .
28/09/2024 01:41 …
28/09/2024 01:42 1,001 app.go
28/09/2024 01:50 41,420,288 caddy.exe
28/09/2024 01:48 35 caddyfile
28/09/2024 01:45 885 caddyfile.go
28/09/2024 01:41 30 go.mod
5 File(s) 41,422,239 bytes
2 Dir(s) 831,909,367,808 bytes free

C:\dev\caddy\caddy-hello>caddy run
2024/09/27 23:51:17.039 ←[34mINFO←[0m using adjacent Caddyfile
Error: adapting config using caddyfile: Caddyfile:2: unrecognized directive: address

C:\dev\caddy\caddy-hello>caddy list-modules
admin.api.load

Standard modules: 121

caddy_hello

Non-standard modules: 1

Unknown modules: 0

C:\dev\caddy\caddy-hello>caddy run
2024/09/27 23:52:34.150 ←[34mINFO←[0m using adjacent Caddyfile
Error: adapting config using caddyfile: Caddyfile:2: unrecognized directive: address

I’ve basically had no joy registering a module as a directive.
Hopefully its something daft I’m doing, so really appreciate the pointer.

caddyfile as per the tutorial

caddy_hello {
address 127.0.0.1:184111
}

Please mind your post’s formatting. It’s hard to follow because you didn’t use code blocks for file/log content. Use the </> button and put the contents between the ```

I don’t see anywhere in your post that you use xcaddy. See this program:

Did you try following this guide?

After that, read this one to understand how to parse Caddyfile config for your plugin:

Show the actual Go code you’re writing as well, if you still can’t get it working.

2 Likes

Hi thanks for the reply, sorry I will use the </> when I paste code next time.

It was such a simple test.

C:\dev\caddy\caddy-hello> xcaddy run

was the command i used and I also tried

xcaddy build --with caddy-hello=.

I generally see the module listed when i execute

C:\dev\caddy\caddy-hello>caddy list-modules

Standard modules: 121

caddy_hello

Non-standard modules: 1

Unknown modules: 0

but I cannot access them as a directive from the caddyfile.

When I run caddy.exe run I get

unrecognized directive

Is there anything special with modules that are local only ?

That’s still only half the story. Show your module code.

Normally Go packages have an ID like github.com/your-username/package.

caddy_hello is certainly not a standard module, so I don’t understand that output. Are you editing that at all? Please don’t, show the actual output.

Well, without seeing your code nor your Caddyfile, I don’t know what to tell you. Also, that’s definitely not the entire error message, it would be a much longer error message than that (with the line number in your Caddyfile etc).

1 Like

Hi Francis, thanks for trying to help, first time here and first time using go!

Let me share the go code

1 - caddyfile.go

package caddy_hello

import (
	"github.com/caddyserver/caddy/v2/caddyconfig"
	"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
	"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
)

func init() {
	httpcaddyfile.RegisterGlobalOption("caddy_hello", func(d *caddyfile.Dispenser, existingVal any) (any, error) {
		h := new(Hello)
		err := h.UnmarshalCaddyfile(d)
		return httpcaddyfile.App{
			Name:  "caddy_hello",
			Value: caddyconfig.JSON(h, nil),
		}, err
	})
}

func (h *Hello) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
	for d.Next() {
		// No same-line options are supported
		if d.NextArg() {
			return d.ArgErr()
		}

		for d.NextBlock(0) {
			switch d.Val() {
			case "address":
				if !d.NextArg() {
					return d.ArgErr()
				}
				h.Address = d.Val()
			default:
				return d.ArgErr()
			}
		}
	}
	return nil
}

2 - app.go

package caddy_hello

import (
	"context"
	"net"

	"github.com/caddyserver/caddy/v2"
)

type Hello struct {
	Address string `json:"address"`

	listener net.Listener
}

func (Hello) CaddyModule() caddy.ModuleInfo {
	return caddy.ModuleInfo{
		ID:  "caddy_hello",
		New: func() caddy.Module { return new(Hello) },
	}
}

func init() {
	caddy.RegisterModule(Hello{})
}

func (h *Hello) Start() error {
	network, err := caddy.ParseNetworkAddress(h.Address)
	if err != nil {
		return err
	}

	listener, err := network.Listen(context.Background(), 0, net.ListenConfig{})
	if err != nil {
		return err
	}
	h.listener = listener.(net.Listener)
	go h.loop()
	return nil
}

func (h *Hello) Stop() error {
	return h.listener.Close()
}

func (h *Hello) loop() {
	for {
		c, err := h.listener.Accept()
		if err != nil {
			break
		}
		go h.handleConn(c)
	}
}

func (h *Hello) handleConn(c net.Conn) {
	_, _ = c.Write([]byte("Hello world"))
	_ = c.Close()
}

3 - go.mod

module caddy-hello

go 1.23.1

4 - caddyfile

caddy_hello {
    address 127.0.0.1:9090
}

Compiling -

xcaddy build --with caddy-hello=./caddy-hello

Gives me an error -

2024/09/28 22:57:21 [INFO] exec (timeout=0s): C:\Program Files\Go\bin\go.exe get -d -v
go: -d flag is deprecated. -d=true is a no-op
go: caddy imports
caddy-hello: package caddy-hello is not in std (C:\Program Files\Go\src\caddy-hello)
2024/09/28 22:57:22 [FATAL] exit status 1

But this allows me to compile

C:\dev\caddy\caddy-hello>xcaddy build --with caddy-hello=.
2024/09/28 22:59:21 [INFO] Resolved relative replacement caddy-hello=. to C:\dev\caddy\caddy-hello
2024/09/28 22:59:21 [INFO] absolute output file path: C:\dev\caddy\caddy-hello\caddy.exe
2024/09/28 22:59:21 [INFO] Temporary folder: C:\Users\datum\AppData\Local\Temp\buildenv_2024-09-28-2259.811483125
2024/09/28 22:59:21 [INFO] Writing main module: C:\Users\datum\AppData\Local\Temp\buildenv_2024-09-28-2259.811483125\main.go
package main

import (
caddycmd “github.com/caddyserver/caddy/v2/cmd

    // plug in Caddy modules here
    _ "github.com/caddyserver/caddy/v2/modules/standard"
    _ "caddy-hello"

)

func main() {
caddycmd.Main()
}
2024/09/28 22:59:21 [INFO] Initializing Go module
2024/09/28 22:59:21 [INFO] exec (timeout=0s): C:\Program Files\Go\bin\go.exe mod init caddy
go: creating new go.mod: module caddy
go: to add module requirements and sums:
go mod tidy
2024/09/28 22:59:21 [INFO] Replace caddy-hello => C:\dev\caddy\caddy-hello
2024/09/28 22:59:21 [INFO] exec (timeout=0s): C:\Program Files\Go\bin\go.exe mod edit -replace caddy-hello=C:\dev\caddy\caddy-hello
2024/09/28 22:59:22 [INFO] Pinning versions
2024/09/28 22:59:22 [INFO] exec (timeout=0s): C:\Program Files\Go\bin\go.exe get -d -v github.com/caddyserver/caddy/v2
go: -d flag is deprecated. -d=true is a no-op
go: added GitHub - beorn7/perks: Effective Computation of Things v1.0.1
go: added github.com/caddyserver/caddy/v2 v2.8.4
go: added GitHub - caddyserver/certmagic: Automatic HTTPS for any Go program: fully-managed TLS certificate issuance and renewal v0.21.3
go: added GitHub - caddyserver/zerossl: ZeroSSL REST API client implementation for Go v0.1.3
go: added github.com/cespare/xxhash/v2 v2.2.0
go: added GitHub - go-task/slim-sprig: Useful template functions for Go templates. v0.0.0-20230315185526-52ccab3ef572
go: added GitHub - google/pprof: pprof is a tool for visualization and analysis of profiling data v0.0.0-20231212022811-ec68065c825e
go: added GitHub - google/uuid: Go package for UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services. v1.6.0
go: added github.com/klauspost/cpuid/v2 v2.2.7
go: added GitHub - libdns/libdns: Core interfaces for universal DNS record manipulation across providers v0.2.2
go: added github.com/mholt/acmez/v2 v2.0.1
go: added GitHub - miekg/dns: DNS library in Go v1.1.59
go: added github.com/onsi/ginkgo/v2 v2.13.2
go: added GitHub - prometheus/client_golang: Prometheus instrumentation library for Go applications v1.19.1
go: added GitHub - prometheus/client_model: Data model artifacts for Prometheus. v0.5.0
go: added GitHub - prometheus/common: Go libraries shared across Prometheus components and libraries. v0.48.0
go: added GitHub - prometheus/procfs: procfs provides functions to retrieve system, kernel and process metrics from the pseudo-filesystem proc. v0.12.0
go: added GitHub - quic-go/qpack: a (minimal) QPACK (RFC 9204) implementation in Go v0.4.0
go: added GitHub - quic-go/quic-go: A QUIC implementation in pure Go v0.44.0
go: added GitHub - zeebo/blake3: Pure Go implementation of BLAKE3 with AVX2 and SSE4.1 acceleration v0.2.3
go: added go.uber.org/mock v0.4.0
go: added go.uber.org/multierr v1.11.0
go: added go.uber.org/zap v1.27.0
go: added go.uber.org/zap/exp v0.2.0
go: added The Go Programming Language v0.23.0
go: added The Go Programming Language v0.0.0-20240506185415-9bf2ced13842
go: added The Go Programming Language v0.17.0
go: added The Go Programming Language v0.25.0
go: added The Go Programming Language v0.7.0
go: added The Go Programming Language v0.20.0
go: added The Go Programming Language v0.20.0
go: added The Go Programming Language v0.15.0
go: added The Go Programming Language v0.5.0
go: added The Go Programming Language v0.21.0
go: added protobuf module - google.golang.org/protobuf - Go Packages v1.34.1
2024/09/28 22:59:22 [INFO] exec (timeout=0s): C:\Program Files\Go\bin\go.exe get -d -v
go: -d flag is deprecated. -d=true is a no-op
go: trying upgrade to caddy-hello@v0.0.0-00010101000000-000000000000
go: added caddy-hello v0.0.0-00010101000000-000000000000
2024/09/28 22:59:23 [INFO] Build environment ready
2024/09/28 22:59:23 [INFO] exec (timeout=0s): C:\Program Files\Go\bin\go.exe list -m github.com/caddyserver/caddy/v2
2024/09/28 22:59:23 [INFO] Building Caddy
2024/09/28 22:59:23 [INFO] exec (timeout=0s): C:\Program Files\Go\bin\go.exe mod tidy -e
2024/09/28 22:59:24 [INFO] exec (timeout=0s): C:\Program Files\Go\bin\go.exe build -o C:\dev\caddy\caddy-hello\caddy.exe -ldflags -w -s -trimpath -tags nobadger
2024/09/28 22:59:25 [INFO] Build complete: .\caddy.exe
2024/09/28 22:59:25 [INFO] Cleaning up temporary folder: C:\Users\datum\AppData\Local\Temp\buildenv_2024-09-28-2259.811483125

..\caddy.exe version
v2.8.4 h1:q3pe0wpBj1OcHFZ3n/1nl4V4bxBrYoSoab7rL9BMYNk=

C:\dev\caddy\caddy-hello>

Then caddy run gives this error (which is the same as I see for other attempts)

C:\dev\caddy\caddy-hello>caddy run
2024/09/28 21:01:18.470 ←[34mINFO←[0m using adjacent Caddyfile
Error: adapting config using caddyfile: Caddyfile:2: unrecognized directive: address

Please put everything in code blocks, don’t use quote blocks for anything like logs. Code blocks preserve the text and indentation, quotes don’t and cause things like Markdown rendering to occur so it messes up anything that looks like a link etc. Notice how messy your post looks because of that.

Ah, you’re trying to declare a global option. In that case, you must put your config within the global options block, it can’t be top level. So it should look like this:

{
	caddy_hello {
		address 127.0.0.1:9090
	}
}

See the structure of the Caddyfile:

3 Likes

Thanks Francis

2 Likes

This topic was automatically closed after 30 days. New replies are no longer allowed.