Crow/include/crow.h

90 lines
1.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 "http_server.h"
#include "utility.h"
#include "routing.h"
// TEST
#include <iostream>
2014-04-26 17:19:59 +00:00
#define CROW_ROUTE(app, url) app.route<crow::black_magic::get_parameter_tag(url)>(url)
2014-04-26 17:19:59 +00:00
namespace crow
2014-03-30 13:53:56 +00:00
{
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;
Crow()
2014-03-30 13:53:56 +00:00
{
}
void handle(const request& req, response& res)
{
return router_.handle(req, res);
}
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;
}
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();
2014-04-26 17:19:59 +00:00
Server<self_t> server(this, port_, concurrency_);
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();
}
private:
uint16_t port_ = 80;
2014-04-17 06:50:28 +00:00
uint16_t concurrency_ = 1;
2014-04-01 12:25:16 +00:00
Router router_;
2014-03-30 13:53:56 +00:00
};
using App = Crow;
2014-03-30 13:53:56 +00:00
};