caddy使用API管理服务

本教程将向您展示如何使用 Caddy 的管理 API,它可以让您以可编程的方式实现自动化。

目标:

  • 🔲 启动守护进程

  • 🔲 给 Caddy 提供配置

  • 🔲 测试配置

  • 🔲 替换活动配置

  • 🔲 遍历配置

  • 🔲 使用 @id
     标签

先决条件:

  • 基本的终端/命令行技能

  • 基本的 JSON 经验

  • caddy
     和 curl
     在您的 PATH 中

要启动 Caddy 守护进程,请使用 run
 子命令:

1
2
3
4
caddy run
```

这会一直阻塞,但它在做什么呢?目前……什么也没做。默认情况下,Caddy 的配置(“config”)是空白的。我们可以通过在另一个终端中使用管理 API 来验证这一点:

curl localhost:2019/config/

1
2
3
4
5
6
7
8
9
  
我们可以通过给它一个配置来让 Caddy 变得有用。做到这一点的一种方法是向 /load
 端点发起 POST 请求。就像任何 HTTP 请求一样,有多种方法可以做到这一点,但在本教程中,我们将使用 curl

## 您的第一个配置

为了准备我们的请求,我们需要创建一个配置。Caddy 的配置只是一个 JSON 文档(或者任何可以转换为 JSON 的东西)。

将其保存到一个 JSON 文件中:

{
    “apps”: {
        “http”: {
            “servers”: {
                “example”: {
                    “listen”: [“:2015”],
                    “routes”: [
                        {
                            “handle”: [{
                                “handler”: “static_response”,
                                “body”: “Hello, world!”
                            }]
                        }
                    ]
                }
            }
        }
    }
}

1
2
  
然后上传它:

curl localhost:2019/load \ -H “Content-Type: application/json” \ -d @caddy.json

1
2
  
我们可以使用另一个 GET 请求来验证 Caddy 是否应用了我们的新配置:

curl localhost:2019/config/

1
2
3
  
通过在浏览器中访问 localhost:2015 或使用 curl
 来测试它是否正常工作:

curl localhost:2015Hello, world!

1
2
3
4
5
  
如果您看到 Hello, world!
,那么恭喜——它起作用了!在将其部署到生产环境中之前,始终确保您的配置按您预期的方式工作,这是很重要的。

让我们将欢迎消息从“Hello world!”改为更具激励性的话语:“I can do hard things.” 在配置文件中进行此更改,以便 handler 对象现在看起来像这样:

{ “handler”: “static_response”, “body”: “I can do hard things.”}

1
2
  
保存配置文件,然后通过再次运行相同的 POST 请求来更新 Caddy 的活动配置:

curl localhost:2019/load \ -H “Content-Type: application/json” \ -d @caddy.json

1
2
  
为了确保万无一失,验证配置是否已更新:

curl localhost:2019/config/

1
2
3
4
5
6
7
8
  
通过在浏览器中刷新页面(或再次运行 curl
),您将看到一条励志信息!
## 配置遍历

与其为了一个小更改而上传整个配置文件,不如使用 Caddy API 的一个强大功能,无需触及配置文件即可进行更改。

使用请求 URI 的路径,我们可以遍历配置结构并仅更新消息字符串(如果被截断,请确保向右滚动):

curl \ localhost:2019/config/apps/http/servers/example/routes/0/handle/0/body \ -H “Content-Type: application/json” \ -d ‘“Work smarter, not harder.”’

1
2
  
您可以使用类似的 GET 请求来验证它是否成功,例如:

curl localhost:2019/config/apps/http/servers/example/routes

1
2
  
您应该看到:

[{“handle”:[{“body”:“Work smarter, not harder.”,“handler”:“static_response”}]}]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  
**重要提示:**
 这应该是显而易见的,但一旦您使用 API 进行了不在原始配置文件中的更改,您的配置文件就会过时。有几种方法可以处理这个问题:
- 使用 caddy run
 命令的 --resume
 选项来使用最后一个活动配置。

- 不要将配置文件与通过 API 进行的更改混用;保持单一的真理来源。

- 使用后续的 GET 请求导出 Caddy 的新配置(不太推荐,不如前两个选项)。

## 在 JSON 中使用 @id

配置遍历当然很有用,但路径是不是有点长?

我们可以在 handler 对象中添加一个 @id
 标签,以便更轻松地访问它:

curl \ localhost:2019/config/apps/http/servers/example/routes/0/handle/0/@id \ -H “Content-Type: application/json” \ -d ‘“msg”’

1
2
3
  
这为我们的 handler 对象添加了一个属性:"@id": "msg"
,因此它现在看起来像这样:

{ “@id”: “msg”, “body”: “Work smarter, not harder.”, “handler”: “static_response”}

1
2
  
然后我们可以直接访问它:

curl localhost:2019/id/msg

1
2
  
现在我们可以使用更短的路径来更改消息:

curl \ localhost:2019/id/msg/body \ -H “Content-Type: application/json” \ -d ‘“Some shortcuts are good.”’

1
2
  
再次检查它:

curl localhost:2019/id/msg/body

  
  

![江达小记](/images/wechatmpscan.png)