Unrecognized directive for custom http.handlers

Hi, I wrote a custom http.handlers module for myself. I’m able to build it using xcaddy. caddy list-modules does show it as expected.

However, whenever I use its directive in my Caddyfile, I get the following:

Error: adapting config using caddyfile: parsing caddyfile tokens for ‘route’: unrecognized directive: redirectssh - are you sure your Caddyfile structure (nesting and braces) is correct?, at /etc/caddy/Caddyfile:131

Can someone please help me troubleshoot it? All relevant code can be found in share/2025/caddy_redirectssh at main · denisidoro/share · GitHub

Thanks in advance!

You need to use the RegisterHandlerDirective.

1 Like

It worked perfectly! Thank you very much!

For future reference, I asked an LLM to vibe code it for me, and here’s what did the trick:

package redirectssh

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

func init() {
	// Register the module itself
	caddy.RegisterModule(RedirectSSH{})

	// Register it as a handler directive for the Caddyfile
	httpcaddyfile.RegisterHandlerDirective("redirectssh", parseCaddyfileRedirectSSH)
}

// parseCaddyfileRedirectSSH converts the Caddyfile tokens into your module
func parseCaddyfileRedirectSSH(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
	var rs RedirectSSH
	err := rs.UnmarshalCaddyfile(h.Dispenser)
	if err != nil {
		return nil, err
	}
	return rs, nil
}