-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (58 loc) · 1.4 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
package main
import (
"log"
"time"
"github.com/gofiber/fiber/v2"
"github.com/google/go-github/v43/github"
"github.com/jellydator/ttlcache/v3"
)
func main() {
app := fiber.New()
hub := github.NewClient(nil)
cache := ttlcache.New(
ttlcache.WithTTL[string, fiber.Map](30 * time.Minute),
)
go cache.Start()
app.Get("/releases/latest", func(c *fiber.Ctx) error {
cached := cache.Get("cached-release")
if cached == nil {
log.Println("Release not in cache")
release, _, err := hub.Repositories.GetLatestRelease(
c.UserContext(),
"picotorrent",
"picotorrent")
if err != nil {
return c.Status(500).SendString(err.Error())
}
markdown, _, err := hub.Markdown(
c.UserContext(),
*release.Body,
&github.MarkdownOptions{
Mode: "gfm",
Context: "picotorrent/picotorrent",
})
if err != nil {
return c.Status(500).SendString(err.Error())
}
// map assets
var assets []fiber.Map
for i := 0; i < len(release.Assets); i++ {
assets = append(assets, fiber.Map{
"name": release.Assets[i].Name,
"url": release.Assets[i].BrowserDownloadURL,
})
}
cached = cache.Set(
"cached-release",
fiber.Map{
"assets": assets,
"notes": markdown,
"url": *release.HTMLURL,
"version": *release.TagName,
},
30*time.Minute)
}
return c.JSON(cached.Value())
})
log.Fatal(app.Listen(":3000"))
}