github.com/sagernet/sing-box@v1.9.0-rc.20/experimental/clashapi/provider.go (about) 1 package clashapi 2 3 import ( 4 "context" 5 "net/http" 6 7 "github.com/go-chi/chi/v5" 8 "github.com/go-chi/render" 9 ) 10 11 func proxyProviderRouter() http.Handler { 12 r := chi.NewRouter() 13 r.Get("/", getProviders) 14 15 r.Route("/{name}", func(r chi.Router) { 16 r.Use(parseProviderName, findProviderByName) 17 r.Get("/", getProvider) 18 r.Put("/", updateProvider) 19 r.Get("/healthcheck", healthCheckProvider) 20 }) 21 return r 22 } 23 24 func getProviders(w http.ResponseWriter, r *http.Request) { 25 render.JSON(w, r, render.M{ 26 "providers": render.M{}, 27 }) 28 } 29 30 func getProvider(w http.ResponseWriter, r *http.Request) { 31 /*provider := r.Context().Value(CtxKeyProvider).(provider.ProxyProvider) 32 render.JSON(w, r, provider)*/ 33 render.NoContent(w, r) 34 } 35 36 func updateProvider(w http.ResponseWriter, r *http.Request) { 37 /*provider := r.Context().Value(CtxKeyProvider).(provider.ProxyProvider) 38 if err := provider.Update(); err != nil { 39 render.Status(r, http.StatusServiceUnavailable) 40 render.JSON(w, r, newError(err.Error())) 41 return 42 }*/ 43 render.NoContent(w, r) 44 } 45 46 func healthCheckProvider(w http.ResponseWriter, r *http.Request) { 47 /*provider := r.Context().Value(CtxKeyProvider).(provider.ProxyProvider) 48 provider.HealthCheck()*/ 49 render.NoContent(w, r) 50 } 51 52 func parseProviderName(next http.Handler) http.Handler { 53 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 54 name := getEscapeParam(r, "name") 55 ctx := context.WithValue(r.Context(), CtxKeyProviderName, name) 56 next.ServeHTTP(w, r.WithContext(ctx)) 57 }) 58 } 59 60 func findProviderByName(next http.Handler) http.Handler { 61 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 62 /*name := r.Context().Value(CtxKeyProviderName).(string) 63 providers := tunnel.ProxyProviders() 64 provider, exist := providers[name] 65 if !exist {*/ 66 render.Status(r, http.StatusNotFound) 67 render.JSON(w, r, ErrNotFound) 68 //return 69 //} 70 71 // ctx := context.WithValue(r.Context(), CtxKeyProvider, provider) 72 // next.ServeHTTP(w, r.WithContext(ctx)) 73 }) 74 }