-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
125 lines (121 loc) · 3.77 KB
/
server.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
package main
import (
"context"
"log"
"net/http"
"net/url"
"os"
"path"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/bson"
)
func main() {
var err error
if len(os.Args) < 2 {
err = godotenv.Load(".env")
} else {
err = godotenv.Load(path.Join(os.Args[1], ".env"))
}
if err != nil {
log.Fatalf("Error occured loading environment variables: %s", err)
}
r := gin.Default()
var collections Collections
ConnectDB(&collections)
dur := time.Duration(4 * time.Hour)
periodicChannel := time.Tick(dur)
go func() {
for {
<-periodicChannel
res := collections.Config.FindOne(context.Background(), bson.M{})
if res == nil {
log.Fatalf("Could not retrieve server config")
}
var config ServerConfig
err = res.Decode(&config)
if err != nil {
log.Fatalf("Error occured while decoding the server config")
}
wakatimeExpiry, err := time.Parse(time.RFC3339, config.Wakatime.ExpiresAt)
if err != nil {
diff := time.Until(wakatimeExpiry)
if diff.Hours() < 24.0 {
reqData := url.Values{
"client_id": {config.Wakatime.ClientID},
"client_secret": {config.Wakatime.ClientSecret},
"redirect_uri": {"https://qat.dev"},
"refresh_token": {config.Wakatime.RefreshToken},
"grant_type": {"refresh_token"},
}
resp, err := http.PostForm(config.Wakatime.RefreshURL, reqData)
if err != nil {
log.Fatalf("Error occured while refreshing the Wakatime token")
}
var bodyBytes []byte
_, err = resp.Body.Read(bodyBytes)
if err != nil {
log.Fatalf("Error while reading the bytes of the response of refreshing the Wakatime token")
}
bodyStr := string(bodyBytes)
if strings.Contains(bodyStr, "&") {
vals := strings.Split(bodyStr, "&")
for i := 0; i < len(vals); i++ {
if strings.Contains(vals[i], "=") {
itemSplit := strings.Split(vals[i], "=")
switch itemSplit[0] {
case "access_token":
config.Wakatime.AccessToken = itemSplit[1]
case "refresh_token":
config.Wakatime.RefreshToken = itemSplit[1]
case "expires_at":
{
expStr, err := url.QueryUnescape(itemSplit[1])
if err != nil {
log.Fatalf("Error while decoding token expiry date string")
}
config.Wakatime.ExpiresAt = expStr
}
}
} else {
log.Fatalf("Error parsing item at index %d in the response of refreshing the wakatime token", i)
}
}
updateRes, err := collections.Config.UpdateOne(context.Background(), bson.M{},
bson.M{"$set": bson.M{
"wakatime.accessToken": config.Wakatime.AccessToken,
"wakatime.refreshToken": config.Wakatime.RefreshToken,
"wakatime.expiresAt": config.Wakatime.ExpiresAt}})
if err != nil {
log.Fatalf("Error while updating Wakatime configuration")
}
if updateRes.ModifiedCount != 1 {
log.Fatalf("Updating Wakatime configuration failed")
}
} else {
log.Fatalf("Error while parsing the response for refreshing Wakatime token")
}
}
}
}
}()
if len(os.Args) != 2 {
os.RemoveAll(os.Getenv("COMPILE_DIR"))
} else {
os.RemoveAll(path.Join(os.Args[1], os.Getenv("COMPILE_DIR")))
}
r.POST("/compile", compileHandler)
r.GET("/releases", releaseListHandler(&collections))
r.POST("/downloadedRelease", downloadedReleaseHandler(&collections))
r.POST("/newCommits", newCommitsHandler(&collections))
r.GET("/latestCommit", latestCommitHandler(&collections))
r.GET("/releaseCount", releaseCountHandler(&collections))
r.GET("/projectStats", projectStatsHandler(&collections))
err = r.Run(os.Getenv("HOST") + ":" + os.Getenv("PORT"))
if err != nil {
log.Println("Server connection failed")
panic(err)
}
}