File: simple_cpp.cpp

package info (click to toggle)
libr3 1.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, stretch
  • size: 1,084 kB
  • ctags: 1,086
  • sloc: ansic: 13,117; cpp: 175; makefile: 112; sh: 64; ruby: 52
file content (97 lines) | stat: -rw-r--r-- 2,954 bytes parent folder | download
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <r3.hpp>

using namespace std;

void example_1() {
    // create a router tree with 10 children capacity (this capacity can grow dynamically)
    r3::Tree tree(10);

    // insert the route path into the router tree
    int route_data_1 = 1;
    tree.insert_path("/bar", &route_data_1); // ignore the length of path

    int route_data_2 = 2;
    tree.insert_pathl("/zoo", strlen("/zoo"), &route_data_2);
    int route_data_3 = 3;
    tree.insert_pathl("/foo/bar", strlen("/foo/bar"), &route_data_3);

    int route_data_4 = 4;
    tree.insert_pathl("/post/{id}", strlen("/post/{id}") , &route_data_4);

    int route_data_5 = 5;
    tree.insert_pathl("/user/{id:\\d+}", strlen("/user/{id:\\d+}"),
        &route_data_5);

    // if you want to catch error, you may call the extended path function for insertion
    int data = 10;
    char* errstr;
    r3::Node ret = tree.insert_pathl("/foo/{name:\\d{5}",
        strlen("/foo/{name:\\d{5}"), &data, &errstr);
    if (ret == NULL) {
        // failed insertion
        cout << "error: " << errstr << endl;
        free(errstr); // errstr is created from `asprintf`, so you have to free it manually.
    }

    // let's compile the tree!
    int err = tree.compile(&errstr);
    if (err != 0) {
        cout << "error: " << errstr << endl;
        free(errstr); // errstr is created from `asprintf`, so you have to free it manually.
    }

    // dump the compiled tree
    tree.dump(0);

    // match a route
    r3::Node matched_node = tree.matchl("/foo/bar", strlen("/foo/bar"));
    if (matched_node) {
        int ret = *static_cast<int*>(matched_node.data());
        cout << "match path ret: " << ret << endl;
    }

    r3::MatchEntry entry("/foo/bar");
    matched_node = tree.match_entry(entry);
    if (matched_node) {
        int ret = *static_cast<int*>(matched_node.data());
        cout << "match entry ret: " << ret << endl;
    }
}

void example_2() {
    // create a router tree with 10 children capacity (this capacity can grow dynamically)
    r3::Tree tree(10);

    // insert the route path into the router tree
    int route_data = 1;
    tree.insert_routel(METHOD_GET | METHOD_POST, "/blog/post",
        sizeof("/blog/post") - 1, &route_data);

    char* errstr;
    int err = tree.compile(&errstr);
    if (err != 0) {
        cout << "errstr: " << errstr << endl;
        free(errstr); // errstr is created from `asprintf`, so you have to free it manually.
    }

    // in your http server handler

    // create the match entry for capturing dynamic variables.
    r3::MatchEntry entry("/blog/post");
    entry.set_request_method(METHOD_GET);

    r3::Route matched_route = tree.match_route(entry);
    if (matched_route) {
        int ret = *static_cast<int*>(matched_route.data());
        cout << "match route ret: " << ret << endl;
    }
}

int main() {
    example_1();
    example_2();
    return 0;
}