AlexCouch / pluvo

Pluvo is a modern web framework purely in Gleam built ontop of mist
4 stars 0 forks source link

Improve LCP logic and simplify parameter matching #3

Open AlexCouch opened 8 months ago

AlexCouch commented 8 months ago

The LCP (longest common part/parent) is an algorithm which computes the longet common parent between two paths.

Take the following examples as routes:

/user/19
/user/69

We should be able to get the longest common parent /user. This would simplify our router quite a bit since the router is doing a lot of stuff just to find the right path with a request parameter.

Improving the LCP logic into a more cohesive library would help out a lot so we could have code like this (pseudocode):

//Step 1. Find LCP between request path and any given node path
for node in router.nodes{
    let lcp = lcp.find(node.path, req.path)
    if !lcp.exists() continue
    //Step 2. Check if the lcp has a parameter
    if !lcp.has_parameter() continue
    //Step 3. Get the parameter name and value
    let param = lcp.get_parameter()
    let value = req.path.last().inner()
    //Step 4. Store it in the route
    let route = routes.get(req.path)
    route.add_param(param, value) 
}