Caddy Embedded in Go

Based on https://github.com/mholt/caddy/wiki/Embedding-Caddy-in-your-Go-program
I want to embed Caddy in Go using a configuration file in a project directory. I modify the basic example by removing this line:
caddyfile, err := caddy.LoadCaddyfile(serverType)
and replacing it with:
file, err := os.Open("Caddyfile") caddyfile, err := caddy.CaddyfileFromPipe(file, "http")

The file loads successfully but when I do caddy.Start(caddyfile) it always returns this error: “no server types plugged in”

How do I get this to work??

I don’t recommend this; that’s what LoadCaddyfile is for. Why are you doing this?

LoadCaddyfileFromPipe() will load the Caddyfile that is being piped to stdin, only if it’s loaded from stdin. Do you want to load from a file or from a pipe?

Make sure you import the httpserver package: import _ "github.com/mholt/caddy/caddyhttp/httpserver"

I would like to be able to give Caddy a Caddyfile of my choosing, I thought that’s what LoadCaddyfileFromPipe() would do. The Caddyfile I’m trying to load works from the commandline with the -conf flag pointing to the Caddyfile.
Would it be better to copy the Caddyfile to the Caddy installation directory and then call LoadCaddyfile()?

Would it be better to copy the Caddyfile to the Caddy installation directory and then call LoadCaddyfile()?

I did this and it still gives me the error: “no server types plugged in”.
I did notice that the Caddy path ($GOPATH/bin/caddy) is not where I installed Caddy, and that Go copies my project’s Caddyfile to that directory. It fails from my Go program, but it works if I just run the executable directly from the command line.

This actually helped get past the “no server types plugged in” error (which I don’t really understand… I’m new to Go), now I get the error: “no action found for directive ‘root’ with server type ‘http’ (missing a plugin?)”

which was just fixed by following your hint and adding:
import _ "github.com/mholt/caddy/caddyhttp/root"

It runs now, thank you

No, this loads it from a pipe (stdin).

Fortunately, that’s what LoadCaddyfile() does. :slight_smile:

No, doesn’t matter where it is.

When you import the httpserver package, it registers its Caddyfile loaders so that you can load the Caddyfile properly. It also registers its server type so that you can load a Caddyfile for the http server type.

Glad you got it working. :slight_smile:

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.