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”
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?)”
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.