|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + |
| 9 | + cu "github.com/sonichigo/hg/common" |
| 10 | +) |
| 11 | + |
| 12 | +func HandleRepoStats(w http.ResponseWriter, r *http.Request) { |
| 13 | + if r.Method != http.MethodGet { |
| 14 | + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
| 15 | + return |
| 16 | + } |
| 17 | + |
| 18 | + repoURL := r.URL.Query().Get("repo") |
| 19 | + if repoURL == "" { |
| 20 | + http.Error(w, "Repository URL is required", http.StatusBadRequest) |
| 21 | + return |
| 22 | + } |
| 23 | + |
| 24 | + owner, repo, err := extractRepoInfo(repoURL) |
| 25 | + if err != nil { |
| 26 | + http.Error(w, fmt.Sprintf("Invalid repository URL: %v", err), http.StatusBadRequest) |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + // Make token optional |
| 31 | + var config *cu.Config |
| 32 | + authHeader := r.Header.Get("Authorization") |
| 33 | + if authHeader != "" { |
| 34 | + token := strings.TrimPrefix(authHeader, "Bearer ") |
| 35 | + token = strings.TrimSpace(token) |
| 36 | + config = &cu.Config{GithubToken: token} |
| 37 | + } |
| 38 | + |
| 39 | + releases, err := getAllReleases(owner, repo, config) |
| 40 | + if err != nil { |
| 41 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + stats := calculateDownloadStats(releases) |
| 46 | + stats.RepoName = fmt.Sprintf("%s/%s", owner, repo) |
| 47 | + |
| 48 | + w.Header().Set("Content-Type", "application/json") |
| 49 | + json.NewEncoder(w).Encode(stats) |
| 50 | +} |
| 51 | +func HandleStarHistory(w http.ResponseWriter, r *http.Request) { |
| 52 | + if r.Method != http.MethodGet { |
| 53 | + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + // Get repositories from query parameter |
| 58 | + repos := r.URL.Query()["repo"] |
| 59 | + if len(repos) == 0 { |
| 60 | + http.Error(w, "At least one repository URL is required", http.StatusBadRequest) |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + // Make token optional |
| 65 | + var config *cu.Config |
| 66 | + authHeader := r.Header.Get("Authorization") |
| 67 | + if authHeader != "" { |
| 68 | + token := strings.TrimPrefix(authHeader, "Bearer ") |
| 69 | + token = strings.TrimSpace(token) |
| 70 | + config = &cu.Config{GithubToken: token} |
| 71 | + } |
| 72 | + |
| 73 | + // Fetch star history for all repositories |
| 74 | + result := cu.MultiRepoStarHistory{ |
| 75 | + Repositories: make([]cu.StarHistory, 0, len(repos)), |
| 76 | + } |
| 77 | + |
| 78 | + for _, repoURL := range repos { |
| 79 | + owner, repo, err := extractRepoInfo(repoURL) |
| 80 | + if err != nil { |
| 81 | + http.Error(w, fmt.Sprintf("Invalid repository URL: %v", err), http.StatusBadRequest) |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + history, err := getStarHistory(owner, repo, config) |
| 86 | + if err != nil { |
| 87 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + result.Repositories = append(result.Repositories, *history) |
| 92 | + } |
| 93 | + |
| 94 | + w.Header().Set("Content-Type", "application/json") |
| 95 | + json.NewEncoder(w).Encode(result) |
| 96 | +} |
| 97 | +func HandleOrgContributors(w http.ResponseWriter, r *http.Request) { |
| 98 | + if r.Method != http.MethodGet { |
| 99 | + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + org := r.URL.Query().Get("org") |
| 104 | + if org == "" { |
| 105 | + http.Error(w, "Organization name is required", http.StatusBadRequest) |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + // Make token optional |
| 110 | + var config *cu.Config |
| 111 | + authHeader := r.Header.Get("Authorization") |
| 112 | + if authHeader != "" { |
| 113 | + token := strings.TrimPrefix(authHeader, "Bearer ") |
| 114 | + token = strings.TrimSpace(token) |
| 115 | + config = &cu.Config{GithubToken: token} |
| 116 | + } |
| 117 | + |
| 118 | + stats, err := getOrgContributors(org, config) |
| 119 | + if err != nil { |
| 120 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + w.Header().Set("Content-Type", "application/json") |
| 125 | + json.NewEncoder(w).Encode(stats) |
| 126 | +} |
0 commit comments