添加 proxy/stats.go
This commit is contained in:
29
proxy/stats.go
Normal file
29
proxy/stats.go
Normal file
@@ -0,0 +1,29 @@
|
||||
// proxy/stats.go
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"siteproxy/cache"
|
||||
)
|
||||
|
||||
type StatsHandler struct {
|
||||
cache *cache.MemoryCache
|
||||
}
|
||||
|
||||
func NewStatsHandler(cache *cache.MemoryCache) *StatsHandler {
|
||||
return &StatsHandler{cache: cache}
|
||||
}
|
||||
|
||||
func (h *StatsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
entries, size := h.cache.Stats()
|
||||
|
||||
stats := map[string]interface{}{
|
||||
"entries": entries,
|
||||
"size": size,
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(stats)
|
||||
}
|
||||
Reference in New Issue
Block a user