mirror of
https://gitlab.com/niansa/commsy-rest.git
synced 2025-03-06 20:48:27 +01:00
Initial commit
This commit is contained in:
commit
5fb5c88c8f
5 changed files with 189 additions and 0 deletions
73
.gitignore
vendored
Normal file
73
.gitignore
vendored
Normal file
|
@ -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
|
||||
|
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
[submodule "qcommsy"]
|
||||
path = qcommsy
|
||||
url = https://gitlab.com/niansa/qcommsy
|
16
commsy-rest.pro
Normal file
16
commsy-rest.pro
Normal file
|
@ -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
|
96
main.cpp
Normal file
96
main.cpp
Normal file
|
@ -0,0 +1,96 @@
|
|||
#include <drogon/drogon.h>
|
||||
#include <jsoncpp/json/json.h>
|
||||
|
||||
#include "qcommsy/libcommsy.hpp"
|
||||
|
||||
using namespace drogon;
|
||||
|
||||
|
||||
int main() {
|
||||
// Register handlers
|
||||
app().registerHandler("/",
|
||||
[](const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&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<void (const HttpResponsePtr &)> &&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<unsigned int>(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();
|
||||
}
|
1
qcommsy
Submodule
1
qcommsy
Submodule
|
@ -0,0 +1 @@
|
|||
Subproject commit 83b2ec15faab5574028012ede42fbb6e6f756d8c
|
Loading…
Add table
Reference in a new issue