Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
update github explore api
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Aug 6, 2023
1 parent 2272ac2 commit 51b0695
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
20 changes: 20 additions & 0 deletions backend/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
"github.com/spf13/viper"
"net/http"
"strings"
Expand Down Expand Up @@ -92,3 +93,22 @@ func RegisterChatGPTAPI(app *gin.Engine) {
ChatGPTAPI(c, body.Message)
})
}

func GithubExploreAPI(c *gin.Context) {
resp, err := GetRandomPopularRepoWithCache(c, c.MustGet("cache").(*redis.Client))

if err != nil {
c.JSON(http.StatusOK, gin.H{
"status": false,
"data": nil,
"reason": err.Error(),
})
return
}

c.JSON(http.StatusOK, gin.H{
"status": true,
"data": resp,
"reason": "",
})
}
38 changes: 36 additions & 2 deletions backend/github.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package main

import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
"github.com/spf13/viper"
"math/rand"
"strconv"
"time"
)

// generate at https://github.com/ozh/github-colors/blob/master/colors.json
Expand Down Expand Up @@ -50,7 +56,8 @@ func GetPopularRepo(page int) ([]PopularRepo, error) {
page = 1
}
data, err := Get("https://api.github.com/search/repositories?q=stars:%3E1000&per_page=100&page="+strconv.Itoa(page), map[string]string{
"Accept": "application/vnd.github.v3+json",
"Accept": "application/vnd.github.v3+json",
"Authorization": "Bearer " + viper.GetString("token"),
})

if err != nil {
Expand Down Expand Up @@ -82,6 +89,33 @@ func HandlePopularRepo(data []GithubRepo) []PopularRepo {
Color: GetColor(v.Language),
})
}
fmt.Println(res)

return res
}

func GetRandomPagination(data []PopularRepo) []PopularRepo {
page := rand.Intn(25)
return data[page*4 : page*4+4]
}

func GetRandomPopularRepoWithCache(ctx *gin.Context, cache *redis.Client) ([]PopularRepo, error) {
page := rand.Intn(10)

if result, err := cache.Get(ctx, fmt.Sprintf("popularepo:%d", page)).Result(); err == nil && result != "" {
var res []PopularRepo
if err = json.Unmarshal([]byte(result), &res); err == nil {
return GetRandomPagination(res), nil
}
}

res, err := GetPopularRepo(page)
if err != nil {
return nil, err
}

if buffer, err := json.Marshal(res); err == nil {
cache.Set(ctx, fmt.Sprintf("popularepo:%d", page), buffer, time.Minute*2)
}

return GetRandomPagination(res), nil
}
2 changes: 1 addition & 1 deletion backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ func main() {
if err := viper.ReadInConfig(); err != nil {
panic(err)
}

if viper.GetBool("debug") {
gin.SetMode(gin.DebugMode)
} else {
Expand All @@ -33,6 +32,7 @@ func main() {
app.POST("/login", LoginAPI)
app.POST("/state", StateAPI)
app.POST("/sync", SyncStorageAPI)
app.GET("/github", GithubExploreAPI)
RegisterChatGPTAPI(app)
}

Expand Down
1 change: 1 addition & 0 deletions backend/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (l *Limiter) RateLimit(ctx *gin.Context, rds *redis.Client, ip string, path
var limits = map[string]Limiter{
"/login": {Duration: 10, Count: 5},
"/state": {Duration: 1, Count: 2},
"/github": {Duration: 10, Count: 5},
"/storage/sync": {Duration: 120, Count: 60},
}

Expand Down

1 comment on commit 51b0695

@vercel
Copy link

@vercel vercel bot commented on 51b0695 Aug 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.