github.com/sagernet/sing-box@v1.9.0-rc.20/experimental/clashapi/cache.go (about)

     1  package clashapi
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  
     7  	"github.com/sagernet/sing-box/adapter"
     8  	"github.com/sagernet/sing/service"
     9  
    10  	"github.com/go-chi/chi/v5"
    11  	"github.com/go-chi/render"
    12  )
    13  
    14  func cacheRouter(ctx context.Context) http.Handler {
    15  	r := chi.NewRouter()
    16  	r.Post("/fakeip/flush", flushFakeip(ctx))
    17  	return r
    18  }
    19  
    20  func flushFakeip(ctx context.Context) func(w http.ResponseWriter, r *http.Request) {
    21  	return func(w http.ResponseWriter, r *http.Request) {
    22  		cacheFile := service.FromContext[adapter.CacheFile](ctx)
    23  		if cacheFile != nil {
    24  			err := cacheFile.FakeIPReset()
    25  			if err != nil {
    26  				render.Status(r, http.StatusInternalServerError)
    27  				render.JSON(w, r, newError(err.Error()))
    28  				return
    29  			}
    30  		}
    31  		render.NoContent(w, r)
    32  	}
    33  }