forked from microsoft/cppgraphqlgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cpp
251 lines (206 loc) · 6.63 KB
/
server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "StarWarsData.h"
#include "graphqlservice/JSONResponse.h"
#ifdef _MSC_VER
#include <SDKDDKVer.h>
#endif // _MSC_VER
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/awaitable.hpp>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/use_awaitable.hpp>
#include <boost/config.hpp>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <thread>
#include <vector>
using namespace std::literals;
using namespace graphql;
namespace beast = boost::beast;
namespace http = beast::http;
namespace net = boost::asio;
using tcp = boost::asio::ip::tcp;
using tcp_stream = typename beast::tcp_stream::rebind_executor<
net::use_awaitable_t<>::executor_with_default<net::any_io_executor>>::other;
constexpr net::string_view c_host { "127.0.0.1" };
constexpr unsigned short c_port = 8080;
constexpr beast::string_view c_target { "/graphql" };
// Based on:
// https://www.boost.org/doc/libs/1_82_0/libs/beast/example/http/server/awaitable/http_server_awaitable.cpp
int main()
{
auto service = star_wars::GetService();
std::cout << "Created the service..." << std::endl;
const auto address = net::ip::make_address(c_host);
// The io_context is required for all I/O.
net::io_context ioc;
// Spawn a listening port.
net::co_spawn(
ioc,
[](tcp::endpoint endpoint, std::shared_ptr<service::Request> service)
-> net::awaitable<void> {
// Open the acceptor.
auto acceptor =
net::use_awaitable.as_default_on(tcp::acceptor(co_await net::this_coro::executor));
acceptor.open(endpoint.protocol());
// Allow address reuse.
acceptor.set_option(net::socket_base::reuse_address(true));
// Bind to the server address.
acceptor.bind(endpoint);
// Start listening for connections.
acceptor.listen(net::socket_base::max_listen_connections);
while (true)
{
net::co_spawn(
acceptor.get_executor(),
[](tcp_stream stream, std::shared_ptr<service::Request> service)
-> net::awaitable<void> {
// This buffer is required to persist across reads
beast::flat_buffer buffer;
bool keepAlive = false;
// This lambda is used to send messages
try
{
do
{
// Set the timeout.
stream.expires_after(std::chrono::seconds(30));
// Read a request
http::request<http::string_body> req;
co_await http::async_read(stream, buffer, req);
// Handle the request
http::response<http::string_body> msg;
try
{
if (req.method() != http::verb::post
|| req.target() != c_target)
{
throw std::runtime_error(
"Only POST requests to /graphql are supported.");
}
else
{
std::ostringstream oss;
oss << req.body();
auto payload = response::parseJSON(oss.str());
if (payload.type() != response::Type::Map)
{
throw std::runtime_error("Invalid request!");
}
const auto queryItr = payload.find("query"sv);
if (queryItr == payload.end()
|| queryItr->second.type() != response::Type::String)
{
throw std::runtime_error("Invalid request!");
}
peg::ast query;
query = peg::parseString(
queryItr->second.get<response::StringType>());
if (!query.root)
{
throw std::runtime_error("Unknown error!");
}
const auto operationNameItr =
payload.find("operationName"sv);
const auto operationName =
(operationNameItr != payload.end()
&& operationNameItr->second.type()
== response::Type::String)
? std::string_view { operationNameItr->second
.get<response::StringType>() }
: std::string_view {};
const auto variablesItr = payload.find("variables"sv);
auto variables = (variablesItr != payload.end()
&& variablesItr->second.type()
== response::Type::String)
? response::parseJSON(operationNameItr->second
.get<response::StringType>())
: response::Value {};
msg = http::response<http::string_body> { http::status::ok,
req.version() };
msg.set(http::field::server, BOOST_BEAST_VERSION_STRING);
msg.set(http::field::content_type, "application/json");
msg.keep_alive(req.keep_alive());
msg.body() = response::toJSON(
service
->resolve(
{ query, operationName, std::move(variables) })
.get());
msg.prepare_payload();
}
}
catch (const std::runtime_error& ex)
{
msg = http::response<http::string_body> {
http::status::bad_request,
req.version()
};
msg.set(http::field::server, BOOST_BEAST_VERSION_STRING);
msg.set(http::field::content_type, "text/plain");
msg.keep_alive(req.keep_alive());
msg.body() = "Error: "s + ex.what();
msg.prepare_payload();
}
// Determine if we should close the connection
keepAlive = msg.keep_alive();
// Send the response
co_await beast::async_write(stream,
http::message_generator { std::move(msg) },
net::use_awaitable);
} while (keepAlive);
}
catch (boost::system::system_error& se)
{
if (se.code() != http::error::end_of_stream)
throw;
}
// Send a TCP shutdown
beast::error_code ec;
stream.socket().shutdown(tcp::socket::shutdown_send, ec);
// At this point the connection is closed gracefully
// we ignore the error because the client might have
// dropped the connection already.
}(tcp_stream(co_await acceptor.async_accept()), service),
[](std::exception_ptr exp) {
if (exp)
{
try
{
std::rethrow_exception(exp);
}
catch (const std::exception& ex)
{
std::cerr << "Session error: " << ex.what() << std::endl;
}
}
});
}
}(tcp::endpoint(address, c_port), std::move(service)),
[](std::exception_ptr exp) {
if (exp)
{
std::rethrow_exception(exp);
}
});
try
{
ioc.run();
}
catch (const std::runtime_error& ex)
{
std::cerr << ex.what() << std::endl;
return 1;
}
return 0;
}