random-api-go/handler/url_stats.go

23 lines
491 B
Go

package handler
import (
"encoding/json"
"net/http"
"random-api-go/service"
)
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 := service.GetURLCounts()
if err := json.NewEncoder(w).Encode(stats); err != nil {
http.Error(w, "Error encoding response", http.StatusInternalServerError)
return
}
}