2014-03-31 16:51:50 +00:00
|
|
|
#pragma once
|
|
|
|
#include <string>
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
#include <future>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2014-04-01 12:25:16 +00:00
|
|
|
#include "http_response.h"
|
2014-03-31 16:51:50 +00:00
|
|
|
#include "http_server.h"
|
|
|
|
|
|
|
|
// TEST
|
|
|
|
#include <iostream>
|
|
|
|
|
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-01 13:58:52 +00:00
|
|
|
if (yameHandlers_.count(req.url) == 0)
|
|
|
|
{
|
|
|
|
return response(404);
|
|
|
|
}
|
|
|
|
return yameHandlers_[req.url]();
|
2014-03-31 16:51:50 +00:00
|
|
|
}
|
|
|
|
|
2014-04-01 12:25:16 +00:00
|
|
|
template <typename F>
|
|
|
|
void route(const std::string& url, F f)
|
2014-03-31 16:51:50 +00:00
|
|
|
{
|
2014-04-02 16:38:08 +00:00
|
|
|
auto yameHandler = [f = std::move(f)]{
|
2014-04-01 12:25:16 +00:00
|
|
|
return response(f());
|
|
|
|
};
|
2014-04-01 13:58:52 +00:00
|
|
|
yameHandlers_.emplace(url, yameHandler);
|
2014-03-31 16:51:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Flask& port(std::uint16_t port)
|
|
|
|
{
|
|
|
|
port_ = port;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-03-30 13:53:56 +00:00
|
|
|
void run()
|
|
|
|
{
|
2014-03-31 16:51:50 +00:00
|
|
|
Server<Flask> server(this, port_);
|
|
|
|
server.run();
|
2014-03-30 13:53:56 +00:00
|
|
|
}
|
2014-03-31 16:51:50 +00:00
|
|
|
private:
|
|
|
|
uint16_t port_ = 80;
|
2014-04-01 12:25:16 +00:00
|
|
|
|
|
|
|
// Someday I will become real handler!
|
2014-04-01 13:58:52 +00:00
|
|
|
std::unordered_map<std::string, std::function<response()>> yameHandlers_;
|
2014-03-30 13:53:56 +00:00
|
|
|
};
|
|
|
|
};
|
2014-03-31 16:51:50 +00:00
|
|
|
|