|
| 1 | +#include <cstdint> |
| 2 | +#include <iostream> |
| 3 | +#include <memory> |
| 4 | +#include <optional> |
| 5 | +#include <ostream> |
| 6 | +#include <sstream> |
| 7 | +#include <string> |
| 8 | +#include <string_view> |
| 9 | + |
| 10 | +#include <eglt/absl_headers.h> |
| 11 | +#include <eglt/actions/action.h> |
| 12 | +#include <eglt/data/eg_structs.h> |
| 13 | +#include <eglt/nodes/async_node.h> |
| 14 | +#include <eglt/sdk/serving/websockets.h> |
| 15 | +#include <eglt/service/service.h> |
| 16 | + |
| 17 | +ABSL_FLAG(uint16_t, port, 20000, "Port to bind to."); |
| 18 | + |
| 19 | +// Simply some type aliases to make the code more readable. |
| 20 | +using Action = eglt::Action; |
| 21 | +using ActionRegistry = eglt::ActionRegistry; |
| 22 | +using Chunk = eglt::Chunk; |
| 23 | +using Service = eglt::Service; |
| 24 | + |
| 25 | +absl::Status RunEcho(const std::shared_ptr<Action>& action) { |
| 26 | + auto input_text = action->GetNode("text"); |
| 27 | + input_text->SetReaderOptions(/*ordered=*/true, |
| 28 | + /*remove_chunks=*/true); |
| 29 | + std::optional<Chunk> chunk; |
| 30 | + while (true) { |
| 31 | + *input_text >> chunk; |
| 32 | + |
| 33 | + if (!chunk.has_value()) { |
| 34 | + break; |
| 35 | + } |
| 36 | + |
| 37 | + if (auto status = input_text->GetReaderStatus(); !status.ok()) { |
| 38 | + LOG(ERROR) << "Failed to read input: " << status; |
| 39 | + return status; |
| 40 | + } |
| 41 | + |
| 42 | + action->GetNode("response") << *chunk; |
| 43 | + } |
| 44 | + |
| 45 | + // This is necessary and indicates the end of stream. |
| 46 | + action->GetNode("response") << eglt::EndOfStream(); |
| 47 | + |
| 48 | + return absl::OkStatus(); |
| 49 | +} |
| 50 | + |
| 51 | +ActionRegistry MakeActionRegistry() { |
| 52 | + ActionRegistry registry; |
| 53 | + |
| 54 | + registry.Register(/*name=*/"echo", |
| 55 | + /*schema=*/ |
| 56 | + { |
| 57 | + .name = "echo", |
| 58 | + .inputs = {{"text", "text/plain"}}, |
| 59 | + .outputs = {{"response", "text/plain"}}, |
| 60 | + }, |
| 61 | + /*handler=*/RunEcho); |
| 62 | + return registry; |
| 63 | +} |
| 64 | + |
| 65 | +int main(int argc, char** argv) { |
| 66 | + absl::InstallFailureSignalHandler({}); |
| 67 | + absl::ParseCommandLine(argc, argv); |
| 68 | + |
| 69 | + eglt::SessionMessage message{ |
| 70 | + .node_fragments = {eglt::NodeFragment{.id = "test", |
| 71 | + .chunk = eglt::EndOfStream()}}, |
| 72 | + }; |
| 73 | + for (const auto bytes = cppack::Pack(message); const auto& byte : bytes) { |
| 74 | + std::cout << std::hex << static_cast<int>(byte) << " "; |
| 75 | + } |
| 76 | + std::cout << std::endl; |
| 77 | + |
| 78 | + const uint16_t port = absl::GetFlag(FLAGS_port); |
| 79 | + ActionRegistry action_registry = MakeActionRegistry(); |
| 80 | + eglt::Service service(&action_registry); |
| 81 | + eglt::sdk::WebsocketEvergreenServer server(&service, "0.0.0.0", port); |
| 82 | + server.Run(); |
| 83 | + server.Join().IgnoreError(); |
| 84 | + |
| 85 | + return 0; |
| 86 | +} |
0 commit comments