From 4f815d195a8b5e9abba5909e43eaa6dfd849f5d6 Mon Sep 17 00:00:00 2001 From: Simon Hardt Date: Sat, 29 May 2021 02:34:31 +0200 Subject: [PATCH] Update Names in api --- .gitignore | 1 + Modules/CMakeLists.txt | 1 + Modules/Server/CMakeLists.txt | 2 +- Modules/Server/src/api.cpp | 32 +++++++++++++++++++++++++------- Modules/Server/src/api.hpp | 2 +- Modules/Server/src/main.cpp | 3 ++- Modules/Test.http | 10 ++++++++++ 7 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 Modules/Test.http diff --git a/.gitignore b/.gitignore index 4780ab3..3a3fd67 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ cmake-build-release/ CMakeSettings.json Modules/Website/elm-stuff/ +Modules/Website/.vscode/settings.json diff --git a/Modules/CMakeLists.txt b/Modules/CMakeLists.txt index 083d9dd..feeafa6 100644 --- a/Modules/CMakeLists.txt +++ b/Modules/CMakeLists.txt @@ -1 +1,2 @@ +add_subdirectory(Website) add_subdirectory(Server) \ No newline at end of file diff --git a/Modules/Server/CMakeLists.txt b/Modules/Server/CMakeLists.txt index 29b931e..fc6c341 100644 --- a/Modules/Server/CMakeLists.txt +++ b/Modules/Server/CMakeLists.txt @@ -20,4 +20,4 @@ target_link_libraries(. nlohmann_json::nlohmann_json Boost::boost ${Boost_FILESYSTEM_LIBRARY} - ${Boost_SYSTEM_LIBRARY}) \ No newline at end of file + ${Boost_SYSTEM_LIBRARY}) diff --git a/Modules/Server/src/api.cpp b/Modules/Server/src/api.cpp index 25621a6..bbcabc4 100644 --- a/Modules/Server/src/api.cpp +++ b/Modules/Server/src/api.cpp @@ -4,6 +4,7 @@ #include "api.hpp" #include "tables.hpp" +#include #include #include @@ -25,16 +26,15 @@ void Api::RegisterServerHandles(httplib::Server& server) server.Post("/api/add", std::bind_front(&Api::add, this)); - server.Get("/api/queue", std::bind_front(&Api::queue, this)); + server.Get("/api/files", std::bind_front(&Api::files, this)); server.Get("/api/file/(\\d+)", std::bind_front(&Api::file, this)); - server.Options("/(.*)", [this](httplib::Request const& rq, httplib::Response& rp) { - spdlog::info("Optin"); - }); + server.Options("/(.*)", [this](httplib::Request const& rq, httplib::Response& rp) {}); server.set_pre_routing_handler([this](httplib::Request const& rq, httplib::Response& rp) { this->set_cross_headers(rp); + spdlog::info("Request {}", rq.method); return httplib::Server::HandlerResponse::Unhandled; }); } @@ -74,13 +74,13 @@ void Api::add(const httplib::Request& rq, httplib::Response& rs) } } -void Api::queue(httplib::Request const& rq, httplib::Response& rs) +void Api::files(httplib::Request const& rq, httplib::Response& rs) { try { json res; - soci::rowset data = (sql.prepare << "SELECT * FROM Files"); + soci::rowset data = (sql.prepare << "SELECT * FROM Files ORDER BY id DESC"); for (auto const& el : data) { @@ -88,7 +88,25 @@ void Api::queue(httplib::Request const& rq, httplib::Response& rs) current["id"] = el.id; current["status"] = magic_enum::enum_name(el.status); current["status_id"] = static_cast(el.status); - current["url"] = el.url; + current["source"] = el.url; + current["name"] = fmt::format("File {}", el.id); + + current["file_name"] = "Unknown"; + + if (el.status == FileStatus::COMPLETE && false) + { + std::string filename; + soci::rowset ret = + (sql.prepare << "SELECT path From Downloads WHERE id=:id", + soci::use(el.id, "id")); + + if (ret.begin() != ret.end()) + { + std::filesystem::path p(*ret.begin()); + + current["file_name"] = p.filename(); + } + } res["queue"].push_back(current); } diff --git a/Modules/Server/src/api.hpp b/Modules/Server/src/api.hpp index e25feaa..ab2e242 100644 --- a/Modules/Server/src/api.hpp +++ b/Modules/Server/src/api.hpp @@ -15,7 +15,7 @@ public: public: // Endpoints void add(httplib::Request const& rq, httplib::Response& rs); - void queue(httplib::Request const& rq, httplib::Response& rs); + void files(httplib::Request const& rq, httplib::Response& rs); void file(httplib::Request const& rq, httplib::Response& rs); void set_cross_headers(httplib::Response& rs); diff --git a/Modules/Server/src/main.cpp b/Modules/Server/src/main.cpp index 5098ad3..742966f 100644 --- a/Modules/Server/src/main.cpp +++ b/Modules/Server/src/main.cpp @@ -68,7 +68,8 @@ int main() Worker worker(sql); httplib::Server srv; - srv.set_mount_point("/", "downloads/"); + srv.set_mount_point("/", "www/"); + srv.set_mount_point("/test", "www/"); Api api(sql, srv); diff --git a/Modules/Test.http b/Modules/Test.http new file mode 100644 index 0000000..0d56b2d --- /dev/null +++ b/Modules/Test.http @@ -0,0 +1,10 @@ +POST 127.0.0.1:80/api/add +Content-Type: text/json + +{ +"url": "https://www.youtube.com/watch?v=ZWIwLMpgcbI" +} + +### + +GET 127.0.0.1:80/api/queue \ No newline at end of file