seasocks icon indicating copy to clipboard operation
seasocks copied to clipboard

Support automatically cascading PathHandlers

Open scallopedllama opened this issue 6 years ago • 2 comments

In order to host a CrackedUriPageHandler on a path like /foo/bar/baz, it seems to me that a separate PathHandler handler has to be created for each component of the path with the CrackedUriPageHandler only set up on the last part of the address.

If my handler class is DataHandler, it seems I have to do something like this to add it at the address /foo/bar/baz

auto root = make_shared<RootPageHandler>();
auto fooHandler = make_shared<PathHandler>("foo");
auto barHandler = make_shared<PathHandler>("bar");
auto bazHandler = make_shared<PathHandler>("baz", make_shared<DataHandler>());
root->add(fooHandler);

Is there any way to avoid having to make all of the intermediate path parts and so something like this?

auto root = make_shared<RootPageHandler>();
auto pageHandler = make_shared<AutoCrackedPathHandler>("/foo/bar/baz", make_shared<DataHandler>());
root->add(pageHandler);

scallopedllama avatar Jul 11 '18 19:07 scallopedllama

Wow; that's a deep hierarchy :) I've never had to worry about that level of depth or complexity in Seasocks!

I agree it would be convenient to be able to nest things like that, but whenever I've had more than one level of hierarchy I always ended up putting multiple pages at levels of the hierarchy, so having fooHandler around still to add pages to still made sense.

mattgodbolt avatar Jul 11 '18 23:07 mattgodbolt

I'm currently working on using seasocks to implement a REST-like API that uses with C++ libraries. I ended up implementing something internal to my program that automatically builds up the PathHandlers needed, the pseudocode is like this:

Hash <string, shared_pointer<PathHandler>> handlers
for all endpoints:
    split destination address
    for all destination address pieces except for the last one:
       Check hash for presence of PathHandler on the needed piece
       If it exists:
            continue
        If it does not:
            Create a PathHandler for the piece and install it to the previous piece's PathHandler
            Add this new PathHandler to the hash
    Create new PathHandler for the final address piece to handle that address, install to the final PathHandler in the chain

scallopedllama avatar Aug 08 '18 18:08 scallopedllama