Code snippets
Hello World
using Callback = std::function<void (const HttpResponsePtr &)> ;
-
-app().registerHandler("/", [](const HttpRequestPtr& req, Callback &&callback)
-{
+
+ app().registerHandler("/", [](const HttpRequestPtr& req, Callback &&callback)
+ {
auto resp = HttpResponse::newHttpResponse();
resp->setBody("Hello World");
callback(resp);
-});
+ });
File upload
app().registerHandler("/upload", [](const HttpRequestPtr& req, Callback &&callback))
-{
+ {
MultiPartParser fileUpload;
if (fileUpload.parse(req) != 0 || fileUpload.getFiles().size() == 0) {
// The framework handles an exception by logging it, and
// by responding to the client with an HTTP 500 status code.
throw std::runtime_error("Something went wrong");
}
-
+
auto &file = fileUpload.getFiles()[0];
file.save();
callback(HttpResponse::newHttpResponse());
-});
+ });
DB with coroutines
app().registerHandler("/get_num_users", [](HttpRequestPtr req) -> Task<HttpResponsePtr>
-{
+ {
auto client = app().getDbClient();
auto result = co_await client->execSqlCoro("SELECT COUNT(*) FROM users;");
-
+
auto resp = HttpResponse::newHttpResponse();
resp->setBody(std::to_string(result[0][0].as<size_t>()));
co_return resp;
-});
+ });
Join Us
@@ -164,12 +164,12 @@ app().registerHandler("/", [](const HttpRequestPtr& req, Callback &&callback)We also want to thank everyone whom contributed to the project: