You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.3 KiB
C++

#ifndef WEBCC_ROUTER_H_
#define WEBCC_ROUTER_H_
#include <regex>
#include <string>
#include "webcc/globals.h"
#include "webcc/view.h"
namespace webcc {
class Router {
public:
virtual ~Router() = default;
// Route a URL to a view.
// The URL should start with "/". E.g., "/instances".
bool Route(string_view url, ViewPtr view,
const Strings& methods = { "GET" });
// Route a URL (as regular expression) to a view.
// The URL should start with "/" and be a regular expression.
// E.g., "/instances/(\\d+)".
bool Route(const UrlRegex& regex_url, ViewPtr view,
const Strings& methods = { "GET" });
// Find the view by HTTP method and URL (path).
ViewPtr FindView(const std::string& method, const std::string& url,
UrlArgs* args);
// Match the view by HTTP method and URL (path).
// Return if a view is matched or not.
// If the view asks for data streaming, |stream| will be set to true.
bool MatchView(const std::string& method, const std::string& url,
bool* stream);
private:
struct RouteInfo {
std::string url;
std::regex url_regex;
ViewPtr view;
Strings methods;
};
// Route table.
std::vector<RouteInfo> routes_;
};
} // namespace webcc
#endif // WEBCC_ROUTER_H_