-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
106 lines (89 loc) · 2.99 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
99
100
101
102
103
104
105
106
package main
import (
"fmt"
"github.com/kataras/iris/v12"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"os"
"strings"
"time"
)
var logger = logrus.New()
type Formatter struct {
}
func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) {
timestamp := time.Now().Format("2006/01/02 15:04")
level := strings.ToUpper(entry.Level.String())
message := entry.Message
var color string
switch entry.Level {
case logrus.DebugLevel:
color = "\u001B[33m"
case logrus.InfoLevel:
color = "\033[32m"
case logrus.WarnLevel:
color = "\033[33m"
case logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel:
color = "\033[31m"
default:
color = "\033[0m"
}
return []byte(fmt.Sprintf("%s[%s]\u001B[0m %s - %s\n", color, level, timestamp, message)), nil
}
func main() {
logger.SetLevel(logrus.DebugLevel)
logger.SetFormatter(&Formatter{})
_, err := os.Stat("config.yaml")
if err != nil && os.IsNotExist(err) {
logger.Fatal("Config file (config.yaml) not found!")
}
viper.SetConfigFile("config.yaml")
if err := viper.ReadInConfig(); err != nil {
logger.Fatalf("Failed to read config file: %v", err)
}
if err := viper.Unmarshal(&conf); err != nil {
logger.Fatalf("Failed to unmarshal config file: %v", err)
}
tokenList = DetectToken()
SetupCache()
app := iris.Default()
{
app.Get("/api/user/{username:string}", CachedHandler(UserAPI))
app.Get("/api/repo/{username:string}/{repo:string}", CachedHandler(RepoAPI))
app.Get("/api/contributor/{username:string}/{repo:string}", CachedHandler(ContributorAPI))
app.Get("/api/release/{username:string}/{repo:string}/{tag:string}", CachedHandler(ReleaseAPI))
app.Get("/api/issue/{username:string}/{repo:string}/{id:string}", CachedHandler(IssueAPI))
app.Get("/api/pull/{username:string}/{repo:string}/{id:string}", CachedHandler(PullRequestAPI))
}
app.Listen(fmt.Sprintf(":%d", conf.Server.Port))
}
func UserAPI(ctx iris.Context) {
username := ctx.Params().Get("username")
data := AnalysisUser(username)
EndBodyWithCache(ctx, data)
}
func RepoAPI(ctx iris.Context) {
username, repo := ctx.Params().Get("username"), ctx.Params().Get("repo")
data := AnalysisRepo(username, repo)
EndBodyWithCache(ctx, data)
}
func ContributorAPI(ctx iris.Context) {
username, repo := ctx.Params().Get("username"), ctx.Params().Get("repo")
data := AnalysisContributor(username, repo)
EndBodyWithCache(ctx, data)
}
func ReleaseAPI(ctx iris.Context) {
username, repo, tag := ctx.Params().Get("username"), ctx.Params().Get("repo"), ctx.Params().Get("tag")
data := AnalysisRelease(username, repo, tag)
EndBodyWithCache(ctx, data)
}
func IssueAPI(ctx iris.Context) {
username, repo, id := ctx.Params().Get("username"), ctx.Params().Get("repo"), ctx.Params().Get("id")
data := AnalysisIssue(username, repo, id)
EndBodyWithCache(ctx, data)
}
func PullRequestAPI(ctx iris.Context) {
username, repo, id := ctx.Params().Get("username"), ctx.Params().Get("repo"), ctx.Params().Get("id")
data := AnalysisPullRequest(username, repo, id)
EndBodyWithCache(ctx, data)
}