Update Names in api

This commit is contained in:
Simon Hardt
2021-05-29 02:34:31 +02:00
parent a374a7c01a
commit 4f815d195a
7 changed files with 41 additions and 10 deletions

View File

@@ -1 +1,2 @@
add_subdirectory(Website)
add_subdirectory(Server)

View File

@@ -20,4 +20,4 @@ target_link_libraries(.
nlohmann_json::nlohmann_json
Boost::boost
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY})
${Boost_SYSTEM_LIBRARY})

View File

@@ -4,6 +4,7 @@
#include "api.hpp"
#include "tables.hpp"
#include <filesystem>
#include <fstream>
#include <functional>
@@ -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<File> data = (sql.prepare << "SELECT * FROM Files");
soci::rowset<File> 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<int>(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<std::string> 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);
}

View File

@@ -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);

View File

@@ -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);

10
Modules/Test.http Normal file
View File

@@ -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