From 5fb5c88c8f31c0dc8c4423d2b4355ca717f6b343 Mon Sep 17 00:00:00 2001 From: niansa Date: Wed, 16 Sep 2020 14:54:48 +0200 Subject: [PATCH] Initial commit --- .gitignore | 73 +++++++++++++++++++++++++++++++++++++ .gitmodules | 3 ++ commsy-rest.pro | 16 +++++++++ main.cpp | 96 +++++++++++++++++++++++++++++++++++++++++++++++++ qcommsy | 1 + 5 files changed, 189 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 commsy-rest.pro create mode 100644 main.cpp create mode 160000 qcommsy diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fab7372 --- /dev/null +++ b/.gitignore @@ -0,0 +1,73 @@ +# This file is used to ignore files which are generated +# ---------------------------------------------------------------------------- + +*~ +*.autosave +*.a +*.core +*.moc +*.o +*.obj +*.orig +*.rej +*.so +*.so.* +*_pch.h.cpp +*_resource.rc +*.qm +.#* +*.*# +core +!core/ +tags +.DS_Store +.directory +*.debug +Makefile* +*.prl +*.app +moc_*.cpp +ui_*.h +qrc_*.cpp +Thumbs.db +*.res +*.rc +/.qmake.cache +/.qmake.stash + +# qtcreator generated files +*.pro.user* + +# xemacs temporary files +*.flc + +# Vim temporary files +.*.swp + +# Visual Studio generated files +*.ib_pdb_index +*.idb +*.ilk +*.pdb +*.sln +*.suo +*.vcproj +*vcproj.*.*.user +*.ncb +*.sdf +*.opensdf +*.vcxproj +*vcxproj.* + +# MinGW generated files +*.Debug +*.Release + +# Python byte code +*.pyc + +# Binaries +# -------- +*.dll +*.exe + diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..e21031b --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "qcommsy"] + path = qcommsy + url = https://gitlab.com/niansa/qcommsy diff --git a/commsy-rest.pro b/commsy-rest.pro new file mode 100644 index 0000000..45d5f14 --- /dev/null +++ b/commsy-rest.pro @@ -0,0 +1,16 @@ +TEMPLATE = app +CONFIG += console c++17 +CONFIG -= app_bundle +CONFIG -= qt + +SOURCES += \ + main.cpp \ + qcommsy/curlreq.cpp \ + qcommsy/libcommsy.cpp + +INCLUDEPATH += /usr/include/jsoncpp +LIBS += -ldrogon -ltrantor -luuid -lz -lcrypto -ldl -ljson-c -lsqlite3 -lssl -ljsoncpp -pthread +LIBS += -lgumbo -lcurl -lcurlpp + +HEADERS += \ + qcommsy/libcommsy.hpp diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..fad1f6b --- /dev/null +++ b/main.cpp @@ -0,0 +1,96 @@ +#include +#include + +#include "qcommsy/libcommsy.hpp" + +using namespace drogon; + + +int main() { + // Register handlers + app().registerHandler("/", + [](const HttpRequestPtr& req, std::function &&callback) + { + auto resp = HttpResponse::newHttpResponse(); + resp->setBody("Hello world!"); + callback(resp); + }, {Get} + ); + + app().registerHandler("/fetchList?url={serverUrl}&sid={sid}&room={room}&startId={startId}&maxPosts={_maxPosts}", + [](const HttpRequestPtr& req, std::function &&callback, + const std::string &serverUrl, const std::string &sid, const std::string &room, const std::string &startId, const std::string& _maxPosts) + { + std::string maxPosts = _maxPosts; + if (maxPosts.empty()) { + maxPosts = "0"; + } + + // Load data from server + libCommsy *connection = nullptr; + std::string resStr = "unknown"; + auto resCode = HttpStatusCode::k500InternalServerError; + try { + connection = new libCommsy(serverUrl, sid, room, startId, std::stoull(maxPosts)); + resStr = "ok"; + resCode = HttpStatusCode::k200OK; + } catch (libCommsy::invalidRoomError&) { + resStr = "invalidRoom"; + resCode = HttpStatusCode::k404NotFound; + } catch (libCommsy::invalidSIDError&) { + resStr = "invalidSid"; + resCode = HttpStatusCode::k403Forbidden; + } catch (libCommsy::connectionFailError&) { + resStr = "invalidUrl"; + resCode = HttpStatusCode::k502BadGateway; + } catch (std::invalid_argument&) { + resStr = "invalidNumber"; + resCode = HttpStatusCode::k400BadRequest; + } + + // Generate JSON + Json::Value json; + json["result"] = resStr; + if (resCode == 200) { + json["numposts"] = static_cast(connection->numposts); + json["posts"] = Json::arrayValue; + for (const auto& rawPost : connection->posts) { + Json::Value jsonPost; + // Get basic details + jsonPost["name"] = rawPost.name; + jsonPost["meta"] = rawPost.meta; + jsonPost["taskState"] = rawPost.taskState; + jsonPost["url"] = rawPost.url; + jsonPost["id"] = rawPost.id; + jsonPost["files"] = Json::arrayValue; + // Get files + for (const auto& rawFile : rawPost.files) { + Json::Value jsonFile; + jsonFile["name"] = rawFile.name; + jsonFile["url"] = rawFile.url; + jsonPost["files"].append(jsonFile); + } + // Append to posts array + json["posts"].append(jsonPost); + } + } + auto resp = HttpResponse::newHttpJsonResponse(json); + resp->setStatusCode(resCode); + callback(resp); + + // Clean up + if (connection) { + delete connection; + } + }, {Get} + ); + + + // Start server + app().setLogPath("./") + .setLogLevel(trantor::Logger::kWarn) + .addListener("0.0.0.0", 8000) + .setThreadNum(16) + //.enableRunAsDaemon() + .run(); +} diff --git a/qcommsy b/qcommsy new file mode 160000 index 0000000..83b2ec1 --- /dev/null +++ b/qcommsy @@ -0,0 +1 @@ +Subproject commit 83b2ec15faab5574028012ede42fbb6e6f756d8c