Chore: improve code architecture
This commit is contained in:
40
hub/route/rules.go
Normal file
40
hub/route/rules.go
Normal file
@ -0,0 +1,40 @@
|
||||
package route
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
T "github.com/Dreamacro/clash/tunnel"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/render"
|
||||
)
|
||||
|
||||
func ruleRouter() http.Handler {
|
||||
r := chi.NewRouter()
|
||||
r.Get("/", getRules)
|
||||
return r
|
||||
}
|
||||
|
||||
type Rule struct {
|
||||
Type string `json:"type"`
|
||||
Payload string `json:"payload"`
|
||||
Proxy string `json:"proxy"`
|
||||
}
|
||||
|
||||
func getRules(w http.ResponseWriter, r *http.Request) {
|
||||
rawRules := T.Instance().Rules()
|
||||
|
||||
var rules []Rule
|
||||
for _, rule := range rawRules {
|
||||
rules = append(rules, Rule{
|
||||
Type: rule.RuleType().String(),
|
||||
Payload: rule.Payload(),
|
||||
Proxy: rule.Adapter(),
|
||||
})
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
render.Respond(w, r, map[string][]Rule{
|
||||
"rules": rules,
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user