-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
196 lines (153 loc) · 5.89 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"context"
"fmt"
"net/http"
"time"
"github.com/google/go-github/v48/github"
"go.mongodb.org/mongo-driver/mongo"
)
// Badge with the number of your GitHub Organizations
func organizationsHandler(client github.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/organizations" {
http.Error(w, "404 not found", http.StatusNotFound)
}
if r.Method != "GET" {
http.Error(w, "Method is not supported", http.StatusNotFound)
}
values := r.URL.Query()
// Get GitHub username, i.e. /organizations?username=dtemir
username := values.Get("username")
// Get shields.io color, default to brightgreen
color := values.Get("color")
if len(color) == 0 {
color = "brigthgreen"
}
// Get shields.io style, default to empty string (shield.io's flat style)
style := values.Get("style")
// Get logo, default to none
logo := values.Get("logo")
// Fetch the number of Organizations the user has
// docs.github.com/en/rest/orgs/orgs?apiVersion=2022-11-28#list-organizations-for-a-user
orgs, _, err := client.Organizations.List(context.Background(), username, nil)
if err != nil {
WarningLogger.Println("GitHub: Error fetching organization list for user", username, err)
http.Error(w, "Incorrect GitHub username. Please make sure it's correct and try again", http.StatusBadRequest)
}
// Create a shields.io badge
url := fmt.Sprintf("https://img.shields.io/badge/Organizations-%d-%s?style=%s&logo=%s", len(orgs), color, style, logo)
svg := getSVG(url)
InfoLogger.Println("Successfully created a badge for", username, url)
// Display SVG badge
w.Header().Set("content-type", "image/svg+xml")
w.Header().Set("cache-control", "no-cache")
w.WriteHeader(200)
fmt.Fprintf(w, "%s", svg)
}
}
// Badge with the number of years you have been a GitHub member
func yearsHandler(client github.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/years" {
http.Error(w, "404 not found", http.StatusNotFound)
}
if r.Method != "GET" {
http.Error(w, "Method is not supported", http.StatusNotFound)
}
values := r.URL.Query()
username := values.Get("username")
color := values.Get("color")
if len(color) == 0 {
color = "brigthgreen"
}
style := values.Get("style")
logo := values.Get("logo")
// Fetch the user data
// https://docs.github.com/en/rest/users/users?apiVersion=2022-11-28#get-a-user
user, _, err := client.Users.Get(context.Background(), username)
if err != nil {
WarningLogger.Println("GitHub: Error fetching years for user", username, err)
http.Error(w, "Incorrect GitHub username. Please make sure it's correct and try again", http.StatusBadRequest)
}
// Calculate the number of years passed since user creation
created := user.GetCreatedAt().Time
now := time.Now()
years := int64(now.Sub(created).Hours() / 24 / 365)
url := fmt.Sprintf("https://img.shields.io/badge/Years-%d-%s?style=%s&logo=%s", years, color, style, logo)
svg := getSVG(url)
InfoLogger.Println("Successfully created a badge for", username, url)
w.Header().Set("content-type", "image/svg+xml")
w.Header().Set("cache-control", "no-cache")
w.WriteHeader(200)
fmt.Fprintf(w, "%s", svg)
}
}
// Badge with the number of public repos you have
func reposHandler(client github.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/repos" {
http.Error(w, "404 not found", http.StatusNotFound)
}
if r.Method != "GET" {
http.Error(w, "Method is not supported", http.StatusNotFound)
}
values := r.URL.Query()
username := values.Get("username")
color := values.Get("color")
if len(color) == 0 {
color = "brigthgreen"
}
style := values.Get("style")
logo := values.Get("logo")
// Fetch the user data
// https://docs.github.com/en/rest/users/users?apiVersion=2022-11-28#get-a-user
user, _, err := client.Users.Get(context.Background(), username)
if err != nil {
WarningLogger.Println("GitHub: Error fetching repos for user", username, err)
http.Error(w, "Incorrect GitHub username. Please make sure it's correct and try again", http.StatusBadRequest)
}
// Get the number of public repos the user has
repos := user.GetPublicRepos()
url := fmt.Sprintf("https://img.shields.io/badge/Repos-%d-%s?style=%s&logo=%s", repos, color, style, logo)
svg := getSVG(url)
InfoLogger.Println("Successfully created a badge for", username, url)
w.Header().Set("content-type", "image/svg+xml")
w.Header().Set("cache-control", "no-cache")
w.WriteHeader(200)
fmt.Fprintf(w, "%s", svg)
}
}
// Badge with the number of visits your repo had so far
func visitsHandler(client github.Client, collection mongo.Collection) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/visits" {
http.Error(w, "404 not found", http.StatusNotFound)
}
if r.Method != "GET" {
http.Error(w, "Method is not supported", http.StatusNotFound)
}
values := r.URL.Query()
username := values.Get("username")
color := values.Get("color")
if len(color) == 0 {
color = "brigthgreen"
}
style := values.Get("style")
logo := values.Get("logo")
// Get a repo name, i.e. dtemir/github-badges-golang
repo := username + "/" + values.Get("repo")
visits := getVisitsCount(repo, collection)
url := fmt.Sprintf("https://img.shields.io/badge/Visits-%d-%s?style=%s&logo=%s", visits, color, style, logo)
svg := getSVG(url)
InfoLogger.Println("Successfully created a badge for", username, url)
w.Header().Set("content-type", "image/svg+xml")
w.Header().Set("cache-control", "no-cache")
w.WriteHeader(200)
fmt.Fprintf(w, "%s", svg)
}
}
// Show a message if people land at "/" instead of any of the other endpoints
func indexHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://github.com/dtemir/git-badges-golang", http.StatusMovedPermanently)
}