From 3268df343699ebffd112ac04f1624410ffa50b7f Mon Sep 17 00:00:00 2001 From: Matt Holt Date: Mon, 6 Jun 2016 23:05:40 -0600 Subject: [PATCH] Updated Embedding Caddy in your Go program (markdown) --- Embedding-Caddy-in-your-Go-program.md | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/Embedding-Caddy-in-your-Go-program.md b/Embedding-Caddy-in-your-Go-program.md index e7c7192..c59e7a1 100644 --- a/Embedding-Caddy-in-your-Go-program.md +++ b/Embedding-Caddy-in-your-Go-program.md @@ -1,8 +1,8 @@ You can use Caddy as a library in your Go program. This is useful if your Go app needs more than just Go's basic static file server or reverse proxy. You can even take advantage of Caddy's graceful restart functionality when you need to reload your program. -You just need the [caddy](https://godoc.org/github.com/mholt/caddy/caddy) package. The godoc has instructions. +You just need the [caddy](https://godoc.org/github.com/mholt/caddy) package. The linked godoc has instructions. -If you don't want to craft a raw Caddyfile string, you can use the [caddyfile](https://godoc.org/github.com/mholt/caddy/caddy/caddyfile) package to convert between that and a JSON representation of the tokens. It's lightly structured and very easy to manipulate with code, rather than dealing with parsing a raw string. +Note: If you don't want to craft a raw Caddyfile string (whih, you can use the [caddyfile](https://godoc.org/github.com/mholt/caddy/caddyfile) package to convert between that and a JSON representation of the tokens. It's lightly structured and very easy to manipulate with code, rather than dealing with parsing a raw string. Here's a basic example: @@ -10,30 +10,28 @@ Here's a basic example: caddy.AppName = "Sprocket" caddy.AppVersion = "1.2.3" -// Always load the Caddyfile with this function. It ensures -// the Caddyfile is properly loaded after restarts. -caddyfile, err := caddy.LoadCaddyfile(myLoader) +// pass in the name of the server type this Caddyfile is for (like "http") +caddyfile, err := caddy.LoadCaddyfile(serverType) if err != nil { log.Fatal(err) } -err = caddy.Start(caddyfile) +instance, err := caddy.Start(caddyfile) if err != nil { log.Fatal(err) } // Start() only blocks until the servers have started. // Wait() blocks until the servers have stopped. -caddy.Wait() +instance.Wait() ``` You can also restart Caddy: ```go -// On Unix systems, this spawns a new process and kills the -// current one! That's how we get graceful reloads. +// On Unix systems, you get graceful restarts. // To use same Caddyfile, just pass in nil. -err = caddy.Restart(newCaddyfile) +err = instance.Restart(newCaddyfile) if err != nil { log.Fatal(err) } @@ -43,7 +41,7 @@ if err != nil { Or stop it: ```go -err = caddy.Stop() +err = instance.Stop() if err != nil { log.Fatal(err) }