I have started learning web development and decided to learn Go to handle the backend. Having come across Caddy has me pretty excited by I seem to be hitting a wall in terms of what its been designed to do. Part of this is definitely due to my lack of understanding of the Go nomenclature.
I have two or three projects on the go on a digitalOcean VPS, and the VPS has a domain redirected to it. So in order to switch between projects I need to kill the Go server and switch working directories and run the server again. The server is using gorilla/mux, gorilla/handlers, and net/http.
From what I understand about Caddy, I should be able to run all three projects from the same VPS from three different domains and handle all requests through Caddy while still accessing the Go functions that handle the requests on my current server configuration.
Currently the server.go looks something like this:
If that’s all you need, then it might just be easiest to write an HTTP handler in Go that checks req.Host, and depending on what it is, execute the middleware (handler) for that hostname.
Using Caddy as a library is a little higher-level: you pass in a Caddyfile to caddy.Start(), but if all you need is those 3 hosts then you’re already 90% of the way there with your Go code above.
If you wouldnt mind indulging me with some info or places to look. Even just a repo I can look at. I do intend to be working toward that higher level even if it is unapplicable in certain cases.
I was just thinking Caddy because I’ll have a half-dozen or more domains and handling all those certs gets complicated. Figured I might be able to use Caddy to avoid reinventing the wheel.
Well, Caddy isn’t meant to be used just as a certificate manager, but what you can use it for is reverse-proxying to your Go application. In other words, put it in front of your Go services and serve your Go apps on some localhost ports and have Caddy reverse proxy to them.
What you read is accurate: Caddy is a web server. It does fully manage the TLS assets (I use the term “SSL” on the homepage because that’s what people are searching for, unfortunately) that it needs. What you don’t want to do is use Caddy only as a means to obtain and keep certificates renewed; there are other tools for that. Caddy’s implementation of its magic TLS feature are very specific to serving its own. Although it does place the certs on disk for other processes and programs to use, I don’t recommend it, you will probably find that it gets hairy pretty quickly.
Anyway, I recommend plopping Caddy in front of your application and reverse proxying to your Go apps. That way, you get all the benefits of Caddy (easy to configure, managed TLS, etc) without having to rewrite your Go apps.
If you want to embed that into the same process as your Go services, you can use caddy.Start() and pass it a Caddyfile as if you were running Caddy separately. It’s essentially the same thing, but without an extra process to manage.
With that Caddyfile, you can start the caddy command as a separate program or you can call caddy.Start() from your own Go program, so you won’t need a separate binary in that case. Your choice!
You probably forgot to put your directive in the list in order; without being in that list, the http server doesn’t know in which order to execute your directive.