caddy使用API管理服务
本教程将向您展示如何使用 Caddy 的管理 API,它可以让您以可编程的方式实现自动化。
目标:
-
🔲 启动守护进程
-
🔲 给 Caddy 提供配置
-
🔲 测试配置
-
🔲 替换活动配置
-
🔲 遍历配置
-
🔲 使用 @id
标签
先决条件:
-
基本的终端/命令行技能
-
基本的 JSON 经验
-
caddy
和 curl
在您的 PATH 中
要启动 Caddy 守护进程,请使用 run
子命令:
1 | caddy run |
curl localhost:2019/config/
1 |
|
{
“apps”: {
“http”: {
“servers”: {
“example”: {
“listen”: [“:2015”],
“routes”: [
{
“handle”: [{
“handler”: “static_response”,
“body”: “Hello, world!”
}]
}
]
}
}
}
}
}
1 |
|
curl localhost:2019/load \ -H “Content-Type: application/json” \ -d @caddy.json
1 |
|
curl localhost:2019/config/
1 |
|
curl localhost:2015Hello, world!
1 |
|
{ “handler”: “static_response”, “body”: “I can do hard things.”}
1 |
|
curl localhost:2019/load \ -H “Content-Type: application/json” \ -d @caddy.json
1 |
|
curl localhost:2019/config/
1 |
|
curl \ localhost:2019/config/apps/http/servers/example/routes/0/handle/0/body \ -H “Content-Type: application/json” \ -d ‘“Work smarter, not harder.”’
1 |
|
curl localhost:2019/config/apps/http/servers/example/routes
1 |
|
[{“handle”:[{“body”:“Work smarter, not harder.”,“handler”:“static_response”}]}]
1 |
|
curl \ localhost:2019/config/apps/http/servers/example/routes/0/handle/0/@id \ -H “Content-Type: application/json” \ -d ‘“msg”’
1 |
|
{ “@id”: “msg”, “body”: “Work smarter, not harder.”, “handler”: “static_response”}
1 |
|
curl localhost:2019/id/msg
1 |
|
curl \ localhost:2019/id/msg/body \ -H “Content-Type: application/json” \ -d ‘“Some shortcuts are good.”’
1 |
|
curl localhost:2019/id/msg/body
