mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2024-11-15 13:41:23 +08:00
56 lines
994 B
Go
56 lines
994 B
Go
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"`
|
|
Proxy string `json:"proxy"`
|
|
}
|
|
|
|
type GetRulesResponse struct {
|
|
Rules []Rule `json:"rules"`
|
|
}
|
|
|
|
func getRules(w http.ResponseWriter, r *http.Request) {
|
|
rawRules := cfg.Rules()
|
|
|
|
var rules []Rule
|
|
for _, rule := range rawRules {
|
|
rules = append(rules, Rule{
|
|
Name: rule.RuleType().String(),
|
|
Payload: rule.Payload(),
|
|
Proxy: rule.Adapter(),
|
|
})
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
render.JSON(w, r, GetRulesResponse{
|
|
Rules: rules,
|
|
})
|
|
}
|
|
|
|
func updateRules(w http.ResponseWriter, r *http.Request) {
|
|
err := cfg.UpdateRules()
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
render.JSON(w, r, Error{
|
|
Error: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|