I guess you have to remember that Akka-HTTP originates from Spray - and so 
those choices were likely already made. (I'm sure there is a fully 
plausible performance threading reason that is beyond me too hehe).

Well I know how i'd do nesting in Java at least if its any help.

Implementing a custom directive is easy! And the requestContext which is 
passed into every layer of your route (presumably its an implicit value in 
scala?) can be extended and passed around with the additional info you 
might need such as a slowly building list of string segment paths. You can 
then signal that your at a leaf node of your tree by calling end or the 
likes - and return a single Path with the full linear evaluation of that 
point in the tree recursion.

So how I'd do it is implement my own directive called Segment maybe a 
little like this:

public abstract class SegmentDirectives extends AllDirectives


public Route segment(String segment, Route inner) {
return this.mapRequestContext((innerCtx) -> {
//You could put some logic here and then do something - you can control the 
RequestContext which gets passed to the child
//Hence you can control whether the lower level stuff get invokes or not 
based on what the full path is
 return new RequestContext(new 
MySuperNewRequestContextWhichObviouslyImplementsRequestContextInterface(
innerCtx.delegate(), segment));
}
}



}



Your new RequestContext impl could then just keep a List<String> - that it made 
available as a PathMatcher when you chose to 'finish' your tree like:

public Route endSegment(Route innerRoute){
return this.mapRequestContext(innerCtx-> {
if(innerCtx instanceof MySuperNewRequest.....){
return path(((MySuperNewRequest)innerCtx).getPathMatcherForBuiltPaths, 
innerRoute);
}
})
}

Then simply do a route a little like:

segment("v1", { segment("orders", {end({whatever})}), segment("customers", 
{end({whatever})}) }


Well obviously the 'this is how id do it' bit is a lie - I wouldn't do it. 
I'd probably ask myself why I had hundreds of apis and wanted to list those 
all in 1 mega file using a nested tree that either presumably is going to 
flip flop all over the classes in the project, or move further and further 
to the right of the screen as it becomes deeper.

I know the reality of software development is generally you get stuck with 
tough situations like that from historical decisions so fair enough if its 
really required. At the end of the day though - even writing 300 linear 
apis reusing PathMatcher variables preconfigured to do most of common 
situations can end up the same kind of amount of code as the nest 
equivalent. I know it doesn't feel like it initially but keep faith! :)

For context our largest service has around 10 classes which implement Route 
- each class probably has about 6 apis in it, and all of these are pulled 
in using Guice multi-binding - meaning I end up with 10 beautifully crafted 
readable classes called things like V1OrdersRoute, V2OrdersRoute, 
V1CustomersRoute and then all have the same OAUTH2 authentication 
protections and error logging applied when the multi-binding is injected 
and connected up to the Route flow on the HTTP server ensuring no-one goes 
without good security protocols or basic access logging. In my HTTPServer I 
simply put @Inject private MultiBinder<Route> allMyRoutes and attach it 
into a tree with the above stated requirements.

On Friday, 17 March 2017 11:25:09 UTC, Alan Burlison wrote:
>
> I'm sure I must be missing something here because I can't believe path 
> matching in Akka HTTP could be broken in the way it seems to be, because 
> it would be unusable for anything other than toy applications if it was: 
>
> I'm composing route handing from a top-level handler and sub-handlers 
> like this: 
>
> pathPrefix("root") { 
>    concat( 
>       pathPrefix("service1") { service1.route }, 
>       pathPrefix("service2") { service2.route } 
>    ) 
> } 
>
> where service1.route etc returns the sub-route for the associated 
> sub-tree. That works fine with a path of say "/root/service1", but it 
> *also* matches "/rootnotroot/service1", because pathPrefix() just 
> matches any arbitrary string prefix and not a full path segment. And if 
> I use path() instead of pathPrefix() it tries to match the entire 
> remaining path. What I'm looking for is something along the lines of 
> segment() where that fully matches just the next path segment and leaves 
> the remaining path to be matched by inner routes, but there doesn't seem 
> to be such a thing. 
>
> What am I missing? 
>
> Thanks, 
>
> -- 
> Alan Burlison 
> -- 
>

-- 


Notice:  This email is confidential and may contain copyright material of 
members of the Ocado Group. Opinions and views expressed in this message 
may not necessarily reflect the opinions and views of the members of the 
Ocado Group. 

 

If you are not the intended recipient, please notify us immediately and 
delete all copies of this message. Please note that it is your 
responsibility to scan this message for viruses. 

 

Fetch and Sizzle are trading names of Speciality Stores Limited and Fabled 
is a trading name of Marie Claire Beauty Limited, both members of the Ocado 
Group.

 

References to the “Ocado Group” are to Ocado Group plc (registered in 
England and Wales with number 7098618) and its subsidiary undertakings (as 
that expression is defined in the Companies Act 2006) from time to time. 
 The registered office of Ocado Group plc is Titan Court, 3 Bishops Square, 
Hatfield Business Park, Hatfield, Herts. AL10 9NE.

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to