Crow/example.cpp

59 lines
1.2 KiB
C++
Raw Normal View History

2014-03-30 13:53:56 +00:00
#include "flask.h"
2014-04-17 14:06:41 +00:00
#include "json.h"
2014-03-30 13:53:56 +00:00
#include <sstream>
2014-03-30 13:53:56 +00:00
int main()
{
flask::Flask app;
2014-04-14 15:31:51 +00:00
FLASK_ROUTE(app, "/")
2014-04-17 06:50:28 +00:00
.name("hello")
([]{
return "Hello World!";
});
2014-04-25 22:44:09 +00:00
FLASK_ROUTE(app, "/about")
([](){
return "About Flask example.";
});
2014-04-25 22:44:09 +00:00
// simple json response
2014-04-17 06:50:28 +00:00
FLASK_ROUTE(app, "/json")
([]{
2014-04-17 14:06:41 +00:00
flask::json::wvalue x;
x["message"] = "Hello, World!";
return x;
2014-04-17 06:50:28 +00:00
});
FLASK_ROUTE(app,"/hello/<int>")
([](int count){
if (count > 100)
return flask::response(400);
std::ostringstream os;
os << count << " bottles of beer!";
return flask::response(os.str());
});
// Compile error with message "Handler type is mismatched with URL paramters"
//FLASK_ROUTE(app,"/another/<int>")
//([](int a, int b){
//return flask::response(500);
//});
2014-04-25 22:44:09 +00:00
// more json example
FLASK_ROUTE(app, "/add_json")
([](const flask::request& req){
auto x = flask::json::load(req.body);
if (!x)
return flask::response(400);
int sum = x["a"].i()+x["b"].i();
std::ostringstream os;
os << sum;
return flask::response{os.str()};
});
2014-04-17 06:50:28 +00:00
app.port(18080)
.run();
2014-03-30 13:53:56 +00:00
}