|
| 1 | +--- |
| 2 | +sidebar_label: Gateway |
| 3 | +hide_table_of_contents: true |
| 4 | +--- |
| 5 | + |
| 6 | +# Gateway |
| 7 | + |
| 8 | +The `gateway` node-type enable VILLASnode to transform api type. |
| 9 | +Currently, the transformation of [http](https://en.wikipedia.org/wiki/HTTP) and [gRPC](https://grpc.io/) is supported. |
| 10 | + |
| 11 | + |
| 12 | +### Limitations |
| 13 | + |
| 14 | +- For protobuf payload that is not [VILLASnode format](../formats/protobuf.md), only simple datatypes are supported. |
| 15 | +- It only supported unary RPC. |
| 16 | + |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +This node-type requires [gRPC](https://grpc.io/) and [reflection.proto](https://github.com/grpc/grpc/blob/master/src/proto/grpc/reflection/v1alpha/reflection.proto) for gRPC server [reflection](https://grpc.io/docs/guides/reflection/). |
| 21 | +The script `packaging/deps.sh` can be used to automatically download reflection.proto and generate protobuf code. |
| 22 | +Please refer to the [installation document](../installation.md). |
| 23 | + |
| 24 | +## Implementation |
| 25 | + |
| 26 | +The source code of the node-type is available here: |
| 27 | +https://github.com/VILLASframework/node/blob/master/lib/nodes/gateway.cpp |
| 28 | + |
| 29 | +## Configuration {#config} |
| 30 | + |
| 31 | +import ApiSchema from '@theme/ApiSchema'; |
| 32 | + |
| 33 | +<ApiSchema id="node" example pointer="#/components/schemas/api" /> |
| 34 | + |
| 35 | +## Example |
| 36 | + |
| 37 | +``` url="external/node/etc/examples/nodes/gateway.conf" title="node/etc/examples/nodes/api.conf" |
| 38 | +http = { |
| 39 | + port = 8080 |
| 40 | +} |
| 41 | +
|
| 42 | +nodes = { |
| 43 | + gateway_node = { |
| 44 | + type = "gateway" |
| 45 | +
|
| 46 | + format = "protobuf" |
| 47 | + # API type |
| 48 | + gateway_type = "gRPC" |
| 49 | + # Address of remote server |
| 50 | + address = "localhost:50051" |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +## Usage |
| 56 | + |
| 57 | +The usage of this node is similar to [`api`](api.md) node-type. |
| 58 | +The following [`curl`](https://curl.se/) commands are example for calling gRPC method with HTTP api. |
| 59 | +The input of gRPC method can be either by the body of http request or input data from other node(via a [path](../config/paths.md)). |
| 60 | +If the http request body is empty, the input from other node will be used. |
| 61 | + |
| 62 | +Note on http methods |
| 63 | + - GET should be used only when the input of gRPC method can be empty or ignored. |
| 64 | + - PUT should be used when the output of gRPC method is in VILLASnode format and the user want to put the data to the path. |
| 65 | + - POST should be used when the output of gRPC method is not in VILLASnode format or the user not want output data to the path. |
| 66 | + |
| 67 | + |
| 68 | +#### The url for sending a request to this node-type are as following |
| 69 | +``` |
| 70 | +http://<address>:<port>/api/v2/gateway/<node name or UUID>/<gRPC package>/<gRPC service>/<gRPC method> |
| 71 | +``` |
| 72 | + |
| 73 | +The following `.proto` file will be used as an example |
| 74 | +``` |
| 75 | +syntax = "proto3"; |
| 76 | +
|
| 77 | +import "villas.proto"; |
| 78 | +
|
| 79 | +package ex_server; |
| 80 | +
|
| 81 | +service ex_service { |
| 82 | + rpc GetData (villas.node.Message) returns (villas.node.Message) {}; |
| 83 | + rpc SetData (villas.node.Message) returns (msg) {}; |
| 84 | + rpc GetDataRef (Reference) returns (villas.node.Message) {}; |
| 85 | +} |
| 86 | +
|
| 87 | +message Reference { |
| 88 | + repeated int32 ref = 1; |
| 89 | +} |
| 90 | +
|
| 91 | +message msg { |
| 92 | + string status = 1; |
| 93 | +} |
| 94 | +
|
| 95 | +``` |
| 96 | + |
| 97 | +#### Call GetData method on gRPC server |
| 98 | + |
| 99 | +```shell |
| 100 | +curl http://localhost:8080/api/v2/gateway/gateway_node/ex_server/ex_service/GetData -XPUT |
| 101 | +``` |
| 102 | +Since the PUT method is used, the output from gRPC method will be put to the path. |
| 103 | + |
| 104 | +#### Call SetData method on gRPC server |
| 105 | + |
| 106 | +```shell |
| 107 | +curl http://localhost:8080/api/v2/gateway/gateway_node/ex_server/ex_service/SetData -XPOST |
| 108 | +``` |
| 109 | +Since the body is empty the data from the path will be send to gRPC server. |
| 110 | + |
| 111 | +#### Call GetDataRef method on gRPC server |
| 112 | + |
| 113 | +```shell |
| 114 | +curl http://localhost:8080/api/v2/gateway/gateway_node/ex_server/ex_service/GetDataRef -d '{"ref":[0,1,2,3,4]}' |
| 115 | +``` |
| 116 | +Since the body is not empty, it will be form into protobuf payload based on the example `.proto` file and send to the gRPC server. |
0 commit comments