-
Notifications
You must be signed in to change notification settings - Fork 509
add http 1 /json endpoint
#136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package server | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "expvar" | ||
| "fmt" | ||
| "io" | ||
|
|
@@ -17,6 +18,7 @@ import ( | |
| "net" | ||
|
|
||
| "github.com/coocood/freecache" | ||
| pb "github.com/envoyproxy/go-control-plane/envoy/service/ratelimit/v2" | ||
| "github.com/envoyproxy/ratelimit/src/settings" | ||
| "github.com/gorilla/mux" | ||
| reuseport "github.com/kavu/go_reuseport" | ||
|
|
@@ -52,6 +54,37 @@ func (server *server) AddDebugHttpEndpoint(path string, help string, handler htt | |
| server.debugListener.endpoints[path] = help | ||
| } | ||
|
|
||
| // add an http/1 handler at the /json endpoint which allows this ratelimit service to work with | ||
| // clients that cannot use the gRPC interface (e.g. lua) | ||
| // example usage from cURL with domain "dummy" and descriptor "perday": | ||
| // echo '{"domain": "dummy", "descriptors": [{"entries": [{"key": "perday"}]}]}' | curl -vvvXPOST --data @/dev/stdin localhost:8080/json | ||
| func (server *server) AddJsonHandler(svc pb.RateLimitServiceServer) { | ||
| handler := func(writer http.ResponseWriter, request *http.Request) { | ||
| var req pb.RateLimitRequest | ||
|
|
||
| if err := json.NewDecoder(request.Body).Decode(&req); err != nil { | ||
| logger.Warnf("error: %s", err.Error()) | ||
| http.Error(writer, err.Error(), http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| resp, err := svc.ShouldRateLimit(nil, &req) | ||
| if err != nil { | ||
| logger.Warnf("error: %s", err.Error()) | ||
| http.Error(writer, err.Error(), http.StatusBadRequest) | ||
| return | ||
| } | ||
| logger.Debugf("resp:%s", resp) | ||
| if resp.OverallCode == pb.RateLimitResponse_OVER_LIMIT { | ||
| http.Error(writer, "over limit", http.StatusTooManyRequests) | ||
| } else if resp.OverallCode == pb.RateLimitResponse_UNKNOWN { | ||
| http.Error(writer, "unknown", http.StatusInternalServerError) | ||
| } | ||
|
Comment on lines
+80
to
+82
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When does this happen? Can this happen?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this check for completeness with the protobuf definition. I have never personally seen it. However if it did occur, we would not want to return a 200 OK to the user (which is what would happen absent this check). |
||
|
|
||
| } | ||
| server.router.HandleFunc("/json", handler) | ||
| } | ||
|
|
||
| func (server *server) GrpcServer() *grpc.Server { | ||
| return server.grpcServer | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a test case for this path.