-
Notifications
You must be signed in to change notification settings - Fork 694
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
76 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package controllers | ||
|
||
import ( | ||
"github.com/astaxie/beego" | ||
"github.com/astaxie/beego/logs" | ||
) | ||
|
||
type ConfigController struct { | ||
beego.Controller | ||
} | ||
|
||
var configPath = "conf/app.conf" | ||
|
||
// hot reload config | ||
func (c *ConfigController) Reload() { | ||
logsign := "[" + LogsSign() + "]" | ||
if open := beego.AppConfig.String("open-hotreload"); open != "1" { | ||
logs.Info(logsign, "[hotreload]", "open-hotreload is 0.") | ||
c.Ctx.WriteString("open-hotreload is disable.\n") | ||
return | ||
} | ||
|
||
// apply config file | ||
err := beego.LoadAppConfig("ini", configPath) | ||
if err != nil { | ||
logs.Error(logsign, "[hotreload]", err.Error()) | ||
c.Ctx.ResponseWriter.WriteHeader(500) | ||
c.Ctx.WriteString("Error reloading config: " + err.Error() + "\n") | ||
return | ||
} | ||
|
||
logs.Info(logsign, "[hotreload]", "Config reloaded successfully.") | ||
c.Ctx.WriteString("Config reloaded successfully.\n") | ||
} | ||
|
||
// Add auth middleware or not? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# 热加载配置接口文档 | ||
|
||
热加载配置接口,类似于 Prometheus 的热加载配置接口。 | ||
|
||
配置文件中的配置项为 `open-hotreload`,默认关闭。可以将其设置为 1 来开启这个功能。暂时没有给接口添加认证。 | ||
|
||
> **请注意:** 如果启动之前没有打开热加载,那么在启动程序之后再开启热加载是不生效的(因为内存中的热加载配置还是关闭的,因此程序还是判断未启用热加载),需要重新启动程序。<br> | ||
> **考虑:** 是否需要给这个接口添加认证? | ||
<br/> | ||
|
||
热加载接口原理:使用 `beego.LoadAppConfig("ini", "conf/app.conf")` 来加载配置。响应中会返回成功还是错误的相关信息。 | ||
|
||
<br/> | ||
<br/> | ||
|
||
## 使用方法 | ||
|
||
使用方法: | ||
|
||
1. 配置文件中开启热加载 | ||
2. 启动程序 | ||
3. 修改配置 | ||
4. 热加载配置 | ||
|
||
```bash | ||
# conf/app.conf | ||
# 开启热加载 | ||
open-hotreload=1 | ||
|
||
# 热加载接口 | ||
curl -X POST http://PrometheusAlert:8080/-/reload | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters