|
| 1 | +// The Reactive C++ Toolbox. |
| 2 | +// Copyright (C) 2013-2019 Swirly Cloud Limited |
| 3 | +// Copyright (C) 2019 Reactive Markets Limited |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +#include <toolbox/http.hpp> |
| 18 | +#include <toolbox/io.hpp> |
| 19 | +#include <toolbox/sys.hpp> |
| 20 | + |
| 21 | +using namespace std; |
| 22 | +using namespace toolbox; |
| 23 | + |
| 24 | +namespace { |
| 25 | + |
| 26 | +void on_foo(const HttpRequest& req, HttpStream& os) |
| 27 | +{ |
| 28 | + os << "Hello, Foo!"; |
| 29 | +} |
| 30 | + |
| 31 | +void on_bar(const HttpRequest& req, HttpStream& os) |
| 32 | +{ |
| 33 | + os << "Hello, Bar!"; |
| 34 | +} |
| 35 | + |
| 36 | +class HttpApp : public HttpAppBase { |
| 37 | + public: |
| 38 | + using Slot = BasicSlot<const HttpRequest&, HttpStream&>; |
| 39 | + using SlotMap = std::unordered_map<std::string, Slot>; |
| 40 | + |
| 41 | + ~HttpApp() override = default; |
| 42 | + void bind(const std::string& path, Slot slot) { slot_map_[path] = slot; } |
| 43 | + |
| 44 | + protected: |
| 45 | + void do_on_connect(CyclTime now, const Endpoint& ep) noexcept override |
| 46 | + { |
| 47 | + TOOLBOX_INFO << "http session connected: " << ep; |
| 48 | + } |
| 49 | + void do_on_disconnect(CyclTime now, const Endpoint& ep) noexcept override |
| 50 | + { |
| 51 | + TOOLBOX_INFO << "http session disconnected: " << ep; |
| 52 | + } |
| 53 | + void do_on_error(CyclTime now, const Endpoint& ep, const std::exception& e, |
| 54 | + HttpStream& os) noexcept override |
| 55 | + { |
| 56 | + TOOLBOX_ERROR << "session error: " << ep << ": " << e.what(); |
| 57 | + } |
| 58 | + void do_on_message(CyclTime now, const Endpoint& ep, const HttpRequest& req, |
| 59 | + HttpStream& os) override |
| 60 | + { |
| 61 | + const auto it = slot_map_.find(string{req.path()}); |
| 62 | + if (it != slot_map_.end()) { |
| 63 | + os.reset(HttpStatus::Ok, TextPlain); |
| 64 | + it->second(req, os); |
| 65 | + } else { |
| 66 | + os.reset(HttpStatus::NotFound, TextPlain); |
| 67 | + os << "Error 404 - Page not found"; |
| 68 | + } |
| 69 | + os.commit(); |
| 70 | + } |
| 71 | + void do_on_timeout(CyclTime now, const Endpoint& ep) noexcept override |
| 72 | + { |
| 73 | + TOOLBOX_WARNING << "session timeout: " << ep; |
| 74 | + } |
| 75 | + |
| 76 | + private: |
| 77 | + SlotMap slot_map_; |
| 78 | +}; |
| 79 | + |
| 80 | +} // namespace |
| 81 | + |
| 82 | +int main(int argc, char* argv[]) |
| 83 | +{ |
| 84 | + int ret = 1; |
| 85 | + try { |
| 86 | + |
| 87 | + const auto start_time = CyclTime::now(); |
| 88 | + |
| 89 | + EpollReactor reactor{1024}; |
| 90 | + HttpApp app; |
| 91 | + app.bind("/foo", bind<on_foo>()); |
| 92 | + app.bind("/bar", bind<on_bar>()); |
| 93 | + |
| 94 | + const TcpEndpoint ep{TcpProtocol::v4(), 8888}; |
| 95 | + HttpServ http_serv{start_time, reactor, ep, app}; |
| 96 | + |
| 97 | + // Start service threads. |
| 98 | + pthread_setname_np(pthread_self(), "main"); |
| 99 | + ReactorRunner reactor_runner{reactor, ThreadConfig{"reactor"s}}; |
| 100 | + |
| 101 | + // Wait for termination. |
| 102 | + SigWait sig_wait; |
| 103 | + for (;;) { |
| 104 | + switch (const auto sig = sig_wait()) { |
| 105 | + case SIGHUP: |
| 106 | + TOOLBOX_INFO << "received SIGHUP"; |
| 107 | + continue; |
| 108 | + case SIGINT: |
| 109 | + TOOLBOX_INFO << "received SIGINT"; |
| 110 | + break; |
| 111 | + case SIGTERM: |
| 112 | + TOOLBOX_INFO << "received SIGTERM"; |
| 113 | + break; |
| 114 | + default: |
| 115 | + TOOLBOX_INFO << "received signal: " << sig; |
| 116 | + continue; |
| 117 | + } |
| 118 | + break; |
| 119 | + } |
| 120 | + ret = 0; |
| 121 | + |
| 122 | + } catch (const std::exception& e) { |
| 123 | + TOOLBOX_ERROR << "exception: " << e.what(); |
| 124 | + } |
| 125 | + return ret; |
| 126 | +} |
0 commit comments