github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/experimental/clashapi/ruleprovider.go (about)

     1  package clashapi
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/go-chi/chi/v5"
     7  	"github.com/go-chi/render"
     8  )
     9  
    10  func ruleProviderRouter() http.Handler {
    11  	r := chi.NewRouter()
    12  	r.Get("/", getRuleProviders)
    13  
    14  	r.Route("/{name}", func(r chi.Router) {
    15  		r.Use(parseProviderName, findRuleProviderByName)
    16  		r.Get("/", getRuleProvider)
    17  		r.Put("/", updateRuleProvider)
    18  	})
    19  	return r
    20  }
    21  
    22  func getRuleProviders(w http.ResponseWriter, r *http.Request) {
    23  	render.JSON(w, r, render.M{
    24  		"providers": []string{},
    25  	})
    26  }
    27  
    28  func getRuleProvider(w http.ResponseWriter, r *http.Request) {
    29  	// provider := r.Context().Value(CtxKeyProvider).(provider.RuleProvider)
    30  	// render.JSON(w, r, provider)
    31  	render.NoContent(w, r)
    32  }
    33  
    34  func updateRuleProvider(w http.ResponseWriter, r *http.Request) {
    35  	/*provider := r.Context().Value(CtxKeyProvider).(provider.RuleProvider)
    36  	if err := provider.Update(); err != nil {
    37  		render.Status(r, http.StatusServiceUnavailable)
    38  		render.JSON(w, r, newError(err.Error()))
    39  		return
    40  	}*/
    41  	render.NoContent(w, r)
    42  }
    43  
    44  func findRuleProviderByName(next http.Handler) http.Handler {
    45  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    46  		/*name := r.Context().Value(CtxKeyProviderName).(string)
    47  		providers := tunnel.RuleProviders()
    48  		provider, exist := providers[name]
    49  		if !exist {*/
    50  		render.Status(r, http.StatusNotFound)
    51  		render.JSON(w, r, ErrNotFound)
    52  		//return
    53  		//}
    54  
    55  		// ctx := context.WithValue(r.Context(), CtxKeyProvider, provider)
    56  		// next.ServeHTTP(w, r.WithContext(ctx))
    57  	})
    58  }