123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- #include "crow.h"
- #include <sstream>
- class ExampleLogHandler : public crow::ILogHandler {
- public:
- void log(std::string , crow::LogLevel ) override {
- }
- };
- struct ExampleMiddleware
- {
- std::string message;
- ExampleMiddleware()
- {
- message = "foo";
- }
- void setMessage(std::string newMsg)
- {
- message = newMsg;
- }
- struct context
- {
- };
- void before_handle(crow::request& , crow::response& , context& )
- {
- CROW_LOG_DEBUG << " - MESSAGE: " << message;
- }
- void after_handle(crow::request& , crow::response& , context& )
- {
-
- }
- };
- int main()
- {
- crow::App<ExampleMiddleware> app;
- app.get_middleware<ExampleMiddleware>().setMessage("hello");
- CROW_ROUTE(app, "/")
- .name("hello")
- ([]{
- return "Hello World!";
- });
- CROW_ROUTE(app, "/about")
- ([](){
- return "About Crow example.";
- });
-
- CROW_ROUTE(app, "/path/")
- ([](){
- return "Trailing slash test case..";
- });
-
-
- CROW_ROUTE(app, "/json")
- ([]{
- crow::json::wvalue x;
- x["message"] = "Hello, World!";
- return x;
- });
-
-
- CROW_ROUTE(app,"/hello/<int>")
- ([](int count){
- if (count > 100)
- return crow::response(400);
- std::ostringstream os;
- os << count << " bottles of beer!";
- return crow::response(os.str());
- });
-
- CROW_ROUTE(app,"/add/<int>/<int>")
- ([](const crow::request& , crow::response& res, int a, int b){
- std::ostringstream os;
- os << a+b;
- res.write(os.str());
- res.end();
- });
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- CROW_ROUTE(app, "/add_json")
- .methods("POST"_method)
- ([](const crow::request& req){
- auto x = crow::json::load(req.body);
- if (!x)
- return crow::response(400);
- int sum = x["a"].i()+x["b"].i();
- std::ostringstream os;
- os << sum;
- return crow::response{os.str()};
- });
-
-
-
- CROW_ROUTE(app, "/params")
- ([](const crow::request& req){
- std::ostringstream os;
-
-
- os << "Params: " << req.url_params << "\n\n";
- 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';
- }
-
-
- auto mydict = req.url_params.get_dict("mydict");
- os << "The key 'dict' contains " << mydict.size() << " value(s).\n";
- for(const auto& mydictVal : mydict) {
- os << " - " << mydictVal.first << " -> " << mydictVal.second << '\n';
- }
- return crow::response{os.str()};
- });
- CROW_ROUTE(app, "/large")
- ([]{
- return std::string(512*1024, ' ');
- });
-
- app.loglevel(crow::LogLevel::DEBUG);
-
- app.port(18080)
- .multithreaded()
- .run();
- }
|