-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (80 loc) · 1.89 KB
/
main.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
package main
import (
"fmt"
"os"
"strconv"
"github.com/gin-gonic/gin"
"github.com/spf13/pflag"
)
const (
typeClones = "clones"
)
type req struct {
GitUser string `form:"git_user" binding:"required"`
GitRepo string `form:"git_repo" binding:"required"`
// "count" or "uniques"
Type string `form:"type" binding:"required"`
Label string `form:"label" binding:"required"`
}
// reference: https://shields.io/endpoint
type response struct {
SchemaVersion int `json:"schemaVersion"`
Label string `json:"label"`
Message string `json:"message"`
Color string `json:"color"`
}
type errResponse struct {
Message string `json:"message"`
}
func main() {
var githubToken string
pflag.StringVarP(
&githubToken,
"token",
"t",
"",
"Your github personal access token(https://github.com/settings/tokens)",
)
pflag.Parse()
if githubToken == "" {
fmt.Fprint(os.Stderr, "must provide your github token by flag --token/-t")
os.Exit(1)
}
router := gin.Default()
router.GET("/v1/github/traffic/clones/total", func(c *gin.Context) {
var r req
if err := c.ShouldBindQuery(&r); err != nil {
c.JSON(400, errResponse{
Message: err.Error(),
})
return
}
githubClones, err := getGithubTrafficClones(r.GitUser, r.GitRepo, githubToken)
if err != nil {
c.JSON(500, errResponse{
Message: err.Error(),
})
return
}
updateGithubTrafficClones(githubClones.Clones, r.GitUser, r.GitRepo)
total := getClonesTotal(r.GitUser, r.GitRepo)
var typeTotal int
if r.Type == "count" {
typeTotal = total.Count
} else if r.Type == "uniques" {
typeTotal = total.Uniques
} else {
c.JSON(400, errResponse{
Message: "type must be 'count' or 'uniques'",
})
return
}
c.JSON(200, response{
SchemaVersion: 1,
Label: r.Label,
Message: strconv.Itoa(typeTotal),
Color: "orange",
})
})
_ = router.Run(":9000")
}