Skip to content

Commit

Permalink
remove limit of some base routes & use data for response & add a new …
Browse files Browse the repository at this point in the history
…TODO
  • Loading branch information
ashk123 committed Feb 7, 2024
1 parent cc64adc commit 93b469b
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 15 deletions.
Binary file added Storage/SunSend.db
Binary file not shown.
2 changes: 2 additions & 0 deletions internals/Data/Response.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func GetErrorByResult(res_code int) (string, int, string) { // TODO: make a stru
return "There is a problem in the system", http.StatusServiceUnavailable, "FAILD" // response Status Error - Server has problem (bug)
case 17:
return "Invalid API KEY", http.StatusBadRequest, "FAILD" // response Status Error - invalid api key
case 18:
return "You Reached the Limit request time", http.StatusForbidden, "FAILD" // respponse Status error - Reached request limit
default:
return "There is a problem", http.StatusNotAcceptable, "FAILD" // Response Status Error - uknown Eror
}
Expand Down
18 changes: 5 additions & 13 deletions internals/Handlers/RareLimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@ package Handlers

import (
"encoding/json"
"sunsend/internals/Data"

"github.com/didip/tollbooth/v7"
"github.com/didip/tollbooth/v7/limiter"
)

type LimitErrorSample struct {
Err string
Body string
Code int
}

// var (
// ipRateLimiter *limiter.Limiter
// store limiter.Store
Expand All @@ -22,14 +17,11 @@ func GetLimiterMiddleWare() *limiter.Limiter {
limiter := tollbooth.NewLimiter(0.4, nil) // 1 request in 3 seconds
// limiter.SetBurst(2) // e.g burst requests in max seconds
limiter.SetMessageContentType("application/json")
message := &LimitErrorSample{
Err: "FIALD",
Body: "you reached the 1 sec request",
Code: 400,
}
jsonMessage, _ := json.Marshal(message)
response, _ := Data.NewResponse(nil, 18, "", nil)
jsonMessage, _ := json.Marshal(response)
limiter.SetMessage(string(jsonMessage))
limiter.SetStatusCode(message.Code)
_, code, _ := Data.GetErrorByResult(18)
limiter.SetStatusCode(code)
return limiter
}

Expand Down
4 changes: 2 additions & 2 deletions internals/Handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (

func Handler(e *echo.Echo) {
limiter := GetLimiterMiddleWare() // set the limiter middleware
e.GET(Routes.GetRootRoute().Path, Routes.GetRootRoute().Runner, tollbooth_echo.LimitHandler(limiter)) // set the Root Router ('/')
e.GET(Routes.GetCssResources().Path, Routes.GetCssResources().Runner, tollbooth_echo.LimitHandler(limiter)) // set the Resources Router ("/css/:file")
e.GET(Routes.GetRootRoute().Path, Routes.GetRootRoute().Runner) // set the Root Router ('/')
e.GET(Routes.GetCssResources().Path, Routes.GetCssResources().Runner) // set the Resources Router ("/css/:file")
e.GET(Routes.GetChatRoute().Path, Routes.GetChatRoute().Runner, tollbooth_echo.LimitHandler(limiter)) // set the chat Router ("/chat/:id")
e.POST(Routes.GetChatPostRoute().Path, Routes.GetChatPostRoute().Runner, tollbooth_echo.LimitHandler(limiter)) // set the chat Router ("/chat/:id")
e.GET(Routes.GetChannelRoute().Path, Routes.GetChannelRoute().Runner, tollbooth_echo.LimitHandler(limiter)) // set the channel Router ("/channel/:id")
Expand Down

0 comments on commit 93b469b

Please sign in to comment.