Skip to content

Commit

Permalink
Merge pull request #334 from Zhang21/feat-hotreload
Browse files Browse the repository at this point in the history
Feat: hotreload conf
  • Loading branch information
feiyu563 committed Aug 28, 2023
2 parents 6279ad6 + 039805a commit d4afcdd
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ curl http://localhost:8080/health

<br/>

=======
-------------------------------------

## 启动
Expand Down Expand Up @@ -136,6 +135,7 @@ feiyu563/prometheus-alert:latest
- 增加告警语音播报插件。
- 增加支持飞书机器人应用。
- 增加告警组,可以将通知媒介写到告警组里面,便于配置和修改。
- 增加热加载配置接口

-------------------------------------

Expand Down Expand Up @@ -198,6 +198,7 @@ feiyu563/prometheus-alert:latest
* [语音播报](doc/readme/conf-voice.md)
* [飞书机器人应用](doc/readme/conf-feishuapp.md)
* [告警组配置](doc/readme/alertgroup.md)
* [热加载配置](doc/readme/hotreload.md)

* [【告警系统接入PrometheusAlert配置】](doc/readme/system.md)
* [Prometheus 接入配置](doc/readme/system-prometheus.md)
Expand Down
2 changes: 2 additions & 0 deletions conf/app-example.conf
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ to_es_url=http://localhost:9200
# to_es_pwd=password
# 长连接最大空闲数
maxIdleConns=100
# 热更新配置文件
open-hotreload=0

#---------------------↓webhook-----------------------
#是否开启钉钉告警通道,可同时开始多个通道0为关闭,1为开启
Expand Down
36 changes: 36 additions & 0 deletions controllers/hotreload.go
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?
33 changes: 33 additions & 0 deletions doc/readme/hotreload.md
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
```
6 changes: 3 additions & 3 deletions routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ func init() {
beego.Router("/gitlab/weixin", &controllers.GitlabController{}, "post:GitlabWeixin")
beego.Router("/gitlab/dingding", &controllers.GitlabController{}, "post:GitlabDingding")
beego.Router("/gitlab/feishu", &controllers.GitlabController{}, "post:GitlabFeishu")
// Todo
// Email
// Feishu

// hotreload
beego.Router("/-/reload", &controllers.ConfigController{}, "post:Reload")

//已经下线的接口
//beego.Router("/prometheus/dingding", &controllers.PrometheusController{},"post:PrometheusRouter")
Expand Down

0 comments on commit d4afcdd

Please sign in to comment.