基于 Gin 和 Teo Go SDK 实现的 CDN 徽章服务。
/cdn-cgi/package/{path} 和 /v1/package/{path}/badge| 变量名 | 必填 | 说明 | 默认值 |
|---|---|---|---|
SECRET_ID | 是 | 腾讯云 SecretId | - |
SECRET_KEY | 是 | 腾讯云 SecretKey | - |
ZONE_ID | 是 | Teo Zone ID | - |
DOMAIN | 否 | 域名过滤,如果设置将只查询该域名的数据 | - |
REDIS_URL | 否 | Redis 连接 URL | 使用内存缓存 |
CACHE_TTL | 否 | 缓存 TTL(秒) | 21600 (6小时) |
PORT | 否 | 服务端口 | 8080 |
cp .env.example .env
.env 文件,填入你的配置:SECRET_ID=your_secret_id_here SECRET_KEY=your_secret_key_here ZONE_ID=your_zone_id_here
go mod tidy
go run main.go
查询指定路径的 CDN 请求量并返回徽章
同上,不同的路由格式
# 查询根路径请求量
curl http://localhost:8080/cdn-cgi/package/
# 查询特定路径请求量
curl http://localhost:8080/cdn-cgi/package/api/users
curl http://localhost:8080/v1/package/api/users/badge
成功时返回 SVG 徽章:
<svg xmlns="http://www.w3.org/2000/svg" width="140" height="20" role="img" aria-label="BringCDN: 1.2k req/month">
<title>BringCDN: 1.2k req/month</title>
<g shape-rendering="crispEdges">
<rect width="70" height="20" fill="#555"/>
<rect x="70" width="70" height="20" fill="#ff5627"/>
</g>
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110">
<text x="350" y="140" transform="scale(.1)" fill="#fff" textLength="520">BringCDN</text>
<text x="1050" y="140" transform="scale(.1)" fill="#fff" textLength="650">1.2k req/month</text>
</g>
</svg>
错误时返回红色徽章并设置错误头信息。
badge:{path}