-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
56 lines (51 loc) · 1.52 KB
/
main.go
File metadata and controls
56 lines (51 loc) · 1.52 KB
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
package main
import (
"net/http"
"time"
"github.com/jetsly/github-missing-api/trending"
"github.com/gin-gonic/gin"
"github.com/patrickmn/go-cache"
)
func getCacheKey(language string, since string, category string) string {
var cacheKey = "nolang" + since
if language != "" {
cacheKey = "" + language + since
}
return category + "::" + cacheKey
}
func main() {
const port = "8000"
goCache := cache.New(1*time.Hour, 1*time.Hour)
r := gin.Default()
trend := r.Group("/trending")
{
trend.GET("/languages", func(c *gin.Context) {
c.JSON(http.StatusOK, trending.Langs)
})
trend.GET("/repositories", func(c *gin.Context) {
var language = c.Query("language")
var since = c.DefaultQuery("since", "daily")
var cacheKey = getCacheKey(language, since, "repositories")
if json, found := goCache.Get(cacheKey); found {
c.JSON(http.StatusOK, json)
} else {
var json = trending.FetchRepositories(language, since)
goCache.Set(cacheKey, json, cache.DefaultExpiration)
c.JSON(http.StatusOK, json)
}
})
trend.GET("/developers", func(c *gin.Context) {
var language = c.Query("language")
var since = c.DefaultQuery("since", "daily")
var cacheKey = getCacheKey(language, since, "developers")
if json, found := goCache.Get(cacheKey); found {
c.JSON(http.StatusOK, json)
} else {
var json = trending.FetchDevelopers(language, since)
goCache.Set(cacheKey, json, cache.DefaultExpiration)
c.JSON(http.StatusOK, json)
}
})
}
r.Run(":" + port) // listen and serve on 0.0.0.0:8080
}