2018-07-12 15:28:38 +00:00
|
|
|
package hub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ruleRouter() http.Handler {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
r.Get("/", getRules)
|
|
|
|
r.Put("/", updateRules)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
type Rule struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Payload string `json:"type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetRulesResponse struct {
|
|
|
|
Rules []Rule `json:"rules"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRules(w http.ResponseWriter, r *http.Request) {
|
2018-07-25 16:04:59 +00:00
|
|
|
rawRules := cfg.Rules()
|
2018-07-12 15:28:38 +00:00
|
|
|
|
|
|
|
var rules []Rule
|
2018-07-25 16:04:59 +00:00
|
|
|
for _, rule := range rawRules {
|
2018-07-12 15:28:38 +00:00
|
|
|
rules = append(rules, Rule{
|
|
|
|
Name: rule.RuleType().String(),
|
|
|
|
Payload: rule.Payload(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
render.JSON(w, r, GetRulesResponse{
|
|
|
|
Rules: rules,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateRules(w http.ResponseWriter, r *http.Request) {
|
2018-07-25 16:04:59 +00:00
|
|
|
err := cfg.UpdateRules()
|
2018-07-12 15:28:38 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
render.JSON(w, r, Error{
|
|
|
|
Error: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|