From 4fd77ac95998a9bff45e30a26f97e60159ad5e1d Mon Sep 17 00:00:00 2001 From: Niklas Halle Date: Sun, 23 Aug 2020 13:57:40 +0200 Subject: added crow and basic example --- .gitmodules | 3 +++ CMakeLists.txt | 2 +- README.md | 7 +++++-- include/crow | 1 + include/crow.h | 1 + libs/crow | 1 + src/main.cpp | 22 +++++++++++++++++++++- 7 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 .gitmodules create mode 120000 include/crow create mode 120000 include/crow.h create mode 160000 libs/crow diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..fe0f6dc --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libs/crow"] + path = libs/crow + url = git@git.niklashalle.net:crow.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c3ea9e..53d1f7e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ project(N_Core DESCRIPTION "Backend of the N bot - serving JSON requests" ) -set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) IF (CMAKE_VERSION VERSION_LESS "3.7.0") diff --git a/README.md b/README.md index c540718..3e598b9 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,12 @@ # N Core - the backend of N + +Build upon [crow](https://github.com/ipkn/crow) [forked](https://git.niklashalle.net/crow) to fix a deprecation in boost 1.7+ + ## N protocol ### request -Each message sent to the N core server must be valid JSON and have a `command` field. It contains the command for N to handle, sanitizied (i.e. no leading '/' or trailing username etc). +Each message sent to the N core server must be valid JSON and have a `command` field. It contains the command for N to handle, sanitized (i.e. no leading '/' or trailing username etc). The arguments field contains the remaining text just as was send to the bridge. -All messages may optionally include an `session` field. When you authorize against N core or switch to a menu, you will be returned (see next section) a session id which you can then send with this field to follow up on the inital command. +All messages may optionally include an `session` field. When you authorize against N core or switch to a menu, you will be returned (see next section) a session id which you can then send with this field to follow up on the initial command. | Field | Type | Required? | Description | |-------|------|-----------|-------------| diff --git a/include/crow b/include/crow new file mode 120000 index 0000000..f237a5a --- /dev/null +++ b/include/crow @@ -0,0 +1 @@ +../libs/crow/include/crow \ No newline at end of file diff --git a/include/crow.h b/include/crow.h new file mode 120000 index 0000000..359d17a --- /dev/null +++ b/include/crow.h @@ -0,0 +1 @@ +../libs/crow/include/crow.h \ No newline at end of file diff --git a/libs/crow b/libs/crow new file mode 160000 index 0000000..414f5ab --- /dev/null +++ b/libs/crow @@ -0,0 +1 @@ +Subproject commit 414f5ab444d38d4c9605d16597450282b4699896 diff --git a/src/main.cpp b/src/main.cpp index d90e58d..57e4686 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,25 @@ #include +#include "crow.h" + int main() { + crow::SimpleApp app; + + CROW_ROUTE(app, "/add_json") + .methods("POST"_method) + ([](crow::request const &request) { + auto data = crow::json::load(request.body); + if (!data) + return crow::response(400); + auto sum = data["a"].i() + data["b"].i(); + std::ostringstream os; + os << sum; + return crow::response{os.str()}; + }); + + app.port(18080).multithreaded().run(); + std::cout << "Hello, World\n" << std::endl; -} \ No newline at end of file +} + +// curl -d '{"a":"5", "b":"12"}' -H "Content-Type: application/json" -X POST http://localhost:18080/add_json -- cgit v1.2.3-54-g00ecf