1
1
package controller
2
2
3
3
import (
4
+ "errors"
4
5
"fmt"
5
6
"html/template"
6
7
"io/fs"
@@ -45,7 +46,7 @@ func IntValueFormatter(v interface{}) string {
45
46
//
46
47
// nolint: funlen
47
48
// TODO: refactor.
48
- func GetRepoChart (github * github.GitHub , cache * cache.Redis ) http.HandlerFunc {
49
+ func GetRepoChart (gh * github.GitHub , cache * cache.Redis ) http.HandlerFunc {
49
50
return func (w http.ResponseWriter , r * http.Request ) {
50
51
name := fmt .Sprintf (
51
52
"%s/%s" ,
@@ -54,13 +55,24 @@ func GetRepoChart(github *github.GitHub, cache *cache.Redis) http.HandlerFunc {
54
55
)
55
56
log := log .WithField ("repo" , name )
56
57
defer log .Trace ("collect_stars" ).Stop (nil )
57
- repo , err := github .RepoDetails (r .Context (), name )
58
+ repo , err := gh .RepoDetails (r .Context (), name )
58
59
if err != nil {
59
60
http .Error (w , err .Error (), http .StatusBadRequest )
60
61
return
61
62
}
62
- stargazers , err := github .Stargazers (r .Context (), repo )
63
+
64
+ w .Header ().Add ("content-type" , "image/svg+xml;charset=utf-8" )
65
+ w .Header ().Add ("cache-control" , "public, max-age=86400" )
66
+ w .Header ().Add ("date" , time .Now ().Format (time .RFC1123 ))
67
+ w .Header ().Add ("expires" , time .Now ().Format (time .RFC1123 ))
68
+
69
+ stargazers , err := gh .Stargazers (r .Context (), repo )
70
+ if errors .Is (err , github .ErrTooManyStars ) {
71
+ w .Write ([]byte (errSvg (err )))
72
+ return
73
+ }
63
74
if err != nil {
75
+ log .WithError (err ).Error ("failed to get stars" )
64
76
http .Error (w , err .Error (), http .StatusServiceUnavailable )
65
77
return
66
78
}
@@ -119,12 +131,14 @@ func GetRepoChart(github *github.GitHub, cache *cache.Redis) http.HandlerFunc {
119
131
Series : []chart.Series {series },
120
132
}
121
133
defer log .Trace ("chart" ).Stop (& err )
122
- w .Header ().Add ("content-type" , "image/svg+xml;charset=utf-8" )
123
- w .Header ().Add ("cache-control" , "public, max-age=86400" )
124
- w .Header ().Add ("date" , time .Now ().Format (time .RFC1123 ))
125
- w .Header ().Add ("expires" , time .Now ().Format (time .RFC1123 ))
126
134
if err := graph .Render (chart .SVG , w ); err != nil {
127
135
log .WithError (err ).Error ("failed to render graph" )
128
136
}
129
137
}
130
138
}
139
+
140
+ func errSvg (err error ) string {
141
+ return fmt .Sprintf (`<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1024" height="50">
142
+ <text xmlns="http://www.w3.org/2000/svg" y="20" x="100" fill="red">%s</text>
143
+ </svg>` , err .Error ())
144
+ }
0 commit comments