Skip to content

Totodore/urlrouter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

urlrouter (WIP)

A header-only, zero-allocation, no std efficient url router in C99. It is implemented with a radix trie and scales well with a high number of routes.

It is inspired by the matchit crate in rust.

Example

#include <stdio.h>

#define URLROUTER_IO
#include "urlrouter.h"

typedef void (*user_cb_t)(urlparam id);

void user_route_cb(urlparam id)
{
	if (id.value)
		printf("New req with id: %.*s\n", id.len, id.value);
	else
		printf("New req without id\n");
}

int main(void)
{
	urlrouter router;
	char buff[1024 * 4]; // This buffer is enough to store ~50 routes
	urlrouter_init(&router, buff, 1024 * 4);
	urlrouter_add(&router, "/user/{id}/test", (void*)user_route_cb);
	urlrouter_print(&router);

	urlrouter_add(&router, "/user", (void*)user_route_cb);

	int err = urlrouter_add(&router, "/user/{id}azd", (void*)user_route_cb);
	if (err < 0)
		printf("Error adding route: %s\n", urlrouter_get_error_str(err));

	urlrouter_print(&router);

	urlparam params[10] = {0};
	user_cb_t cb = (user_cb_t)urlrouter_find(&router, "/user/168/test", params, 10, NULL);
	if (cb)
		cb(params[0]);
	else
		printf("Route not found\n");

	return 0;
}

About

A zero-alloc, no std, efficient url router in C99

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors