mirror of
https://github.com/caddyserver/caddy.git
synced 2025-10-29 01:32:46 -04:00
Updated Embedding Caddy in your Go program (markdown)
parent
4ae5b0e639
commit
3268df3436
@ -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)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user