условно работающие статические файлы и "динамический" контент
This commit is contained in:
98
src/main.cpp
Normal file
98
src/main.cpp
Normal file
@@ -0,0 +1,98 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <sys/prctl.h>
|
||||
#include <boost/asio.hpp>
|
||||
#include "server/server.hpp"
|
||||
#include <boost/log/trivial.hpp>
|
||||
#include <boost/log/expressions.hpp>
|
||||
#include <boost/log/utility/setup/common_attributes.hpp>
|
||||
#include <boost/log/utility/setup/console.hpp>
|
||||
#include <boost/log/utility/setup/file.hpp>
|
||||
#include <boost/log/utility/setup/formatter_parser.hpp>
|
||||
|
||||
|
||||
namespace mime_types = http::server::mime_types;
|
||||
|
||||
void init_logging() {
|
||||
namespace log = boost::log;
|
||||
namespace keywords = log::keywords;
|
||||
namespace expressions = log::expressions;
|
||||
namespace attributes = log::attributes;
|
||||
|
||||
log::register_simple_formatter_factory<log::trivial::severity_level, char>("Severity");
|
||||
|
||||
// #ifdef USE_DEBUG
|
||||
// log::add_console_log(std::clog, keywords::format = "%TimeStamp%: [%Severity%] %Message% [%ThreadID%]");
|
||||
// #else
|
||||
// log::add_file_log(
|
||||
// keywords::file_name = "/home/root/manager_orlik_%N.log",
|
||||
// keywords::rotation_size = 10 * 1024 * 1024,
|
||||
// keywords::time_based_rotation = log::sinks::file::rotation_at_time_point(0, 0, 0),
|
||||
// keywords::format = expressions::format("%1% [%2%] [%3%] <%4%> [%5%]")
|
||||
// % expressions::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d, %H:%M:%S.%f")
|
||||
// % expressions::format_named_scope("Scope", keywords::format = "%n (%f:%l)")
|
||||
// % expressions::attr<log::trivial::severity_level>("Severity")
|
||||
// % expressions::message % expressions::attr<attributes::current_thread_id::value_type>("ThreadID"),
|
||||
// keywords::open_mode = std::ios_base::app,
|
||||
// keywords::auto_flush = true
|
||||
// );
|
||||
// #endif
|
||||
log::add_console_log(std::clog, keywords::format = "%TimeStamp%: [%Severity%] %Message% [%ThreadID%]");
|
||||
log::core::get()->set_filter(log::trivial::severity >= log::trivial::info);
|
||||
|
||||
log::add_common_attributes();
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
try {
|
||||
// Check command line arguments.
|
||||
if (argc != 3) {
|
||||
std::cerr << "Usage: http_server <address> <port>\n";
|
||||
std::cerr << " For IPv4, try:\n";
|
||||
std::cerr << " receiver 0.0.0.0 80\n";
|
||||
std::cerr << " For IPv6, try:\n";
|
||||
std::cerr << " receiver 0::0 80\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
prctl(PR_SET_NAME, "main", 0, 0, 0);
|
||||
|
||||
init_logging();
|
||||
boost::log::core::get()->add_thread_attribute("Scope", boost::log::attributes::named_scope());
|
||||
|
||||
#ifdef USE_DEBUG
|
||||
BOOST_LOG_TRIVIAL(info) << "Starting DEBUG " << argv[0];
|
||||
#else
|
||||
BOOST_LOG_TRIVIAL(info) << "Starting RELEASE build" << argv[0];
|
||||
#endif
|
||||
|
||||
// Initialise the server.
|
||||
http::server::server s(argv[1], argv[2]);
|
||||
|
||||
s.resources.emplace_back(std::make_unique<http::resource::StaticFileResource>("/", "static/login.html", mime_types::text_html));
|
||||
s.resources.emplace_back(std::make_unique<http::resource::StaticFileResource>("/favicon.ico", "static/favicon.png", mime_types::image_png));
|
||||
s.resources.emplace_back(std::make_unique<http::resource::StaticFileResource>("/js/vue.js", "static/js/vue.js", mime_types::javascript));
|
||||
|
||||
s.resources.emplace_back(std::make_unique<http::resource::GenericResource>("/api/statistics", [](const auto& req, auto& rep) {
|
||||
if (req.method != "GET") {
|
||||
http::server::stock_reply(http::server::bad_request, rep);
|
||||
}
|
||||
|
||||
rep.status = http::server::ok;
|
||||
rep.headers.clear();
|
||||
rep.headers.push_back({.name = "Content-Type", .value = to_string(mime_types::json)});
|
||||
const char* json = R"({"key":"value"})";
|
||||
rep.content.insert(rep.content.end(), json, json + strlen(json));
|
||||
}));
|
||||
|
||||
// Run the server until stopped.
|
||||
s.run();
|
||||
} catch (std::exception &e) {
|
||||
BOOST_LOG_TRIVIAL(error) << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user