|
| 1 | +package lark |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "sync" |
| 9 | + |
| 10 | + "github.com/go-chi/chi/v5" |
| 11 | + "github.com/go-chi/chi/v5/middleware" |
| 12 | + "github.com/go-kit/log" |
| 13 | + "github.com/go-kit/log/level" |
| 14 | + |
| 15 | + "github.com/timonwong/prometheus-webhook-dingtalk/config" |
| 16 | + "github.com/timonwong/prometheus-webhook-dingtalk/notifier" |
| 17 | + "github.com/timonwong/prometheus-webhook-dingtalk/pkg/chilog" |
| 18 | + "github.com/timonwong/prometheus-webhook-dingtalk/pkg/models" |
| 19 | + "github.com/timonwong/prometheus-webhook-dingtalk/template" |
| 20 | +) |
| 21 | + |
| 22 | +type API struct { |
| 23 | + // Protect against config, template and http client |
| 24 | + mtx sync.RWMutex |
| 25 | + |
| 26 | + conf *config.Config |
| 27 | + tmpl *template.Template |
| 28 | + targets map[string]config.Target |
| 29 | + httpClient *http.Client |
| 30 | + logger log.Logger |
| 31 | +} |
| 32 | + |
| 33 | +func NewAPI(logger log.Logger) *API { |
| 34 | + return &API{ |
| 35 | + logger: logger, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func (api *API) Update(conf *config.Config, tmpl *template.Template) { |
| 40 | + api.mtx.Lock() |
| 41 | + defer api.mtx.Unlock() |
| 42 | + |
| 43 | + api.conf = conf |
| 44 | + api.tmpl = tmpl |
| 45 | + |
| 46 | + tmp := make(map[string]config.Target) |
| 47 | + for k, v := range conf.Targets { |
| 48 | + if strings.Contains(v.URL.Host, "open.larksuite.com") { |
| 49 | + tmp[k] = v |
| 50 | + } |
| 51 | + } |
| 52 | + api.targets = tmp |
| 53 | + |
| 54 | + api.httpClient = &http.Client{ |
| 55 | + Transport: &http.Transport{ |
| 56 | + Proxy: http.ProxyFromEnvironment, |
| 57 | + DisableKeepAlives: true, |
| 58 | + }, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func (api *API) Routes() chi.Router { |
| 63 | + router := chi.NewRouter() |
| 64 | + router.Use(middleware.RealIP) |
| 65 | + router.Use(middleware.RequestLogger(&chilog.KitLogger{Logger: api.logger})) |
| 66 | + router.Use(middleware.Recoverer) |
| 67 | + router.Post("/{name}/send", api.serveSend) |
| 68 | + return router |
| 69 | +} |
| 70 | + |
| 71 | +func (api *API) serveSend(w http.ResponseWriter, r *http.Request) { |
| 72 | + api.mtx.RLock() |
| 73 | + targets := api.targets |
| 74 | + conf := api.conf |
| 75 | + tmpl := api.tmpl |
| 76 | + httpClient := api.httpClient |
| 77 | + api.mtx.RUnlock() |
| 78 | + |
| 79 | + targetName := chi.URLParam(r, "name") |
| 80 | + logger := log.With(api.logger, "target", targetName) |
| 81 | + |
| 82 | + target, ok := targets[targetName] |
| 83 | + if !ok { |
| 84 | + level.Warn(logger).Log("msg", "target not found") |
| 85 | + http.NotFound(w, r) |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + var promMessage models.WebhookMessage |
| 90 | + if err := json.NewDecoder(r.Body).Decode(&promMessage); err != nil { |
| 91 | + level.Error(logger).Log("msg", "Cannot decode prometheus webhook JSON request", "err", err) |
| 92 | + http.Error(w, "Bad Request", http.StatusBadRequest) |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + builder := notifier.NewDingNotificationBuilder(tmpl, conf, &target) |
| 97 | + notification, err := builder.BuildLark(&promMessage) |
| 98 | + if err != nil { |
| 99 | + level.Error(logger).Log("msg", "Failed to build notification", "err", err) |
| 100 | + http.Error(w, "Bad Request", http.StatusBadRequest) |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + robotResp, err := notifier.SendNotification(notification, httpClient, &target) |
| 105 | + if err != nil { |
| 106 | + level.Error(logger).Log("msg", "Failed to send notification", "err", err) |
| 107 | + http.Error(w, "Bad Request", http.StatusBadRequest) |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + if robotResp.Code != 0 { |
| 112 | + level.Error(logger).Log("msg", "Failed to send notification to DingTalk", "respCode", robotResp.Code, "respMsg", robotResp.Msg) |
| 113 | + http.Error(w, "Unable to talk to DingTalk", http.StatusBadRequest) |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + io.WriteString(w, "OK") |
| 118 | +} |
0 commit comments