How can I embed Caddy 2 within my application?

I’m trying to develop a single binary web app with assets embedded with pkger, sqlite, etc.

  • Is it possible to embed Caddy v2 within my normal Gorilla Mux routing application so that I can take advantage of the easy SSL configuration?
  • How can I embed Caddy to use the reserve proxy feature to pass to the Webpack/Rollup development server (when in dev mode).

I’m trying to do all of this crazy stuff one binary as an educational project (that fits my deployment needs). The most important thing is how can I run Caddy and a std http stack at the same time or do I need to hook my routing into Caddy?

Do I need to turn my application into a Caddy v2 plugin? I’m not 100% sure which direction to go.

You can technically embed Caddy in your application (just see Caddy’s own main / entry point). However this is a bit unwieldy for most use cases.

Most people will instead want to write a Caddy module/plugin. For example, if your module just needs to respond to HTTP requests, then write an HTTP handler module.

It seems that Caddy v2 is no longer suitable for embedding into an existing application. Maybe that was too niche of a use case but it seemed like it had a lot of potential in v1. In my situation I want to have control over the command line organization so that certain parts of caddy are just run under a Cobra subcommand. This sort of kills my single binary dream.

Maybe all that I’m looking for is the certmagic embed. Is it suitable for my spicing up and existing http mux app?

Your Caddy plugin can provide its own CLI commands. It just needs to call caddycmd.RegisterCommand(). See the code in cmd/commands.go in the Caddy source.

Caddy v2 is essentially an “app platform” at the core. It just happens to ship with an HTTP app by default.

Read more about the architecture here:

That said, yeah, certmagic is probably what you’re looking for if you only need the TLS management part.


Yeah, if all you need is TLS / HTTPS in your Go program, then CertMagic is exactly the right abstraction.

1 Like

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