aboutsummaryrefslogtreecommitdiffstats
path: root/example.cpp
blob: 96ea3d43abdcb39261c493236991405ac7b83e9e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "flask.h"
#include "json.h"

#include <sstream>

int main()
{
    flask::Flask app;

    FLASK_ROUTE(app, "/")
        .name("hello")
    ([]{
        return "Hello World!";
    });

    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()};
    });

    FLASK_ROUTE(app, "/json")
    ([]{
        flask::json::wvalue x;
        x["message"] = "Hello, World!";
        return x;
    });

    FLASK_ROUTE(app, "/about")
    ([](){
        return "About Flask example.";
    });

    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);
    //});

    app.port(18080)
        .run();
}