Skip to content

Commit

Permalink
added /get route
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonLovesDoggo committed May 25, 2024
1 parent 706337c commit 720be24
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ func main() {
context.JSON(http.StatusOK, gin.H{
"status": "ok", "uptime": time.Since(startTime).String()})
})
route.GET("/docs", func(context *gin.Context) {
context.Redirect(http.StatusPermanentRedirect, DocsUrl)
})


route.GET("/get/:namespace/*key", GetView)

route.GET("/hit/:namespace/*key", HitView)
route.GET("/stream/:namespace/*key", middleware.SSEMiddleware(), StreamValueView)
Expand Down
31 changes: 31 additions & 0 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,37 @@ func HitView(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"value": val})
}


func GetView(c *gin.Context) {
namespace, key := utils.GetNamespaceKey(c)
if namespace == "" || key == "" {
return
}
dbKey := utils.CreateKey(c, namespace, key, false)
if dbKey == "" { // error is handled in CreateKey
return
}
// Get data from Redis
val, err := Client.Get(context.Background(), dbKey).Result()


if err == redis.Nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Key not found"})
return
} else if err != nil { // Other Redis errors
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get data. Try again later."})
return
}

go func() {
Client.Expire(context.Background(), dbKey, utils.BaseTTLPeriod)
}()

c.JSON(http.StatusOK, gin.H{"value": val})
}



func CreateRandomView(c *gin.Context) {
key, _ := utils.GenerateRandomString(16)
namespace, err := utils.GenerateRandomString(16)
Expand Down

0 comments on commit 720be24

Please sign in to comment.