aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/crow.h42
-rw-r--r--include/http_response.h14
-rw-r--r--include/http_server.h12
-rw-r--r--include/json.h6
4 files changed, 51 insertions, 23 deletions
diff --git a/include/crow.h b/include/crow.h
index ec4c0ee..aff6716 100644
--- a/include/crow.h
+++ b/include/crow.h
@@ -9,7 +9,7 @@
#include <thread>
#include "settings.h"
-#include "logging.h"
+#include "logging.h"
#include "utility.h"
#include "routing.h"
#include "middleware_context.h"
@@ -64,6 +64,12 @@ namespace crow
return *this;
}
+ self_t& bindaddr(std::string bindaddr)
+ {
+ bindaddr_ = bindaddr;
+ return *this;
+ }
+
self_t& multithreaded()
{
return concurrency(std::thread::hardware_concurrency());
@@ -88,14 +94,28 @@ namespace crow
#ifdef CROW_ENABLE_SSL
if (use_ssl_)
{
- ssl_server_t server(this, port_, &middlewares_, concurrency_, &ssl_context_);
- server.run();
+ ssl_server_ = std::move(std::unique_ptr<ssl_server_t>(new ssl_server_t(this, bindaddr_, port_, &middlewares_, concurrency_, &ssl_context_)));
+ ssl_server_->run();
+ }
+ else
+#endif
+ {
+ server_ = std::move(std::unique_ptr<server_t>(new server_t(this, bindaddr_, port_, &middlewares_, concurrency_, nullptr)));
+ server_->run();
+ }
+ }
+
+ void stop()
+ {
+#ifdef CROW_ENABLE_SSL
+ if (use_ssl_)
+ {
+ ssl_server_->stop();
}
else
#endif
{
- server_t server(this, port_, &middlewares_, concurrency_, nullptr);
- server.run();
+ server_->stop();
}
}
@@ -151,7 +171,7 @@ namespace crow
// 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,
+ std::is_base_of<T, void>::value,
"Define CROW_ENABLE_SSL to enable ssl support.");
return *this;
}
@@ -162,7 +182,7 @@ namespace crow
// 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,
+ std::is_base_of<T, void>::value,
"Define CROW_ENABLE_SSL to enable ssl support.");
return *this;
}
@@ -187,13 +207,17 @@ namespace crow
private:
uint16_t port_ = 80;
uint16_t concurrency_ = 1;
-
+ std::string bindaddr_ = "0.0.0.0";
Router router_;
std::tuple<Middlewares...> middlewares_;
+
+#ifdef CROW_ENABLE_SSL
+ std::unique_ptr<ssl_server_t> ssl_server_;
+#endif
+ std::unique_ptr<server_t> server_;
};
template <typename ... Middlewares>
using App = Crow<Middlewares...>;
using SimpleApp = Crow<>;
};
-
diff --git a/include/http_response.h b/include/http_response.h
index a7b63bf..23f312a 100644
--- a/include/http_response.h
+++ b/include/http_response.h
@@ -14,9 +14,9 @@ namespace crow
template <typename Adaptor, typename Handler, typename ... Middlewares>
friend class crow::Connection;
+ int code{200};
std::string body;
json::wvalue json_value;
- int code{200};
// `headers' stores HTTP headers.
ci_map headers;
@@ -40,12 +40,12 @@ namespace crow
response() {}
explicit response(int code) : code(code) {}
response(std::string body) : body(std::move(body)) {}
- response(json::wvalue&& json_value) : json_value(std::move(json_value))
+ response(json::wvalue&& json_value) : json_value(std::move(json_value))
{
- json_mode();
+ json_mode();
}
- response(int code, std::string body) : body(std::move(body)), code(code) {}
- response(const json::wvalue& json_value) : body(json::dump(json_value))
+ response(int code, std::string body) : code(code), body(std::move(body)) {}
+ response(const json::wvalue& json_value) : body(json::dump(json_value))
{
json_mode();
}
@@ -95,7 +95,7 @@ namespace crow
if (!completed_)
{
completed_ = true;
-
+
if (complete_request_handler_)
{
complete_request_handler_();
@@ -118,7 +118,7 @@ namespace crow
bool completed_{};
std::function<void()> complete_request_handler_;
std::function<bool()> is_alive_helper_;
-
+
//In case of a JSON object, set the Content-Type header
void json_mode()
{
diff --git a/include/http_server.h b/include/http_server.h
index 80ef7a4..fbe470d 100644
--- a/include/http_server.h
+++ b/include/http_server.h
@@ -20,17 +20,18 @@ namespace crow
{
using namespace boost;
using tcp = asio::ip::tcp;
-
+
template <typename Handler, typename Adaptor = SocketAdaptor, typename ... Middlewares>
class Server
{
public:
- Server(Handler* handler, uint16_t port, std::tuple<Middlewares...>* middlewares = nullptr, uint16_t concurrency = 1, typename Adaptor::context* adaptor_ctx = nullptr)
- : acceptor_(io_service_, tcp::endpoint(asio::ip::address(), port)),
+ Server(Handler* handler, std::string bindaddr, uint16_t port, std::tuple<Middlewares...>* middlewares = nullptr, uint16_t concurrency = 1, typename Adaptor::context* adaptor_ctx = nullptr)
+ : acceptor_(io_service_, tcp::endpoint(boost::asio::ip::address::from_string(bindaddr), port)),
signals_(io_service_, SIGINT, SIGTERM),
- handler_(handler),
+ handler_(handler),
concurrency_(concurrency),
port_(port),
+ bindaddr_(bindaddr),
middlewares_(middlewares),
adaptor_ctx_(adaptor_ctx)
{
@@ -145,7 +146,7 @@ namespace crow
is, handler_, server_name_, middlewares_,
get_cached_date_str_pool_[roundrobin_index_], *timer_queue_pool_[roundrobin_index_],
adaptor_ctx_);
- acceptor_.async_accept(p->socket(),
+ acceptor_.async_accept(p->socket(),
[this, p, &is](boost::system::error_code ec)
{
if (!ec)
@@ -171,6 +172,7 @@ namespace crow
uint16_t concurrency_{1};
std::string server_name_ = "Crow/0.1";
uint16_t port_;
+ std::string bindaddr_;
unsigned int roundrobin_index_{};
std::tuple<Middlewares...>* middlewares_;
diff --git a/include/json.h b/include/json.h
index 6f9fec9..c2ce20e 100644
--- a/include/json.h
+++ b/include/json.h
@@ -82,7 +82,7 @@ namespace crow
Object,
};
- const char* get_type_str(type t) {
+ inline const char* get_type_str(type t) {
switch(t){
case type::Number: return "Number";
case type::False: return "False";
@@ -286,7 +286,9 @@ namespace crow
case type::String:
return boost::lexical_cast<int64_t>(start_, end_-start_);
default:
- throw std::runtime_error(strcat("expected number, got: ", get_type_str(t())));
+ const std::string msg = "expected number, got: "
+ + std::string(get_type_str(t()));
+ throw std::runtime_error(msg);
}
#endif
return boost::lexical_cast<int64_t>(start_, end_-start_);