Feature:Supported Rule-Set
This commit is contained in:
@ -75,3 +75,54 @@ func findProviderByName(next http.Handler) http.Handler {
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
func ruleProviderRouter() http.Handler {
|
||||
r := chi.NewRouter()
|
||||
r.Get("/", getRuleProviders)
|
||||
r.Route("/{name}", func(r chi.Router) {
|
||||
r.Use(parseRuleProviderName, findRuleProviderByName)
|
||||
r.Put("/", updateRuleProvider)
|
||||
})
|
||||
return r
|
||||
}
|
||||
|
||||
func getRuleProviders(w http.ResponseWriter, r *http.Request) {
|
||||
ruleProviders := tunnel.RuleProviders()
|
||||
render.JSON(w, r, render.M{
|
||||
"providers": ruleProviders,
|
||||
})
|
||||
}
|
||||
|
||||
func updateRuleProvider(w http.ResponseWriter, r *http.Request) {
|
||||
provider := r.Context().Value(CtxKeyProvider).(*provider.RuleProvider)
|
||||
if err := (*provider).Update(); err != nil {
|
||||
render.Status(r, http.StatusServiceUnavailable)
|
||||
render.JSON(w, r, newError(err.Error()))
|
||||
}
|
||||
|
||||
render.NoContent(w, r)
|
||||
}
|
||||
|
||||
func parseRuleProviderName(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
name := getEscapeParam(r, "name")
|
||||
ctx := context.WithValue(r.Context(), CtxKeyProviderName, name)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
func findRuleProviderByName(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
name := r.Context().Value(CtxKeyProviderName).(string)
|
||||
providers := tunnel.RuleProviders()
|
||||
provider, exist := providers[name]
|
||||
if !exist {
|
||||
render.Status(r, http.StatusNotFound)
|
||||
render.JSON(w, r, ErrNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.WithValue(r.Context(), CtxKeyProvider, provider)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
@ -70,6 +70,7 @@ func Start(addr string, secret string) {
|
||||
r.Mount("/rules", ruleRouter())
|
||||
r.Mount("/connections", connectionRouter())
|
||||
r.Mount("/providers/proxies", proxyProviderRouter())
|
||||
r.Mount("/providers/rules", ruleProviderRouter())
|
||||
})
|
||||
|
||||
if uiPath != "" {
|
||||
|
Reference in New Issue
Block a user