From 7e1b4ee58c1c088cd5c1bdb9aa58df0e1a239ad9 Mon Sep 17 00:00:00 2001 From: mnpk Date: Wed, 10 Jun 2015 15:53:58 +0900 Subject: Add the very basic example, and Update README.md --- README.md | 120 ++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 59 insertions(+), 61 deletions(-) (limited to 'README.md') diff --git a/README.md b/README.md index c6a9fa2..a3e2742 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,30 @@ -# Crow - ![Crow logo](http://i.imgur.com/wqivvjK.jpg) Crow is C++ microframework for web. (inspired by Python Flask) - ![Travis Build](https://travis-ci.org/ipkn/crow.svg?branch=master) +```c++ +#include "crow.h" + +int main() +{ + crow::SimpleApp app; + + CROW_ROUTE(app, "/")([](){ + return "Hello world"; + }); + + app.port(18080).multithreaded().run(); +} +``` + ## Features - Easy routing - Similiar to Flask - Type-safe Handlers (see Example) - - Very Fast + - Very Fast - ![Benchmark Result in one chart](https://docs.google.com/spreadsheets/d/1KidO9XpuwCRZ2p_JRDJj2aep61H8Sh_KDOhApizv4LE/pubchart?oid=2041467789&format=image) - More data on [crow-benchmark](https://github.com/ipkn/crow-benchmark) - Fast built-in JSON parser (crow::json) @@ -21,68 +33,54 @@ Crow is C++ microframework for web. (inspired by Python Flask) - Provide an amalgamated header file `crow_all.h' with every features - Middleware support -## Still in development +## Still in development - Built-in ORM -## Example +## Examples +#### JSON Response ```c++ +CROW_ROUTE(app, "/json") +([]{ + crow::json::wvalue x; + x["message"] = "Hello, World!"; + return x; +}); +``` -#include "crow.h" -#include "json.h" - -#include - -int main() -{ - crow::SimpleApp app; - - CROW_ROUTE(app, "/about") - ([](){ - return "About Crow example."; - }); - - // simple json response - CROW_ROUTE(app, "/json") - ([]{ - crow::json::wvalue x; - x["message"] = "Hello, World!"; - return x; - }); - - // argument - CROW_ROUTE(app,"/hello/") - ([](int count){ - if (count > 100) - return crow::response(400); - std::ostringstream os; - os << count << " bottles of beer!"; - return crow::response(os.str()); - }); - - // Compile error with message "Handler type is mismatched with URL paramters" - //CROW_ROUTE(app,"/another/") - //([](int a, int b){ - //return crow::response(500); - //}); - - // more json example - 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()}; - }); +#### Arguments +```c++ +CROW_ROUTE(app,"/hello/") +([](int count){ + if (count > 100) + return crow::response(400); + std::ostringstream os; + os << count << " bottles of beer!"; + return crow::response(os.str()); +}); +``` +Handler arguments type check at compile time +```c++ +// Compile error with message "Handler type is mismatched with URL paramters" +CROW_ROUTE(app,"/another/") +([](int a, int b){ + return crow::response(500); +}); +``` - app.port(18080) - .multithreaded() - .run(); -} +#### Handling JSON Requests +```c++ +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()}; +}); ``` ## How to Build @@ -97,7 +95,7 @@ If you just want to use crow, copy amalgamate/crow_all.h and include it. - Linking with tcmalloc/jemalloc is recommended for speed. - Now supporting VS2013 with limited functionality (only run-time check for url is available.) - + ### Building (Tests, Examples) Out-of-source build with CMake is recommended. -- cgit v1.2.3-54-g00ecf