poa_backend v0.1.0 POABackend.CustomHandler.REST View Source
This module implements the REST Custom Handler over HTTP/1.1.
Plugin Architecture
This plugin involves many processes. When the POABackend.CustomHandler.Supervisor
calls the
child_spec/2
function it will create its own supervision tree under that supervisor
POABackend.CustomHandler.REST.Supervisor
is the main supervisor. It is in charge of supervise its three children.- The
Ranch/Cowboy
branch is managed by Ranch and Cowboy apps. They are in charge of expose the REST endpoints on top of http. - The Registry is an Elixir Registry in charge of track/untrack Activity Monitor Servers, created by the next child
POABackend.CustomHandler.REST.Monitor.Supervisor
is a Supervisor with:simple_one_for_one
strategy. It will start dynamicallyGenServer
’s implemented byPOABackend.CustomHandler.REST.Monitor.Server
module.
REST endpoints
This Pluting also defines the endpoints needed to use the POA Protocol.
All the endpoints accept pure JSON or MessagePack formats
session endpoint
In order to get valid access to the rest of the endpoint you will need a JWT token. In order to get it check here
ping endpoint
POST /ping
Headers:
HTTP header | Values |
---|---|
content-type | application/json or application/msgpack |
authorization | Bearer jwtToken |
payload:
JSON:
{
id: String() # agent id
}
MessagePack:
Same format as JSON but packed thru MessagePack library
responses:
Code | Description |
---|---|
200 | Success {“result”:”success”} |
401 | Unauthorized |
415 | Unsupported Media Type (only application/json and application/msgpack allowed) |
422 | Unprocessable Entity. Required fields missing |
Example:
curl -v -d '{"id":"agentID"}' -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJwb2FfYmFja2VuZCIsImV4cCI6MTUzMzkyNDc4NCwiaWF0IjoxNTMzOTIxMTg0LCJpc3MiOiJwb2FfYmFja2VuZCIsImp0aSI6IjFlM2U0MTU0LWJiNDMtNDY0MS04ZGFmLTIxNjRlOTFmMTU4YiIsIm5iZiI6MTUzMzkyMTE4Mywic3ViIjoiUmp1YURzdi0iLCJ0eXAiOiJhY2Nlc3MifQ.SqE48FXKhN7Y7aAsipFwvIAqetyhVJGwARMEwNwCXSK-QnXZdu3f2xkHDrXBsf1L1IlhTwg78Y-RiKrgepV34Q" -X POST http://localhost:4002/ping
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying ::1...
* TCP_NODELAY set
* Connection failed
* connect to ::1 port 4002 failed: Connection refused
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 4002 (#0)
> POST /ping HTTP/1.1
> Host: localhost:4002
> User-Agent: curl/7.53.1
> Accept: */*
> Content-Type: application/json
> Authorization: Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJwb2FfYmFja2VuZCIsImV4cCI6MTUzMzkyNDc4NCwiaWF0IjoxNTMzOTIxMTg0LCJpc3MiOiJwb2FfYmFja2VuZCIsImp0aSI6IjFlM2U0MTU0LWJiNDMtNDY0MS04ZGFmLTIxNjRlOTFmMTU4YiIsIm5iZiI6MTUzMzkyMTE4Mywic3ViIjoiUmp1YURzdi0iLCJ0eXAiOiJhY2Nlc3MifQ.SqE48FXKhN7Y7aAsipFwvIAqetyhVJGwARMEwNwCXSK-QnXZdu3f2xkHDrXBsf1L1IlhTwg78Y-RiKrgepV34Q
> Content-Length: 16
>
* upload completely sent off: 16 out of 16 bytes
< HTTP/1.1 200 OK
< server: Cowboy
< date: Fri, 10 Aug 2018 17:14:33 GMT
< content-length: 20
< cache-control: max-age=0, private, must-revalidate
< content-type: application/json; charset=utf-8
<
* Connection #0 to host localhost left intact
{"result":"success"}
data endpoint
POST /data
Headers:
HTTP header | Values |
---|---|
content-type | application/json or application/msgpack |
authorization | Bearer jwtToken |
payload:
JSON:
{
id: String() # agent id
type: String() # data type (for now only ethereum_metrics)
data: Object() # metric data itself
}
MessagePack:
Same format as JSON but packed thru MessagePack library
responses:
Code | Description |
---|---|
200 | Success {“result”:”success”} |
401 | Unauthorized |
415 | Unsupported Media Type (only application/json and application/msgpack allowed) |
422 | Unprocessable Entity. Required fields missing |
Example:
curl -v -d '{"id":"agentID", "type":"ethereum_metrics", "data":{"hello":"world"}}' -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJwb2FfYmFja2VuZCIsImV4cCI6MTUzMzkyNDc4NCwiaWF0IjoxNTMzOTIxMTg0LCJpc3MiOiJwb2FfYmFja2VuZCIsImp0aSI6IjFlM2U0MTU0LWJiNDMtNDY0MS04ZGFmLTIxNjRlOTFmMTU4YiIsIm5iZiI6MTUzMzkyMTE4Mywic3ViIjoiUmp1YURzdi0iLCJ0eXAiOiJhY2Nlc3MifQ.SqE48FXKhN7Y7aAsipFwvIAqetyhVJGwARMEwNwCXSK-QnXZdu3f2xkHDrXBsf1L1IlhTwg78Y-RiKrgepV34Q" -X POST http://localhost:4002/data
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying ::1...
* TCP_NODELAY set
* Connection failed
* connect to ::1 port 4002 failed: Connection refused
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 4002 (#0)
> POST /data HTTP/1.1
> Host: localhost:4002
> User-Agent: curl/7.53.1
> Accept: */*
> Content-Type: application/json
> Authorization: Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJwb2FfYmFja2VuZCIsImV4cCI6MTUzMzkyNDc4NCwiaWF0IjoxNTMzOTIxMTg0LCJpc3MiOiJwb2FfYmFja2VuZCIsImp0aSI6IjFlM2U0MTU0LWJiNDMtNDY0MS04ZGFmLTIxNjRlOTFmMTU4YiIsIm5iZiI6MTUzMzkyMTE4Mywic3ViIjoiUmp1YURzdi0iLCJ0eXAiOiJhY2Nlc3MifQ.SqE48FXKhN7Y7aAsipFwvIAqetyhVJGwARMEwNwCXSK-QnXZdu3f2xkHDrXBsf1L1IlhTwg78Y-RiKrgepV34Q
> Content-Length: 69
>
* upload completely sent off: 69 out of 69 bytes
< HTTP/1.1 200 OK
< server: Cowboy
< date: Fri, 10 Aug 2018 17:17:34 GMT
< content-length: 20
< cache-control: max-age=0, private, must-revalidate
< content-type: application/json; charset=utf-8
<
* Connection #0 to host localhost left intact
{"result":"success"}
bye endpoint
POST /bye
Headers:
HTTP header | Values |
---|---|
content-type | application/json or application/msgpack |
authorization | Bearer jwtToken |
payload:
JSON:
{
id: String() # agent id
}
MessagePack:
Same format as JSON but packed thru MessagePack library
responses:
Code | Description |
---|---|
200 | Success {“result”:”success”} |
401 | Unauthorized |
415 | Unsupported Media Type (only application/json and application/msgpack allowed) |
422 | Unprocessable Entity. Required fields missing |
Example:
curl -v -d '{"id":"agentID"}' -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJwb2FfYmFja2VuZCIsImV4cCI6MTUzMzkyNDc4NCwiaWF0IjoxNTMzOTIxMTg0LCJpc3MiOiJwb2FfYmFja2VuZCIsImp0aSI6IjFlM2U0MTU0LWJiNDMtNDY0MS04ZGFmLTIxNjRlOTFmMTU4YiIsIm5iZiI6MTUzMzkyMTE4Mywic3ViIjoiUmp1YURzdi0iLCJ0eXAiOiJhY2Nlc3MifQ.SqE48FXKhN7Y7aAsipFwvIAqetyhVJGwARMEwNwCXSK-QnXZdu3f2xkHDrXBsf1L1IlhTwg78Y-RiKrgepV34Q" -X POST http://localhost:4002/bye
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying ::1...
* TCP_NODELAY set
* Connection failed
* connect to ::1 port 4002 failed: Connection refused
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 4002 (#0)
> POST /bye HTTP/1.1
> Host: localhost:4002
> User-Agent: curl/7.53.1
> Accept: */*
> Content-Type: application/json
> Authorization: Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJwb2FfYmFja2VuZCIsImV4cCI6MTUzMzkyNDc4NCwiaWF0IjoxNTMzOTIxMTg0LCJpc3MiOiJwb2FfYmFja2VuZCIsImp0aSI6IjFlM2U0MTU0LWJiNDMtNDY0MS04ZGFmLTIxNjRlOTFmMTU4YiIsIm5iZiI6MTUzMzkyMTE4Mywic3ViIjoiUmp1YURzdi0iLCJ0eXAiOiJhY2Nlc3MifQ.SqE48FXKhN7Y7aAsipFwvIAqetyhVJGwARMEwNwCXSK-QnXZdu3f2xkHDrXBsf1L1IlhTwg78Y-RiKrgepV34Q
> Content-Length: 16
>
* upload completely sent off: 16 out of 16 bytes
< HTTP/1.1 200 OK
< server: Cowboy
< date: Fri, 10 Aug 2018 17:19:24 GMT
< content-length: 20
< cache-control: max-age=0, private, must-revalidate
< content-type: application/json; charset=utf-8
<
* Connection #0 to host localhost left intact
{"result":"success"}
Link to this section Summary
Functions
This function will initialize an Activity Monitor Server for a given Agent ID if it doesn’t exist already. If it exist this function will send a ping message to the Monitor Server in order to restart the timeout countdown
Link to this section Functions
This function will initialize an Activity Monitor Server for a given Agent ID if it doesn’t exist already. If it exist this function will send a ping message to the Monitor Server in order to restart the timeout countdown.
The Activity Monitor Server is a GenServer
which will be initialized under the
POABackend.CustomHandler.REST.Monitor.Supervisor
supervisor.