|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, RTBtech, MediaSniper, Oleg Romanenko ([email protected]) |
| 3 | + */ |
| 4 | + |
| 5 | +#include <sniper/event/Loop.h> |
| 6 | +#include <sniper/event/Timer.h> |
| 7 | +#include <sniper/http/Server.h> |
| 8 | +#include <sniper/log/log.h> |
| 9 | +#include <sniper/std/check.h> |
| 10 | +#include <sniper/std/vector.h> |
| 11 | +#include <sniper/threads/Stop.h> |
| 12 | +#include <thread> |
| 13 | + |
| 14 | +using namespace sniper; |
| 15 | + |
| 16 | +class Server |
| 17 | +{ |
| 18 | +public: |
| 19 | + explicit Server() |
| 20 | + { |
| 21 | + for (unsigned i = 0; i < std::thread::hardware_concurrency(); i++) |
| 22 | + _workers.emplace_back(&Server::worker_noexcept, this, i); |
| 23 | + } |
| 24 | + |
| 25 | + ~Server() |
| 26 | + { |
| 27 | + for (auto& w : _workers) |
| 28 | + if (w.joinable()) |
| 29 | + w.join(); |
| 30 | + } |
| 31 | + |
| 32 | +private: |
| 33 | + void worker_noexcept(unsigned int thread_num) noexcept |
| 34 | + { |
| 35 | + try { |
| 36 | + worker(thread_num); |
| 37 | + } |
| 38 | + catch (std::exception& e) { |
| 39 | + log_err(e.what()); |
| 40 | + } |
| 41 | + catch (...) { |
| 42 | + log_err("[Server] non std::exception occured"); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + static string gen_date() |
| 47 | + { |
| 48 | + return fmt::format("Date: {:%a, %d %b %Y %H:%M:%S} GMT\r\n", fmt::gmtime(time(nullptr))); |
| 49 | + } |
| 50 | + |
| 51 | + void worker(unsigned thread_num) |
| 52 | + { |
| 53 | + auto loop = event::make_loop(); |
| 54 | + check(loop, "[Server] cannot init event loop"); |
| 55 | + |
| 56 | + http::server::Config config; |
| 57 | + config.max_conns = 100000; |
| 58 | + config.connection.keep_alive_timeout = 10min; |
| 59 | + config.connection.request_read_timeout = 10s; |
| 60 | + |
| 61 | + http::Server http_server(loop, config); |
| 62 | + check(http_server.bind("", 8090), "[Server] cannot bind to localhost:8090"); |
| 63 | + |
| 64 | + string date = gen_date(); |
| 65 | + http_server.set_cb_request([&, this](const auto& conn, const auto& req, const auto& resp) { |
| 66 | + resp->add_header_nocopy("Server: libsniper\r\n"); |
| 67 | + resp->add_header_copy(date); |
| 68 | + |
| 69 | + if (req->path() == "/plaintext") { |
| 70 | + resp->code = http::ResponseStatus::OK; |
| 71 | + resp->add_header_nocopy("Content-Type: text/plain; charset=UTF-8\r\n"); |
| 72 | + resp->set_data_nocopy("Hello, World!"); |
| 73 | + conn->send(resp); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + resp->code = http::ResponseStatus::NO_CONTENT; |
| 78 | + conn->send(resp); |
| 79 | + }); |
| 80 | + |
| 81 | + event::TimerRepeat timer_stop(loop, 1s, [&loop] { |
| 82 | + if (threads::Stop::get().is_stopped()) |
| 83 | + loop->break_loop(ev::ALL); |
| 84 | + }); |
| 85 | + |
| 86 | + event::TimerRepeat timer_update_time(loop, 1s, [&date] { date = gen_date(); }); |
| 87 | + |
| 88 | + loop->run(); |
| 89 | + } |
| 90 | + |
| 91 | + vector<std::thread> _workers; |
| 92 | +}; |
| 93 | + |
| 94 | +int main(int argc, char** argv) |
| 95 | +{ |
| 96 | + auto loop = event::make_loop(); |
| 97 | + if (!loop) { |
| 98 | + log_err("Main: cannot init event loop"); |
| 99 | + return EXIT_FAILURE; |
| 100 | + } |
| 101 | + |
| 102 | + signal(SIGPIPE, SIG_IGN); |
| 103 | + |
| 104 | + Server server; |
| 105 | + loop->run(); |
| 106 | + |
| 107 | + return EXIT_SUCCESS; |
| 108 | +} |
0 commit comments