2015-02-20 20:46:28 +00:00
|
|
|
#include "crow.h"
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
class ExampleLogHandler : public crow::ILogHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void log(std::string message, crow::LogLevel level) override
|
|
|
|
{
|
|
|
|
// cerr << "ExampleLogHandler -> " << message;
|
|
|
|
}
|
2015-02-20 20:46:28 +00:00
|
|
|
};
|
|
|
|
|
2021-11-21 16:00:44 +00:00
|
|
|
struct ExampleMiddleware
|
2015-02-20 20:46:28 +00:00
|
|
|
{
|
|
|
|
std::string message;
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
ExampleMiddleware():
|
|
|
|
message("foo")
|
2015-02-20 20:46:28 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
void setMessage(const std::string& newMsg)
|
2015-02-20 20:46:28 +00:00
|
|
|
{
|
|
|
|
message = newMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct context
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
void before_handle(crow::request& req, crow::response& res, context& ctx)
|
|
|
|
{
|
|
|
|
CROW_LOG_DEBUG << " - MESSAGE: " << message;
|
|
|
|
}
|
|
|
|
|
|
|
|
void after_handle(crow::request& req, crow::response& res, context& ctx)
|
|
|
|
{
|
|
|
|
// no-op
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
crow::App<ExampleMiddleware> app;
|
|
|
|
|
|
|
|
app.get_middleware<ExampleMiddleware>().setMessage("hello");
|
|
|
|
|
2021-05-23 08:58:23 +00:00
|
|
|
CROW_ROUTE(app, "/")
|
2021-11-25 11:45:38 +00:00
|
|
|
.name("hello")([]
|
|
|
|
{ return "Hello World!"; });
|
2015-02-20 20:46:28 +00:00
|
|
|
|
2021-05-23 08:58:23 +00:00
|
|
|
CROW_ROUTE(app, "/about")
|
2021-11-25 11:45:38 +00:00
|
|
|
([]()
|
|
|
|
{ return "About Crow example."; });
|
2015-02-20 20:46:28 +00:00
|
|
|
|
|
|
|
// a request to /path should be forwarded to /path/
|
2021-05-23 08:58:23 +00:00
|
|
|
CROW_ROUTE(app, "/path/")
|
2021-11-25 11:45:38 +00:00
|
|
|
([]()
|
|
|
|
{ return "Trailing slash test case.."; });
|
2015-02-20 20:46:28 +00:00
|
|
|
|
|
|
|
// simple json response
|
2021-05-23 08:58:23 +00:00
|
|
|
CROW_ROUTE(app, "/json")
|
2021-11-25 11:45:38 +00:00
|
|
|
([]
|
|
|
|
{
|
2015-02-20 20:46:28 +00:00
|
|
|
crow::json::wvalue x;
|
|
|
|
x["message"] = "Hello, World!";
|
2021-11-25 11:45:38 +00:00
|
|
|
return x; });
|
2015-02-20 20:46:28 +00:00
|
|
|
|
2021-05-23 08:58:23 +00:00
|
|
|
CROW_ROUTE(app, "/hello/<int>")
|
2021-11-25 11:45:38 +00:00
|
|
|
([](int count)
|
|
|
|
{
|
2015-02-20 20:46:28 +00:00
|
|
|
if (count > 100)
|
|
|
|
return crow::response(400);
|
|
|
|
std::ostringstream os;
|
|
|
|
os << count << " bottles of beer!";
|
2021-11-25 11:45:38 +00:00
|
|
|
return crow::response(os.str()); });
|
2015-02-20 20:46:28 +00:00
|
|
|
|
2021-05-23 08:58:23 +00:00
|
|
|
CROW_ROUTE(app, "/add/<int>/<int>")
|
2021-11-25 11:45:38 +00:00
|
|
|
([](crow::response& res, int a, int b)
|
|
|
|
{
|
2015-02-20 20:46:28 +00:00
|
|
|
std::ostringstream os;
|
|
|
|
os << a+b;
|
|
|
|
res.write(os.str());
|
2021-11-25 11:45:38 +00:00
|
|
|
res.end(); });
|
2015-02-20 20:46:28 +00:00
|
|
|
|
|
|
|
// Compile error with message "Handler type is mismatched with URL paramters"
|
|
|
|
//CROW_ROUTE(app,"/another/<int>")
|
|
|
|
//([](int a, int b){
|
2021-11-25 11:45:38 +00:00
|
|
|
//return crow::response(500);
|
2015-02-20 20:46:28 +00:00
|
|
|
//});
|
|
|
|
|
|
|
|
// more json example
|
2021-05-23 08:58:23 +00:00
|
|
|
CROW_ROUTE(app, "/add_json")
|
2021-11-25 11:45:38 +00:00
|
|
|
.methods(crow::HTTPMethod::Post)([](const crow::request& req)
|
|
|
|
{
|
2015-02-20 20:46:28 +00:00
|
|
|
auto x = crow::json::load(req.body);
|
|
|
|
if (!x)
|
|
|
|
return crow::response(400);
|
|
|
|
auto sum = x["a"].i()+x["b"].i();
|
|
|
|
std::ostringstream os;
|
|
|
|
os << sum;
|
2021-11-25 11:45:38 +00:00
|
|
|
return crow::response{os.str()}; });
|
2015-02-20 20:46:28 +00:00
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
app.route_dynamic("/params")([](const crow::request& req)
|
|
|
|
{
|
2015-02-20 20:46:28 +00:00
|
|
|
std::ostringstream os;
|
2021-11-21 16:00:44 +00:00
|
|
|
os << "Params: " << req.url_params << "\n\n";
|
2015-02-20 20:46:28 +00:00
|
|
|
os << "The key 'foo' was " << (req.url_params.get("foo") == nullptr ? "not " : "") << "found.\n";
|
|
|
|
if(req.url_params.get("pew") != nullptr) {
|
|
|
|
double countD = boost::lexical_cast<double>(req.url_params.get("pew"));
|
|
|
|
os << "The value of 'pew' is " << countD << '\n';
|
|
|
|
}
|
|
|
|
auto count = req.url_params.get_list("count");
|
|
|
|
os << "The key 'count' contains " << count.size() << " value(s).\n";
|
|
|
|
for(const auto& countVal : count) {
|
|
|
|
os << " - " << countVal << '\n';
|
|
|
|
}
|
2021-11-25 11:45:38 +00:00
|
|
|
return crow::response{os.str()}; });
|
2015-02-20 20:46:28 +00:00
|
|
|
|
|
|
|
// ignore all log
|
2021-02-08 16:25:02 +00:00
|
|
|
crow::logger::setLogLevel(crow::LogLevel::Debug);
|
2015-02-20 20:46:28 +00:00
|
|
|
//crow::logger::setHandler(std::make_shared<ExampleLogHandler>());
|
|
|
|
|
|
|
|
app.port(18080)
|
2021-11-25 11:45:38 +00:00
|
|
|
.multithreaded()
|
|
|
|
.run();
|
2015-02-20 20:46:28 +00:00
|
|
|
}
|