Skip to content

Commit 710dccb

Browse files
authored
Merge pull request #321 from Accenture/221-response-codes
221 response codes
2 parents 3807bf8 + 8a8b0c4 commit 710dccb

File tree

26 files changed

+1776
-184
lines changed

26 files changed

+1776
-184
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Support publishing events consumed from [NATS](https://nats.io) topics. See the [documentation](https://accenture.github.io/reactive-interaction-gateway/docs/event-streams.html#nats) for how to get started. [#297](https://github.com/Accenture/reactive-interaction-gateway/issues/297)
1313
- Added validation for reverse proxy configuration. Now it crashes RIG on start when configuration is not valid or returns `400` when using REST API to update configuration. [#277](https://github.com/Accenture/reactive-interaction-gateway/issues/277)
1414
- Added basic distributed tracing support in [W3C Trace Context specification](https://www.w3.org/TR/trace-context/) with Jaeger and Openzipkin exporters. RIG opens a span at the API Gateway and emits trace context in Cloud Events following the [distributed tracing spec](https://github.com/cloudevents/spec/blob/v1.0/extensions/distributed-tracing.md). [#281](https://github.com/Accenture/reactive-interaction-gateway/issues/281)
15+
- Added possibility to set response code for `response_from` messages in reverse proxy (`kafka` and `http_async`). [#321](https://github.com/Accenture/reactive-interaction-gateway/pull/321)
16+
- Added new version - `v3` - for internal endpoints to support response code in the `/responses` endpoint
1517

1618
### Changed
1719

1820
- Incorporated [cloudevents-ex](https://github.com/kevinbader/cloudevents-ex) to handle binary and structured modes for [Kafka protocol binding](https://github.com/cloudevents/spec/blob/v1.0/kafka-protocol-binding.md) in a proper way. This introduces some **breaking changes**:
1921
- Binary mode is now using `ce_` prefix for CloudEvents context attribute headers, before it was `ce-` - done according to the [Kafka protocol binding](https://github.com/cloudevents/spec/blob/v1.0/kafka-protocol-binding.md)
2022
- Change above affects also `"response_from": "kafka"` proxy functionality. RIG will forward to clients only Kafka body, no headers. This means, when using binary mode, clients receive only the data part, no CloudEvents context attributes.
23+
- Changed `response_from` handler to expect a message in binary format, **NOT** a cloud event (`kafka` and `http_async`). [#321](https://github.com/Accenture/reactive-interaction-gateway/pull/321)
2124

2225
### Fixed
2326

config/rig_api/config.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ config :phoenix, :serve_endpoints, true
4545

4646
config :rig, RigApi.V1.APIs, rig_proxy: RigInboundGateway.Proxy
4747
config :rig, RigApi.V2.APIs, rig_proxy: RigInboundGateway.Proxy
48+
config :rig, RigApi.V3.APIs, rig_proxy: RigInboundGateway.Proxy
4849

4950
config :rig, :event_filter, Rig.EventFilter
5051

config/rig_api/test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ config :rig, RigApi.Endpoint,
1010

1111
config :rig, RigApi.V1.APIs, rig_proxy: RigInboundGateway.ProxyMock
1212
config :rig, RigApi.V2.APIs, rig_proxy: RigInboundGateway.ProxyMock
13+
config :rig, RigApi.V3.APIs, rig_proxy: RigInboundGateway.ProxyMock
1314

1415
config :rig, :event_filter, Rig.EventFilterMock

docs/api-gateway.md

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,7 @@ The endpoint expects the following request format:
180180
### Wait for response
181181
182182
Sometimes it makes sense to provide a simple request-response API to something that runs asynchronously on the backend. For example, let's say there's a ticket reservation process that takes 10 seconds in total and involves three different services that communicate via message passing. For an external client, it may be simpler to wait 10 seconds for the response instead of polling for a response every other second.
183-
A behavior like this can be configured using an endpoints' `response_from` property. When set to `kafka`, the response to the request is not taken from the `target` (e.g., for `target` = `http` this means the backend's HTTP response is ignored), but instead it's read from a Kafka topic. In order to enable RIG to correlate the response from the topic with the original request, RIG adds a correlation ID to the request (using a query parameter in case of `target` = `http`, or backed into the produced CloudEvent otherwise). Backend services that work with the request need to include that correlation ID in their response; otherwise, RIG won't be able to forward it to the client (and times out).
184-
185-
> In case you want to use _binary_ transport mode, make sure that `rig` extension (containing correlation ID) is prefixed with `ce_` as well.
183+
A behavior like this can be configured using an endpoints' `response_from` property. When set to `kafka`, the response to the request is not taken from the `target` (e.g., for `target` = `http` this means the backend's HTTP response is ignored), but instead it's read from a Kafka topic. In order to enable RIG to correlate the response from the topic with the original request, RIG adds a correlation ID to the request (using a query parameter in case of `target` = `http`, or backed into the produced CloudEvent otherwise). **Backend services that work with the request need to include that correlation ID in their response; otherwise, RIG won't be able to forward it to the client (and times out).**
186184
187185
Configuration of such API endpoint might look like this:
188186
@@ -207,26 +205,37 @@ Configuration of such API endpoint might look like this:
207205
208206
> Note the presence of `response_from` field. This tells RIG to wait for different event with the same correlation ID.
209207
210-
As an alternative you can set `response_from` to `http_async`. This means that correlated response has to be sent to internal `:4010/v2/responses` `POST` endpoint with a body like this:
208+
#### Supported combinations (`target` -> `response_from`)
209+
210+
- HTTP -> `kafka`/`http_async`/`kinesis`
211+
- Kafka -> `kafka`
212+
- Kinesis -> **not supported**
213+
- Nats -> `nats`
214+
215+
`http_async` means that correlated response has to be sent to internal `:4010/v3/responses` `POST` endpoint.
216+
217+
#### Supported formats
218+
219+
All `response_from` options support only binary mode.
220+
221+
Message headers:
222+
223+
```plaintext
224+
rig-correlation: "correlation_id_sent_by_rig"
225+
rig-response-code: "201"
226+
content-type: "application/json"
227+
```
228+
229+
> All headers are required.
230+
231+
Message body:
211232
212233
```json
213234
{
214-
"id": "1",
215-
"specversion": "0.2",
216-
"source": "my-service",
217-
"type": "com.example",
218-
"rig": {
219-
"correlation": "_id_"
220-
},
221-
"data": {
222-
...
223-
}
224-
...
235+
"foo": "bar"
225236
}
226237
```
227238
228-
> **NOTE:** Kinesis doesn't support `response_from` field yet.
229-
230239
## Auth
231240
232241
RIG can do simple auth check for endpoints. Currently supports JWT.

lib/rig_api/router.ex

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,32 @@ defmodule RigApi.Router do
7575
end
7676
end
7777

78+
scope "/v3", RigApi.V3 do
79+
scope "/apis" do
80+
pipe_through(:body_parser)
81+
get("/", APIs, :list_apis)
82+
post("/", APIs, :add_api)
83+
get("/:id", APIs, :get_api_detail)
84+
put("/:id", APIs, :update_api)
85+
delete("/:id", APIs, :deactivate_api)
86+
end
87+
88+
scope "/messages" do
89+
post("/", Messages, :publish)
90+
end
91+
92+
scope "/responses" do
93+
pipe_through(:body_parser)
94+
resources("/", Responses, only: [:create])
95+
end
96+
97+
scope "/session-blacklist" do
98+
pipe_through(:body_parser)
99+
post("/", SessionBlacklist, :blacklist_session)
100+
get("/:session_id", SessionBlacklist, :check_status)
101+
end
102+
end
103+
78104
def swagger_info do
79105
%{
80106
info: %{

lib/rig_api/v1/responses.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ defmodule RigApi.V1.Responses do
3333

3434
@doc """
3535
Accepts message to be sent to correlated HTTP process.
36-
3736
Note that body has to contain following field `"rig": { "correlation": "_id_" }`.
3837
"""
3938
def create(conn, message) do

lib/rig_api/v2/responses.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ defmodule RigApi.V2.Responses do
3333

3434
@doc """
3535
Accepts message to be sent to correlated HTTP process.
36-
3736
Note that body has to contain following field `"rig": { "correlation": "_id_" }`.
3837
"""
3938
def create(conn, message) do

0 commit comments

Comments
 (0)