-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: removed static lib dependency between tests and snail, used src…
… level dependency instead
- Loading branch information
Showing
17 changed files
with
161 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#include <snail.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
#include "snail.h" | ||
|
||
typedef struct { | ||
int val; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#include <snail.h> | ||
|
||
int main() { | ||
return sn_listen(3000, NULL); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,36 @@ | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include "../sn_common.h" | ||
#include "snail.h" | ||
|
||
#define MIDDLEWARE_CAP 4 | ||
#define MIDDLEWARE_EXT 4 | ||
|
||
int sn_routing_init(sn_router_t **router, sn_http_method_t method, const char *path, sn_route_handler_cb action) { | ||
sn_router_t *new_router; | ||
MALLOC_OR_RETURN_ERR(new_router, sn_router_t, 1) | ||
new_router->middlewares.handlers = malloc(sizeof (sn_route_handler_cb) * MIDDLEWARE_CAP); | ||
if (new_router->middlewares.handlers == NULL) { | ||
free(router); | ||
return SN_ENOMEM; | ||
} | ||
new_router->middlewares.size = 0; | ||
new_router->middlewares.capacity = MIDDLEWARE_CAP; | ||
new_router->path = path; | ||
new_router->method = method; | ||
new_router->action = action; | ||
new_router->type = SN_ROUTER; | ||
*router = new_router; | ||
return 0; | ||
void sn_route_handler_init(sn_route_handler_t *route_handler, sn_http_method_t method, const char *path, sn_req_handler handler) { | ||
sn_dlist_init(&route_handler->middlewares, NULL); | ||
route_handler->path = path; | ||
route_handler->method = method; | ||
route_handler->action = handler; | ||
route_handler->type = SN_ROUTE_HANDLER; | ||
} | ||
|
||
int sn_routing_init_group(sn_route_group_t **route_group, const char *path) { | ||
int error; | ||
sn_map_t *routes; | ||
sn_route_group_t *new_route_group; | ||
MALLOC_OR_RETURN_ERR(new_route_group, sn_route_group_t, 1) | ||
new_route_group->middlewares.handlers = malloc(sizeof (sn_route_handler_cb) * MIDDLEWARE_CAP); | ||
if (new_route_group->middlewares.handlers == NULL) { | ||
free(new_route_group); | ||
return SN_ENOMEM; | ||
} | ||
error = sn_map_init(&routes, 8, NULL); | ||
if (error != 0) { | ||
free(new_route_group->middlewares.handlers); | ||
free(new_route_group); | ||
return error; | ||
} | ||
new_route_group->path = path; | ||
new_route_group->middlewares.size = 0; | ||
new_route_group->middlewares.capacity = MIDDLEWARE_CAP; | ||
new_route_group->type = SN_ROUTER_GROUP; | ||
*route_group = new_route_group; | ||
return 0; | ||
void sn_route_group_init(sn_route_group_t *route_group, const char *path) { | ||
sn_dlist_init(&route_group->middlewares, NULL); | ||
sn_dlist_init(&route_group->routes, (sn_data_destructor) sn_route_destroy); | ||
route_group->path = path; | ||
route_group->type = SN_ROUTE_GROUP; | ||
} | ||
|
||
int sn_routing_add_group(sn_route_group_t *route_group, sn_handler_t *handler) { | ||
return sn_map_set(route_group->routes, handler->path, handler); | ||
int sn_route_add_subgroup(sn_route_group_t *route_group, sn_handler_t *handler) { | ||
return sn_dlist_push(&route_group->routes, handler); | ||
} | ||
|
||
int sn_routing_add_middleware(sn_handler_t *handler, sn_route_handler_cb middleware) { | ||
if (handler->middlewares.size < handler->middlewares.capacity) { | ||
handler->middlewares.handlers[handler->middlewares.size++] = middleware; | ||
return 0; | ||
} | ||
void *ext = realloc(handler->middlewares.handlers, (sizeof (sn_route_handler_cb) * (handler->middlewares.capacity + MIDDLEWARE_EXT))); | ||
if (ext == NULL) { | ||
return SN_ENOMEM; | ||
} | ||
handler->middlewares.capacity += MIDDLEWARE_EXT; | ||
handler->middlewares.handlers = ext; | ||
handler->middlewares.handlers[handler->middlewares.size++] = middleware; | ||
return 0; | ||
int sn_route_add_middleware(sn_handler_t *handler, sn_req_handler middleware) { | ||
return sn_dlist_push(&handler->middlewares, middleware); | ||
} | ||
|
||
void sn_routing_destroy(sn_handler_t *handler) { | ||
void sn_route_destroy(sn_handler_t *handler) { | ||
if (handler->type == SN_UNKNOWN_HANDLER) { | ||
return; | ||
} | ||
if (handler->type == SN_ROUTER) { | ||
free(handler); | ||
return; | ||
} | ||
if (handler->type == SN_ROUTER_GROUP) { | ||
sn_map_destroy(((sn_route_group_t*)handler)->routes); | ||
sn_dlist_destroy(&handler->middlewares); | ||
if (handler->type == SN_ROUTE_GROUP) { | ||
sn_dlist_destroy(&((sn_route_group_t*)handler)->routes); | ||
free(handler); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.