Crow/include/crow.h

205 lines
5.8 KiB
C
Raw Normal View History

#pragma once
2014-07-08 09:30:45 +00:00
#include <string>
#include <functional>
#include <memory>
#include <future>
2014-07-08 09:30:45 +00:00
#include <cstdint>
#include <type_traits>
2014-04-17 06:50:28 +00:00
#include <thread>
2014-05-20 16:17:56 +00:00
#include "settings.h"
#include "logging.h"
#include "utility.h"
#include "routing.h"
2014-09-06 19:30:53 +00:00
#include "middleware_context.h"
#include "http_request.h"
#include "http_server.h"
2015-07-26 18:59:44 +00:00
#ifdef CROW_MSVC_WORKAROUND
#define CROW_ROUTE(app, url) app.route_dynamic(url)
#else
2014-04-26 17:19:59 +00:00
#define CROW_ROUTE(app, url) app.route<crow::black_magic::get_parameter_tag(url)>(url)
2015-07-26 18:59:44 +00:00
#endif
2014-04-26 17:19:59 +00:00
namespace crow
2014-03-30 13:53:56 +00:00
{
#ifdef CROW_ENABLE_SSL
using ssl_context_t = boost::asio::ssl::context;
#endif
template <typename ... Middlewares>
2014-04-26 17:19:59 +00:00
class Crow
2014-03-30 13:53:56 +00:00
{
public:
2014-04-26 17:19:59 +00:00
using self_t = Crow;
using server_t = Server<Crow, SocketAdaptor, Middlewares...>;
#ifdef CROW_ENABLE_SSL
using ssl_server_t = Server<Crow, SSLAdaptor, Middlewares...>;
#endif
2014-04-26 17:19:59 +00:00
Crow()
2014-03-30 13:53:56 +00:00
{
}
void handle(const request& req, response& res)
{
2014-09-06 19:30:53 +00:00
router_.handle(req, res);
}
2015-02-18 15:57:01 +00:00
DynamicRule& route_dynamic(std::string&& rule)
{
return router_.new_rule_dynamic(std::move(rule));
}
template <uint64_t Tag>
auto route(std::string&& rule)
-> typename std::result_of<decltype(&Router::new_rule_tagged<Tag>)(Router, std::string&&)>::type
{
return router_.new_rule_tagged<Tag>(std::move(rule));
}
2014-04-26 17:19:59 +00:00
self_t& port(std::uint16_t port)
{
port_ = port;
return *this;
}
self_t& bindaddr(std::string bindaddr)
{
bindaddr_ = bindaddr;
return *this;
}
2014-04-26 17:19:59 +00:00
self_t& multithreaded()
2014-04-17 06:50:28 +00:00
{
return concurrency(std::thread::hardware_concurrency());
}
2014-04-26 17:19:59 +00:00
self_t& concurrency(std::uint16_t concurrency)
2014-04-17 06:50:28 +00:00
{
if (concurrency < 1)
concurrency = 1;
concurrency_ = concurrency;
return *this;
}
void validate()
{
router_.validate();
}
2014-03-30 13:53:56 +00:00
void run()
{
validate();
#ifdef CROW_ENABLE_SSL
if (use_ssl_)
{
ssl_server_t server(this, bindaddr_, port_, &middlewares_, concurrency_, &ssl_context_);
server.run();
}
else
#endif
{
server_t server(this, bindaddr_, port_, &middlewares_, concurrency_, nullptr);
server.run();
}
2014-03-30 13:53:56 +00:00
}
2014-05-20 22:30:51 +00:00
void debug_print()
{
2014-05-20 22:30:51 +00:00
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 <typename T, typename ... Remain>
self_t& ssl_file(T&& 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<T, void>::value,
"Define CROW_ENABLE_SSL to enable ssl support.");
return *this;
}
template <typename T>
self_t& ssl(T&& ctx)
{
// 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<T, void>::value,
"Define CROW_ENABLE_SSL to enable ssl support.");
return *this;
}
#endif
// middleware
using context_t = detail::context<Middlewares...>;
template <typename T>
2014-09-07 22:07:53 +00:00
typename T::context& get_context(const request& req)
{
static_assert(black_magic::contains<T, Middlewares...>::value, "App doesn't have the specified middleware type.");
auto& ctx = *reinterpret_cast<context_t*>(req.middleware_context);
2014-09-06 19:30:53 +00:00
return ctx.template get<T>();
}
template <typename T>
T& get_middleware()
{
return utility::get_element_by_type<T, Middlewares...>(middlewares_);
}
private:
uint16_t port_ = 80;
2014-04-17 06:50:28 +00:00
uint16_t concurrency_ = 1;
std::string bindaddr_ = "0.0.0.0";
Router router_;
std::tuple<Middlewares...> middlewares_;
2014-03-30 13:53:56 +00:00
};
template <typename ... Middlewares>
using App = Crow<Middlewares...>;
using SimpleApp = Crow<>;
2014-03-30 13:53:56 +00:00
};