diff --git a/examples/example.cpp b/examples/example.cpp index c3294410f..b4cbe4094 100644 --- a/examples/example.cpp +++ b/examples/example.cpp @@ -1,5 +1,4 @@ #include "crow.h" -#include "json.h" #include diff --git a/examples/example_chat.cpp b/examples/example_chat.cpp index 2f80ffeb9..d187f5194 100644 --- a/examples/example_chat.cpp +++ b/examples/example_chat.cpp @@ -1,6 +1,4 @@ #include "crow.h" -#include "json.h" -#include "mustache.h" #include #include #include diff --git a/examples/example_vs.cpp b/examples/example_vs.cpp index 95f46b028..dbc4d3101 100644 --- a/examples/example_vs.cpp +++ b/examples/example_vs.cpp @@ -1,5 +1,4 @@ #include "crow.h" -#include "json.h" #include diff --git a/examples/websocket/example_ws.cpp b/examples/websocket/example_ws.cpp index 7fbd5efe3..ec3603c29 100644 --- a/examples/websocket/example_ws.cpp +++ b/examples/websocket/example_ws.cpp @@ -1,6 +1,4 @@ #include "crow.h" -#include "mustache.h" -#include "websocket.h" #include #include diff --git a/include/crow.h b/include/crow.h index 0ec8a574c..9a0c22e11 100644 --- a/include/crow.h +++ b/include/crow.h @@ -1,242 +1,23 @@ #pragma once - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "settings.h" -#include "logging.h" -#include "utility.h" -#include "routing.h" -#include "middleware_context.h" -#include "http_request.h" -#include "http_server.h" - - -#ifdef CROW_MSVC_WORKAROUND -#define CROW_ROUTE(app, url) app.route_dynamic(url) -#else -#define CROW_ROUTE(app, url) app.route(url) -#endif - -namespace crow -{ -#ifdef CROW_ENABLE_SSL - using ssl_context_t = boost::asio::ssl::context; -#endif - template - class Crow - { - public: - using self_t = Crow; - using server_t = Server; -#ifdef CROW_ENABLE_SSL - using ssl_server_t = Server; -#endif - Crow() - { - } - - template - void handle_upgrade(const request& req, response& res, Adaptor&& adaptor) - { - router_.handle_upgrade(req, res, adaptor); - } - - void handle(const request& req, response& res) - { - router_.handle(req, res); - } - - DynamicRule& route_dynamic(std::string&& rule) - { - return router_.new_rule_dynamic(std::move(rule)); - } - - template - auto route(std::string&& rule) - -> typename std::result_of)(Router, std::string&&)>::type - { - return router_.new_rule_tagged(std::move(rule)); - } - - self_t& port(std::uint16_t port) - { - port_ = port; - return *this; - } - - self_t& bindaddr(std::string bindaddr) - { - bindaddr_ = bindaddr; - return *this; - } - - self_t& multithreaded() - { - return concurrency(std::thread::hardware_concurrency()); - } - - self_t& concurrency(std::uint16_t concurrency) - { - if (concurrency < 1) - concurrency = 1; - concurrency_ = concurrency; - return *this; - } - - void validate() - { - router_.validate(); - } - - void run() - { - validate(); -#ifdef CROW_ENABLE_SSL - if (use_ssl_) - { - ssl_server_ = std::move(std::unique_ptr(new ssl_server_t(this, bindaddr_, port_, &middlewares_, concurrency_, &ssl_context_))); - ssl_server_->set_tick_function(tick_interval_, tick_function_); - ssl_server_->run(); - } - else -#endif - { - server_ = std::move(std::unique_ptr(new server_t(this, bindaddr_, port_, &middlewares_, concurrency_, nullptr))); - server_->set_tick_function(tick_interval_, tick_function_); - server_->run(); - } - } - - void stop() - { -#ifdef CROW_ENABLE_SSL - if (use_ssl_) - { - ssl_server_->stop(); - } - else -#endif - { - server_->stop(); - } - } - - void debug_print() - { - CROW_LOG_DEBUG << "Routing:"; - router_.debug_print(); - } - -#ifdef CROW_ENABLE_SSL - self_t& ssl_file(const std::string& crt_filename, const std::string& key_filename) - { - use_ssl_ = true; - ssl_context_.set_verify_mode(boost::asio::ssl::verify_peer); - ssl_context_.use_certificate_file(crt_filename, ssl_context_t::pem); - ssl_context_.use_private_key_file(key_filename, ssl_context_t::pem); - ssl_context_.set_options( - boost::asio::ssl::context::default_workarounds - | boost::asio::ssl::context::no_sslv2 - | boost::asio::ssl::context::no_sslv3 - ); - return *this; - } - - self_t& ssl_file(const std::string& pem_filename) - { - use_ssl_ = true; - ssl_context_.set_verify_mode(boost::asio::ssl::verify_peer); - ssl_context_.load_verify_file(pem_filename); - ssl_context_.set_options( - boost::asio::ssl::context::default_workarounds - | boost::asio::ssl::context::no_sslv2 - | boost::asio::ssl::context::no_sslv3 - ); - return *this; - } - - self_t& ssl(boost::asio::ssl::context&& ctx) - { - use_ssl_ = true; - ssl_context_ = std::move(ctx); - return *this; - } - - - bool use_ssl_{false}; - ssl_context_t ssl_context_{boost::asio::ssl::context::sslv23}; - -#else - template - self_t& ssl_file(T&&, Remain&&...) - { - // We can't call .ssl() member function unless CROW_ENABLE_SSL is defined. - static_assert( - // make static_assert dependent to T; always false - std::is_base_of::value, - "Define CROW_ENABLE_SSL to enable ssl support."); - return *this; - } - - template - self_t& ssl(T&&) - { - // We can't call .ssl() member function unless CROW_ENABLE_SSL is defined. - static_assert( - // make static_assert dependent to T; always false - std::is_base_of::value, - "Define CROW_ENABLE_SSL to enable ssl support."); - return *this; - } -#endif - - // middleware - using context_t = detail::context; - template - typename T::context& get_context(const request& req) - { - static_assert(black_magic::contains::value, "App doesn't have the specified middleware type."); - auto& ctx = *reinterpret_cast(req.middleware_context); - return ctx.template get(); - } - - template - T& get_middleware() - { - return utility::get_element_by_type(middlewares_); - } - - template - self_t& tick(Duration d, Func f) { - tick_interval_ = std::chrono::duration_cast(d); - tick_function_ = f; - return *this; - } - - private: - uint16_t port_ = 80; - uint16_t concurrency_ = 1; - std::string bindaddr_ = "0.0.0.0"; - Router router_; - - std::chrono::milliseconds tick_interval_; - std::function tick_function_; - - std::tuple middlewares_; - -#ifdef CROW_ENABLE_SSL - std::unique_ptr ssl_server_; -#endif - std::unique_ptr server_; - }; - template - using App = Crow; - using SimpleApp = Crow<>; -} +#include "crow/query_string.h" +#include "crow/http_parser_merged.h" +#include "crow/ci_map.h" +#include "crow/TinySHA1.hpp" +#include "crow/settings.h" +#include "crow/socket_adaptors.h" +#include "crow/json.h" +#include "crow/mustache.h" +#include "crow/logging.h" +#include "crow/dumb_timer_queue.h" +#include "crow/utility.h" +#include "crow/common.h" +#include "crow/http_request.h" +#include "crow/websocket.h" +#include "crow/parser.h" +#include "crow/http_response.h" +#include "crow/middleware.h" +#include "crow/routing.h" +#include "crow/middleware_context.h" +#include "crow/http_connection.h" +#include "crow/http_server.h" +#include "crow/app.h" diff --git a/include/TinySHA1.hpp b/include/crow/TinySHA1.hpp similarity index 100% rename from include/TinySHA1.hpp rename to include/crow/TinySHA1.hpp diff --git a/include/crow/app.h b/include/crow/app.h new file mode 100644 index 000000000..cb72d3a69 --- /dev/null +++ b/include/crow/app.h @@ -0,0 +1,242 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "crow/settings.h" +#include "crow/logging.h" +#include "crow/utility.h" +#include "crow/routing.h" +#include "crow/middleware_context.h" +#include "crow/http_request.h" +#include "crow/http_server.h" + + +#ifdef CROW_MSVC_WORKAROUND +#define CROW_ROUTE(app, url) app.route_dynamic(url) +#else +#define CROW_ROUTE(app, url) app.route(url) +#endif + +namespace crow +{ +#ifdef CROW_ENABLE_SSL + using ssl_context_t = boost::asio::ssl::context; +#endif + template + class Crow + { + public: + using self_t = Crow; + using server_t = Server; +#ifdef CROW_ENABLE_SSL + using ssl_server_t = Server; +#endif + Crow() + { + } + + template + void handle_upgrade(const request& req, response& res, Adaptor&& adaptor) + { + router_.handle_upgrade(req, res, adaptor); + } + + void handle(const request& req, response& res) + { + router_.handle(req, res); + } + + DynamicRule& route_dynamic(std::string&& rule) + { + return router_.new_rule_dynamic(std::move(rule)); + } + + template + auto route(std::string&& rule) + -> typename std::result_of)(Router, std::string&&)>::type + { + return router_.new_rule_tagged(std::move(rule)); + } + + self_t& port(std::uint16_t port) + { + port_ = port; + return *this; + } + + self_t& bindaddr(std::string bindaddr) + { + bindaddr_ = bindaddr; + return *this; + } + + self_t& multithreaded() + { + return concurrency(std::thread::hardware_concurrency()); + } + + self_t& concurrency(std::uint16_t concurrency) + { + if (concurrency < 1) + concurrency = 1; + concurrency_ = concurrency; + return *this; + } + + void validate() + { + router_.validate(); + } + + void run() + { + validate(); +#ifdef CROW_ENABLE_SSL + if (use_ssl_) + { + ssl_server_ = std::move(std::unique_ptr(new ssl_server_t(this, bindaddr_, port_, &middlewares_, concurrency_, &ssl_context_))); + ssl_server_->set_tick_function(tick_interval_, tick_function_); + ssl_server_->run(); + } + else +#endif + { + server_ = std::move(std::unique_ptr(new server_t(this, bindaddr_, port_, &middlewares_, concurrency_, nullptr))); + server_->set_tick_function(tick_interval_, tick_function_); + server_->run(); + } + } + + void stop() + { +#ifdef CROW_ENABLE_SSL + if (use_ssl_) + { + ssl_server_->stop(); + } + else +#endif + { + server_->stop(); + } + } + + void debug_print() + { + CROW_LOG_DEBUG << "Routing:"; + router_.debug_print(); + } + +#ifdef CROW_ENABLE_SSL + self_t& ssl_file(const std::string& crt_filename, const std::string& key_filename) + { + use_ssl_ = true; + ssl_context_.set_verify_mode(boost::asio::ssl::verify_peer); + ssl_context_.use_certificate_file(crt_filename, ssl_context_t::pem); + ssl_context_.use_private_key_file(key_filename, ssl_context_t::pem); + ssl_context_.set_options( + boost::asio::ssl::context::default_workarounds + | boost::asio::ssl::context::no_sslv2 + | boost::asio::ssl::context::no_sslv3 + ); + return *this; + } + + self_t& ssl_file(const std::string& pem_filename) + { + use_ssl_ = true; + ssl_context_.set_verify_mode(boost::asio::ssl::verify_peer); + ssl_context_.load_verify_file(pem_filename); + ssl_context_.set_options( + boost::asio::ssl::context::default_workarounds + | boost::asio::ssl::context::no_sslv2 + | boost::asio::ssl::context::no_sslv3 + ); + return *this; + } + + self_t& ssl(boost::asio::ssl::context&& ctx) + { + use_ssl_ = true; + ssl_context_ = std::move(ctx); + return *this; + } + + + bool use_ssl_{false}; + ssl_context_t ssl_context_{boost::asio::ssl::context::sslv23}; + +#else + template + self_t& ssl_file(T&&, Remain&&...) + { + // We can't call .ssl() member function unless CROW_ENABLE_SSL is defined. + static_assert( + // make static_assert dependent to T; always false + std::is_base_of::value, + "Define CROW_ENABLE_SSL to enable ssl support."); + return *this; + } + + template + self_t& ssl(T&&) + { + // We can't call .ssl() member function unless CROW_ENABLE_SSL is defined. + static_assert( + // make static_assert dependent to T; always false + std::is_base_of::value, + "Define CROW_ENABLE_SSL to enable ssl support."); + return *this; + } +#endif + + // middleware + using context_t = detail::context; + template + typename T::context& get_context(const request& req) + { + static_assert(black_magic::contains::value, "App doesn't have the specified middleware type."); + auto& ctx = *reinterpret_cast(req.middleware_context); + return ctx.template get(); + } + + template + T& get_middleware() + { + return utility::get_element_by_type(middlewares_); + } + + template + self_t& tick(Duration d, Func f) { + tick_interval_ = std::chrono::duration_cast(d); + tick_function_ = f; + return *this; + } + + private: + uint16_t port_ = 80; + uint16_t concurrency_ = 1; + std::string bindaddr_ = "0.0.0.0"; + Router router_; + + std::chrono::milliseconds tick_interval_; + std::function tick_function_; + + std::tuple middlewares_; + +#ifdef CROW_ENABLE_SSL + std::unique_ptr ssl_server_; +#endif + std::unique_ptr server_; + }; + template + using App = Crow; + using SimpleApp = Crow<>; +} diff --git a/include/ci_map.h b/include/crow/ci_map.h similarity index 100% rename from include/ci_map.h rename to include/crow/ci_map.h diff --git a/include/common.h b/include/crow/common.h similarity index 99% rename from include/common.h rename to include/crow/common.h index 820337847..84d13614f 100644 --- a/include/common.h +++ b/include/crow/common.h @@ -4,7 +4,7 @@ #include #include #include -#include "utility.h" +#include "crow/utility.h" namespace crow { diff --git a/include/dumb_timer_queue.h b/include/crow/dumb_timer_queue.h similarity index 98% rename from include/dumb_timer_queue.h rename to include/crow/dumb_timer_queue.h index 6b690bb96..fd5151834 100644 --- a/include/dumb_timer_queue.h +++ b/include/crow/dumb_timer_queue.h @@ -6,7 +6,7 @@ #include #include -#include "logging.h" +#include "crow/logging.h" namespace crow { diff --git a/include/http_connection.h b/include/crow/http_connection.h similarity index 98% rename from include/http_connection.h rename to include/crow/http_connection.h index e8eb4b1fe..96f2d1458 100644 --- a/include/http_connection.h +++ b/include/crow/http_connection.h @@ -7,15 +7,15 @@ #include #include -#include "http_parser_merged.h" +#include "crow/http_parser_merged.h" -#include "parser.h" -#include "http_response.h" -#include "logging.h" -#include "settings.h" -#include "dumb_timer_queue.h" -#include "middleware_context.h" -#include "socket_adaptors.h" +#include "crow/parser.h" +#include "crow/http_response.h" +#include "crow/logging.h" +#include "crow/settings.h" +#include "crow/dumb_timer_queue.h" +#include "crow/middleware_context.h" +#include "crow/socket_adaptors.h" namespace crow { diff --git a/include/http_parser_merged.h b/include/crow/http_parser_merged.h similarity index 100% rename from include/http_parser_merged.h rename to include/crow/http_parser_merged.h diff --git a/include/http_request.h b/include/crow/http_request.h similarity index 95% rename from include/http_request.h rename to include/crow/http_request.h index 535a1fd84..a70c29901 100644 --- a/include/http_request.h +++ b/include/crow/http_request.h @@ -1,10 +1,11 @@ #pragma once -#include "common.h" -#include "ci_map.h" -#include "query_string.h" #include +#include "crow/common.h" +#include "crow/ci_map.h" +#include "crow/query_string.h" + namespace crow { template diff --git a/include/http_response.h b/include/crow/http_response.h similarity index 97% rename from include/http_response.h rename to include/crow/http_response.h index 23f312a03..f5e03b46a 100644 --- a/include/http_response.h +++ b/include/crow/http_response.h @@ -1,9 +1,10 @@ #pragma once #include #include -#include "json.h" -#include "http_request.h" -#include "ci_map.h" + +#include "crow/json.h" +#include "crow/http_request.h" +#include "crow/ci_map.h" namespace crow { diff --git a/include/http_server.h b/include/crow/http_server.h similarity index 98% rename from include/http_server.h rename to include/crow/http_server.h index 49c4ef050..d5abb1189 100644 --- a/include/http_server.h +++ b/include/crow/http_server.h @@ -13,9 +13,9 @@ #include -#include "http_connection.h" -#include "logging.h" -#include "dumb_timer_queue.h" +#include "crow/http_connection.h" +#include "crow/logging.h" +#include "crow/dumb_timer_queue.h" namespace crow { diff --git a/include/json.h b/include/crow/json.h similarity index 99% rename from include/json.h rename to include/crow/json.h index f0b4a872d..891fedfcb 100644 --- a/include/json.h +++ b/include/crow/json.h @@ -12,7 +12,7 @@ #include #include -#include "settings.h" +#include "crow/settings.h" #if defined(__GNUG__) || defined(__clang__) #define crow_json_likely(x) __builtin_expect(x, 1) diff --git a/include/logging.h b/include/crow/logging.h similarity index 99% rename from include/logging.h rename to include/crow/logging.h index 13cdad245..9c8bfbbd3 100644 --- a/include/logging.h +++ b/include/crow/logging.h @@ -7,7 +7,7 @@ #include #include -#include "settings.h" +#include "crow/settings.h" namespace crow { diff --git a/include/middleware.h b/include/crow/middleware.h similarity index 98% rename from include/middleware.h rename to include/crow/middleware.h index 534a87a36..5e3ea322b 100644 --- a/include/middleware.h +++ b/include/crow/middleware.h @@ -1,7 +1,7 @@ #pragma once #include -#include "http_request.h" -#include "http_response.h" +#include "crow/http_request.h" +#include "crow/http_response.h" namespace crow { diff --git a/include/middleware_context.h b/include/crow/middleware_context.h similarity index 96% rename from include/middleware_context.h rename to include/crow/middleware_context.h index daaaa5c7d..6885c139d 100644 --- a/include/middleware_context.h +++ b/include/crow/middleware_context.h @@ -1,8 +1,8 @@ #pragma once -#include "utility.h" -#include "http_request.h" -#include "http_response.h" +#include "crow/utility.h" +#include "crow/http_request.h" +#include "crow/http_response.h" namespace crow { diff --git a/include/mustache.h b/include/crow/mustache.h similarity index 99% rename from include/mustache.h rename to include/crow/mustache.h index 43e2b0809..bfb9f1432 100644 --- a/include/mustache.h +++ b/include/crow/mustache.h @@ -4,7 +4,7 @@ #include #include #include -#include "json.h" +#include "crow/json.h" namespace crow { namespace mustache diff --git a/include/parser.h b/include/crow/parser.h similarity index 98% rename from include/parser.h rename to include/crow/parser.h index b62185023..4a4f57e55 100644 --- a/include/parser.h +++ b/include/crow/parser.h @@ -6,8 +6,8 @@ #include #include -#include "http_parser_merged.h" -#include "http_request.h" +#include "crow/http_parser_merged.h" +#include "crow/http_request.h" namespace crow { diff --git a/include/query_string.h b/include/crow/query_string.h similarity index 99% rename from include/query_string.h rename to include/crow/query_string.h index 03e5cfdf8..3f8bffecd 100644 --- a/include/query_string.h +++ b/include/crow/query_string.h @@ -6,6 +6,8 @@ #include #include +namespace crow +{ // ---------------------------------------------------------------------------- // qs_parse (modified) // https://github.com/bartgrantham/qs_parse @@ -231,6 +233,7 @@ inline char * qs_scanvalue(const char * key, const char * qs, char * val, size_t return val; } +} // ---------------------------------------------------------------------------- diff --git a/include/routing.h b/include/crow/routing.h similarity index 99% rename from include/routing.h rename to include/crow/routing.h index 337d258ce..a8aa233c9 100644 --- a/include/routing.h +++ b/include/crow/routing.h @@ -8,12 +8,12 @@ #include #include -#include "common.h" -#include "http_response.h" -#include "http_request.h" -#include "utility.h" -#include "logging.h" -#include "websocket.h" +#include "crow/common.h" +#include "crow/http_response.h" +#include "crow/http_request.h" +#include "crow/utility.h" +#include "crow/logging.h" +#include "crow/websocket.h" namespace crow { diff --git a/include/settings.h b/include/crow/settings.h similarity index 100% rename from include/settings.h rename to include/crow/settings.h diff --git a/include/socket_adaptors.h b/include/crow/socket_adaptors.h similarity index 98% rename from include/socket_adaptors.h rename to include/crow/socket_adaptors.h index 634bd4bcd..eebd50f9f 100644 --- a/include/socket_adaptors.h +++ b/include/crow/socket_adaptors.h @@ -3,7 +3,7 @@ #ifdef CROW_ENABLE_SSL #include #endif -#include "settings.h" +#include "crow/settings.h" namespace crow { using namespace boost; diff --git a/include/utility.h b/include/crow/utility.h similarity index 99% rename from include/utility.h rename to include/crow/utility.h index 26ca151cf..43503e810 100644 --- a/include/utility.h +++ b/include/crow/utility.h @@ -8,7 +8,7 @@ #include #include -#include "settings.h" +#include "crow/settings.h" namespace crow { diff --git a/include/websocket.h b/include/crow/websocket.h similarity index 99% rename from include/websocket.h rename to include/crow/websocket.h index 5b82046b5..a1e8e8f75 100644 --- a/include/websocket.h +++ b/include/crow/websocket.h @@ -1,8 +1,8 @@ #pragma once #include -#include "socket_adaptors.h" -#include "http_request.h" -#include "TinySHA1.hpp" +#include "crow/socket_adaptors.h" +#include "crow/http_request.h" +#include "crow/TinySHA1.hpp" namespace crow { diff --git a/tests/template/mustachetest.cpp b/tests/template/mustachetest.cpp index 1caf53792..0041889a3 100644 --- a/tests/template/mustachetest.cpp +++ b/tests/template/mustachetest.cpp @@ -2,8 +2,8 @@ #include #include #include -#include "mustache.h" -#include "json.h" +#include "crow/mustache.h" +#include "crow/json.h" using namespace std; using namespace crow; using namespace crow::mustache; diff --git a/tests/unittest.cpp b/tests/unittest.cpp index 241413419..a85f7bc01 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -3,16 +3,9 @@ #include #include #include -#include "settings.h" +#include "crow.h" #undef CROW_LOG_LEVEL #define CROW_LOG_LEVEL 0 -#include "routing.h" -#include "utility.h" -#include "crow.h" -#include "json.h" -#include "mustache.h" -#include "middleware.h" -#include "query_string.h" using namespace std; using namespace crow;