Crow/flask.h

60 lines
1.1 KiB
C
Raw Normal View History

#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"
#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-04-01 13:58:52 +00:00
if (yameHandlers_.count(req.url) == 0)
{
return response(404);
}
return yameHandlers_[req.url]();
}
2014-04-01 12:25:16 +00:00
template <typename F>
void route(const std::string& url, F f)
{
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);
}
Flask& port(std::uint16_t port)
{
port_ = port;
return *this;
}
2014-03-30 13:53:56 +00:00
void run()
{
Server<Flask> server(this, port_);
server.run();
2014-03-30 13:53:56 +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
};
};