|
|
|
|
@ -1,6 +1,8 @@
|
|
|
|
|
#include <server_http.hpp> |
|
|
|
|
#include <http.hpp> |
|
|
|
|
#include <session.hpp> |
|
|
|
|
#include <libtorrent/session_stats.hpp> |
|
|
|
|
#include <boost/filesystem.hpp> |
|
|
|
|
|
|
|
|
|
typedef SimpleWeb::Server<SimpleWeb::HTTP> HttpServer; |
|
|
|
|
|
|
|
|
|
@ -8,30 +10,57 @@ using namespace std;
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) { |
|
|
|
|
int http_port=8080, http_threads=4; |
|
|
|
|
const boost::filesystem::path root_dir="/home/zalox/downloads"; |
|
|
|
|
const boost::filesystem::path default_download_dir=root_dir/"toREST"; |
|
|
|
|
if(!boost::filesystem::exists(default_download_dir)){ |
|
|
|
|
boost::system::error_code ec; |
|
|
|
|
boost::filesystem::create_directories(default_download_dir,ec); |
|
|
|
|
if(ec){ |
|
|
|
|
std::cout << ec.message() << "\n"; |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
HttpServer http_server(http_port,http_threads); |
|
|
|
|
|
|
|
|
|
libtorrent::session session; |
|
|
|
|
|
|
|
|
|
const auto stats_metrics = libtorrent::session_stats_metrics(); |
|
|
|
|
http_server.default_resource["GET"]=[](std::shared_ptr<HttpServer::Response> resp, std::shared_ptr<HttpServer::Request> req) { |
|
|
|
|
auto response = http::response(); |
|
|
|
|
response.set_status(http::not_found); |
|
|
|
|
*resp << response; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
http_server.resource["^/session(\\?.*)?\\/?$"]["GET"]=[&session](std::shared_ptr<HttpServer::Response> resp, std::shared_ptr<HttpServer::Request> req) { |
|
|
|
|
http_server.resource["^/session(\\?.*)?\\/?$"]["GET"]=[&](std::shared_ptr<HttpServer::Response> resp, std::shared_ptr<HttpServer::Request> req) { |
|
|
|
|
auto response = http::response(); |
|
|
|
|
const auto path = util::uri::parse(req->path); |
|
|
|
|
if (session.is_valid()) { |
|
|
|
|
const auto session_settings=session.get_settings(); |
|
|
|
|
|
|
|
|
|
response.set_body( |
|
|
|
|
{ |
|
|
|
|
{"paused",session.is_paused()}, |
|
|
|
|
{"up_limit",session.get_settings()} |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
const auto settings = session.get_settings(); |
|
|
|
|
response.set_body({ |
|
|
|
|
{ "paused", session.is_paused() }, |
|
|
|
|
{ "port", session.listen_port() }, |
|
|
|
|
{ "dht_enabled", session.is_dht_running() }, |
|
|
|
|
{ "down_limit", settings.get_int(settings.download_rate_limit) }, |
|
|
|
|
{ "up_limit", settings.get_int(settings.upload_rate_limit) }, |
|
|
|
|
{ "torrents", session.get_torrents().size() }, //TODO Optimize
|
|
|
|
|
{ "default_download_dir", default_download_dir.filename().string() }, |
|
|
|
|
{ "root_dir", root_dir.string() }, |
|
|
|
|
}); |
|
|
|
|
} else { |
|
|
|
|
auto response_code = http::code(http::service_unavailable); |
|
|
|
|
response.set_body({{response_code.first, response_code.second}}); |
|
|
|
|
response.set_status(response_code.first); |
|
|
|
|
} |
|
|
|
|
*resp << response; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
http_server.resource["^/session/torrents(\\?.*)?\\/?$"]["GET"]=[&session](std::shared_ptr<HttpServer::Response> resp, std::shared_ptr<HttpServer::Request> req) { |
|
|
|
|
auto response = http::response(); |
|
|
|
|
if (session.is_valid()) { |
|
|
|
|
response.set_body({ |
|
|
|
|
{ { "torrents", nlohmann::json::array() } }, |
|
|
|
|
}); |
|
|
|
|
} else { |
|
|
|
|
auto response_code = http::code(http::service_unavailable); |
|
|
|
|
response.set_body({{response_code.first, response_code.second}}); |
|
|
|
|
response.set_status(response_code.first); |
|
|
|
|
} |
|
|
|
|
*resp << response; |
|
|
|
|
}; |
|
|
|
|
|