This repository has been archived by the owner on Jun 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ivy.go
98 lines (85 loc) · 2.72 KB
/
ivy.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
// Copyright (C) Oussama Ben Brahim - All Rights Reserved
// Use of this source code is governed by a MIT License that can be found in
// https://github.com/benbraou/ivy-status-api/blob/main/LICENSE
package ivy
import (
"io/ioutil"
"log"
"net/http"
"sync"
"time"
"github.com/benbraou/ivy-status-api/business"
"github.com/benbraou/ivy-status-api/constants"
"github.com/benbraou/ivy-status-api/model"
"google.golang.org/appengine"
"google.golang.org/appengine/urlfetch"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func init() {
// Create an empty response that will be sent if no updates on ivy are found in `STATUS.md`
response := emptyResponse()
mutex := &sync.RWMutex{}
lastUpdateTime := time.Now()
initialStatusBuildDone := false
r := gin.New()
r.Use(cors.New(cors.Config{
AllowMethods: []string{"GET"},
AllowHeaders: []string{"Origin"},
ExposeHeaders: []string{"Content-Length"},
AllowAllOrigins: true,
AllowCredentials: true,
}))
r.GET("v1/status", func(c *gin.Context) {
mutex.RLock()
ivyStatusNeedsRefresh := shouldUpdateStatus(lastUpdateTime, initialStatusBuildDone)
mutex.RUnlock()
if ivyStatusNeedsRefresh {
ctx := appengine.NewContext(c.Request)
client := urlfetch.Client(ctx)
md, err := client.Get(constants.MarkdownStatusURL)
if err != nil {
// If we encounter an issue updating ivy status, we will return the currently stored one
// So, for now, we just log the error
log.Println("Error encountered when retrieving Ivy markdown raw status: ", err.Error())
} else {
buf, err := ioutil.ReadAll(md.Body)
if err != nil {
log.Println("Error encountered when reading Ivy markdown raw status body: ", err.Error())
} else {
mutex.Lock()
initialStatusBuildDone = true
lastUpdateTime = time.Now()
response = model.
NewResponseBuilder().
Version(1.0).
Data(
model.
NewIvyStatusBuilder().
LastUpdateDate(lastUpdateTime.Format("2006-01-02T15:04:05")).
RootFeatureGroup(business.ProduceIvyStatus(string(buf))).
Build(),
).
Build()
log.Println("Ivy status successfully updated on: ", lastUpdateTime.Format("2006-01-02T15:04:05"))
mutex.Unlock()
}
}
}
mutex.RLock()
c.JSON(200, response)
mutex.RUnlock()
})
http.Handle("/", r)
}
func emptyResponse() *model.Response {
return model.
NewResponseBuilder().
Version(1.0).
Build()
}
// shouldUpdateStatus indicates when the ivy status need to be `refreshed`.
// For the moment, the update is done when tha api is first served and then every 5 min
func shouldUpdateStatus(lastUpdateTime time.Time, firstUpdateDone bool) bool {
return !firstUpdateDone || time.Since(lastUpdateTime).Minutes() >= 5
}