register method
Register a route handler
Implementation
void register(HttpMethod method, String path, Function handler) {
// Normalize path: ensure start with / and remove trailing /
if (!path.startsWith('/')) path = '/$path';
if (path.length > 1 && path.endsWith('/')) {
path = path.substring(0, path.length - 1);
}
final segments = path.split('/').where((s) => s.isNotEmpty).toList();
var current = _root;
for (final segment in segments) {
if (segment.startsWith(':')) {
// Parameter node
// In this simple trie, we assume only one param node per level for simplicity
// Ideally we check if existing child is param or not
final paramName = segment.substring(1);
// Find or create param child
// For now, let's use a special key for storage, e.g., '?'
if (!current.children.containsKey('?')) {
final node = TrieNode('?');
node.isParam = true;
node.paramName = paramName;
current.children['?'] = node;
}
current = current.children['?']!;
} else {
// Static node
current = current.children.putIfAbsent(
segment,
() => TrieNode(segment),
);
}
}
current.handlers[method] = handler;
}