2014-03-31 16:51:50 +00:00
|
|
|
#pragma once
|
|
|
|
#include <string>
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
#include <future>
|
|
|
|
#include <stdint.h>
|
2014-04-09 23:17:08 +00:00
|
|
|
#include <type_traits>
|
2014-04-17 06:50:28 +00:00
|
|
|
#include <thread>
|
2014-03-31 16:51:50 +00:00
|
|
|
|
2014-04-21 18:27:53 +00:00
|
|
|
//#define FLASK_ENABLE_LOGGING
|
2014-04-20 08:45:10 +00:00
|
|
|
|
2014-03-31 16:51:50 +00:00
|
|
|
#include "http_server.h"
|
2014-04-13 02:24:06 +00:00
|
|
|
#include "utility.h"
|
2014-04-09 23:17:08 +00:00
|
|
|
#include "routing.h"
|
2014-03-31 16:51:50 +00:00
|
|
|
|
|
|
|
// TEST
|
|
|
|
#include <iostream>
|
|
|
|
|
2014-04-13 02:24:06 +00:00
|
|
|
#define FLASK_ROUTE(app, url) app.route<flask::black_magic::get_parameter_tag(url)>(url)
|
|
|
|
|
2014-03-30 13:53:56 +00:00
|
|
|
namespace flask
|
|
|
|
{
|
|
|
|
class Flask
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Flask()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-01 13:58:52 +00:00
|
|
|
response handle(const request& req)
|
2014-03-31 16:51:50 +00:00
|
|
|
{
|
2014-04-09 23:17:08 +00:00
|
|
|
return router_.handle(req);
|
2014-03-31 16:51:50 +00:00
|
|
|
}
|
|
|
|
|
2014-04-13 02:24:06 +00:00
|
|
|
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-03-31 16:51:50 +00:00
|
|
|
Flask& port(std::uint16_t port)
|
|
|
|
{
|
|
|
|
port_ = port;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-04-17 06:50:28 +00:00
|
|
|
Flask& multithreaded()
|
|
|
|
{
|
|
|
|
return concurrency(std::thread::hardware_concurrency());
|
|
|
|
}
|
|
|
|
|
|
|
|
Flask& concurrency(std::uint16_t concurrency)
|
|
|
|
{
|
|
|
|
if (concurrency < 1)
|
|
|
|
concurrency = 1;
|
|
|
|
concurrency_ = concurrency;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-04-09 23:17:08 +00:00
|
|
|
void validate()
|
|
|
|
{
|
|
|
|
router_.validate();
|
|
|
|
}
|
|
|
|
|
2014-03-30 13:53:56 +00:00
|
|
|
void run()
|
|
|
|
{
|
2014-04-09 23:17:08 +00:00
|
|
|
validate();
|
2014-04-17 06:50:28 +00:00
|
|
|
Server<Flask> server(this, port_, concurrency_);
|
2014-03-31 16:51:50 +00:00
|
|
|
server.run();
|
2014-03-30 13:53:56 +00:00
|
|
|
}
|
2014-04-15 17:57:18 +00:00
|
|
|
void debug_print()
|
|
|
|
{
|
|
|
|
std::cerr << "Routing:" << std::endl;
|
|
|
|
router_.debug_print();
|
|
|
|
}
|
|
|
|
|
2014-03-31 16:51:50 +00:00
|
|
|
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
|
|
|
|
2014-04-09 23:17:08 +00:00
|
|
|
Router router_;
|
2014-03-30 13:53:56 +00:00
|
|
|
};
|
|
|
|
};
|
2014-03-31 16:51:50 +00:00
|
|
|
|