random-api-go/handlers/url_stats.go

23 lines
494 B
Go

package handlers
import (
"encoding/json"
"net/http"
"random-api-go/services"
)
func HandleURLStats(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "application/json")
stats := services.GetURLCounts()
if err := json.NewEncoder(w).Encode(stats); err != nil {
http.Error(w, "Error encoding response", http.StatusInternalServerError)
return
}
}