route api improvement
This commit is contained in:
parent
15933a061e
commit
9433e2192a
4 changed files with 29 additions and 19 deletions
19
README.md
19
README.md
|
@ -100,23 +100,24 @@ node *matched_node = r3_tree_match_entry(n, entry);
|
|||
### Routing with conditions
|
||||
|
||||
```c
|
||||
// create the match entry for capturing dynamic variables.
|
||||
match_entry * entry = match_entry_create("/foo/bar");
|
||||
entry->request_method = METHOD_GET;
|
||||
|
||||
// create a router tree with 10 children capacity (this capacity can grow dynamically)
|
||||
n = r3_tree_create(10);
|
||||
|
||||
int route_data = 3;
|
||||
|
||||
// define the route with conditions
|
||||
route *r1 = route_create("/blog/post");
|
||||
r1->request_method = METHOD_GET | METHOD_POST; // ALLOW GET OR POST METHOD
|
||||
|
||||
// insert the route path into the router tree
|
||||
r3_tree_insert_route(n, r1, &route_data );
|
||||
r3_tree_insert_routel(n, METHOD_GET | METHOD_POST, "/blog/post", sizeof("/blog/post") - 1, &route_data );
|
||||
|
||||
r3_tree_compile(n);
|
||||
|
||||
|
||||
// in your http server handler
|
||||
|
||||
// create the match entry for capturing dynamic variables.
|
||||
match_entry * entry = match_entry_create("/foo/bar");
|
||||
entry->request_method = METHOD_GET;
|
||||
|
||||
|
||||
route *matched_route = r3_tree_match_route(n, entry);
|
||||
matched_route->data; // get the data from matched route
|
||||
|
||||
|
|
|
@ -103,8 +103,12 @@ void r3_node_append_edge(node *n, edge *child);
|
|||
|
||||
node * r3_tree_insert_pathl(node *tree, char *path, int path_len, void * data);
|
||||
|
||||
route * r3_tree_insert_routel(node *tree, int method, char *path, int path_len, void *data);
|
||||
|
||||
#define r3_tree_insert_path(n,p,d) r3_tree_insert_pathl_(n,p,strlen(p), NULL, d)
|
||||
#define r3_tree_insert_route(n,r,d) r3_tree_insert_pathl_(n, r->path, r->path_len, r, d)
|
||||
|
||||
#define r3_tree_insert_route(n,method,path,data) r3_tree_insert_routel(n, method, path, strlen(path), data)
|
||||
|
||||
|
||||
/**
|
||||
* The private API to insert a path
|
||||
|
|
10
src/node.c
10
src/node.c
|
@ -429,6 +429,16 @@ route * r3_route_createl(const char * path, int path_len) {
|
|||
return info;
|
||||
}
|
||||
|
||||
|
||||
route * r3_tree_insert_routel(node *tree, int method, char *path, int path_len, void *data) {
|
||||
route *r = r3_route_createl(path, path_len);
|
||||
r->request_method = method; // ALLOW GET OR POST METHOD
|
||||
r3_tree_insert_pathl_(tree, path, path_len, r, data);
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
|
||||
node * r3_tree_insert_pathl(node *tree, char *path, int path_len, void * data)
|
||||
{
|
||||
return r3_tree_insert_pathl_(tree, path, path_len, NULL , data);
|
||||
|
|
|
@ -338,24 +338,19 @@ START_TEST(test_insert_route)
|
|||
{
|
||||
int var1 = 22;
|
||||
int var2 = 33;
|
||||
route *r1 = r3_route_create("/blog/post");
|
||||
route *r2 = r3_route_create("/blog/post");
|
||||
r1->request_method = METHOD_GET;
|
||||
r2->request_method = METHOD_POST;
|
||||
|
||||
match_entry * entry = match_entry_create("/blog/post");
|
||||
entry->request_method = METHOD_GET;
|
||||
|
||||
node * n = r3_tree_create(2);
|
||||
r3_tree_insert_route(n, r1, &var1);
|
||||
r3_tree_insert_route(n, r2, &var2);
|
||||
r3_tree_insert_route(n, METHOD_GET, "/blog/post", &var1);
|
||||
r3_tree_insert_route(n, METHOD_POST, "/blog/post", &var2);
|
||||
|
||||
route *c = r3_tree_match_route(n, entry);
|
||||
fail_if(c == NULL);
|
||||
|
||||
r3_tree_free(n);
|
||||
match_entry_free(entry);
|
||||
r3_route_free(r1);
|
||||
r3_route_free(r2);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
|
Loading…
Reference in a new issue