(urCfg usagestats.Config)
| 850 | } |
| 851 | |
| 852 | func usageStatsHandler(urCfg usagestats.Config) http.HandlerFunc { |
| 853 | if !urCfg.Enabled { |
| 854 | return func(w http.ResponseWriter, r *http.Request) { |
| 855 | http.Error(w, "usage-stats is not enabled", http.StatusOK) |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | // usage stats is Enabled, build and return usage stats json |
| 860 | reportBytes, err := json.Marshal(usagestats.BuildStats()) |
| 861 | if err != nil { |
| 862 | return func(w http.ResponseWriter, r *http.Request) { |
| 863 | http.Error(w, "error building usage report", http.StatusInternalServerError) |
| 864 | } |
| 865 | } |
| 866 | reportStr := string(reportBytes) |
| 867 | |
| 868 | return func(w http.ResponseWriter, r *http.Request) { |
| 869 | w.WriteHeader(http.StatusOK) |
| 870 | w.Header().Set("Content-Type", "application/json") |
| 871 | _, _ = io.WriteString(w, reportStr) |
| 872 | } |
| 873 | } |
no test coverage detected