Skip to content

Commit

Permalink
Updated SetView and added UpdateByView (incr/decr by value)
Browse files Browse the repository at this point in the history
This concludes all of the major endpoints in the TODO. the rest should be smaller things like ratelimiting.

+ Added disc config.
  • Loading branch information
JasonLovesDoggo committed Mar 25, 2024
1 parent 0f21ad1 commit 50dab2c
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 13 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea/copilot
.idea
abacus.exe
.env

!.idea/httpRequests
9 changes: 8 additions & 1 deletion .idea/discord.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ I currently use it on my personal website to keep track of the number of visitor
- [x] ~~K8 Deployment~~ (GCP CloudRun + Redis on OCI)
- [x] impl /create endpoint which creates a new counter initialized to 0 and returns a secret key that can be used to modify the counter via the following endpoints
- [x] /delete endpoint
- [ ] /set endpoint
- [ ] /reset (alias to /set 0)
- [ ] /update endpoint (updates the counter x)
- [x] /set endpoint
- [x] /reset (alias to /set 0)
- [x] /update endpoint (updates the counter x)
- [ ] SSE Stream for the counters? Low priority.
- [ ] Tests
- [ ] Rate limiting (max 30 requests per second per IP address)
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ func main() {

authorized.POST("/delete/:namespace/*key", DeleteView)

authorized.POST("/update/:namespace/*key", UpdateView)
authorized.POST("/set/:namespace/*key", SetView)
authorized.POST("/reset/:namespace/*key", ResetView)
authorized.POST("/update/:namespace/*key", UpdateByView)

// Run the server
_ = r.Run("0.0.0.0:" + os.Getenv("PORT"))
Expand Down
45 changes: 43 additions & 2 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ func DeleteView(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "ok", "message": "Deleted key: " + createKey})
}

func UpdateView(c *gin.Context) {
fmt.Println("update view", c.Params)
func SetView(c *gin.Context) {
updatedValueRaw, _ := c.GetQuery("value")
if updatedValueRaw == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "value is required, please provide a number in the fmt of ?value=NEW_VALUE"})
Expand Down Expand Up @@ -187,3 +186,45 @@ func ResetView(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"value": 0})
}
}

func UpdateByView(c *gin.Context) {
updatedValueRaw, _ := c.GetQuery("value")
if updatedValueRaw == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "value is required, please provide a number in the fmt of ?value=NEW_VALUE"})
return

}
incrByValue, err := strconv.Atoi(updatedValueRaw)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "value must be a number, this means no floats."})
return
}
if incrByValue == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "changing value by 0 does nothing, please provide a non-zero value in the fmt of ?value=NEW_VALUE"})
return
}
namespace, key := utils.GetNamespaceKey(c)
if namespace == "" || key == "" {
return
}
dbKey := utils.CreateKey(c, namespace, key, false)
if dbKey == "" { // error is handled in CreateKey
return
}

exists := Client.Exists(context.Background(), dbKey).Val() == 0
if exists {
c.JSON(http.StatusConflict, gin.H{"error": "Key does not exist, please first create it using /create."})
return
}

// Get data from Redis
val, err := Client.IncrByFloat(context.Background(), dbKey, float64(incrByValue)).Result()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to set data. Try again later."})
return
}

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

}
27 changes: 22 additions & 5 deletions tests/Tests.http
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
@base=http://localhost:8080
@token="REPLACE_ME"
@key=jvc
@admin_token=f559c7d6-2f5a-4220-806a-83e23c4e91c3
@namespace=default
@key=qifYRkw0B47FFhNN


POST {{base}}/delete/{{key}}?token=e7fad266-b34f-4fc3-8788-a6f78f71af1c
POST {{base}}/create/{{namespace}}/{{key}}

#GET http://localhost:8080/info/jasoncameron.dev/portfolio

>{%
client.global.set("admin_token", response.body.admin_key);
%}

###
GET {{base}}/info/{{key}}

###
GET {{base}}/hit/{{namespace}}/{{key}}


#{{base}}/info/jasoncameron.dev/portfolio
Expand All @@ -20,4 +30,11 @@ Authorization: Bearer {{token}}

//
###
POST http://localhost:8080/reset/SAOH9hhi220cn4VJ/qifYRkw0B47FFhNN?token=d7c38748-7f94-4ae1-adc3-fa3bbc22b6e0
POST {{base}}/reset/{{namespace}}/{{key}}?token={{admin_token}}

###
POST {{base}}/update/{{namespace}}/{{key}}?token={{admin_token}}&value=40589

###
// use at the end to clear up testcases.
POST {{base}}/delete/{{key}}?token={{admin_token}}

0 comments on commit 50dab2c

Please sign in to comment.