Skip to content
This repository was archived by the owner on Dec 8, 2021. It is now read-only.

Conversation

Tiberriver256
Copy link
Contributor

*Note this should also fix the matching issue described at the bottom of issue #116

This allows for the following scenario:

New-PolarisGetRoute -Path "/Hello/{Name}" -ScriptBlock {
                $Response.Send("Hello $($Parameters.Name)")
            }

$Result = Invoke-WebRequest -Uri "http://localhost:$Port/hello/Testing" -UseBasicParsing
$Result.Content | Should Be 'Hello Testing'

It introduces a new $Parameters PSCustomObject representing any path based parameters and it also makes route matching more strict as it is now converts paths to a regular expression using Convert-PathParameters function.

At the time a request is made the above mentioned path /Hello/{Name} would get converted to a regular expression of ^/Hello/(?<Name>.*)$ for matching against the client's requested route.

A more basic example would be that /Hello would be converted to ^/Hello$ to guarantee an exact path match. I've also added some test cases to ensure we are throwing 404 errors for paths that don't match exactly.

@Tiberriver256
Copy link
Contributor Author

Appveyor is still drunk, but tests appear to be passing. Take a look at the changes and let me know what you think @tylerl0706

https://ci.appveyor.com/project/PowerShell/polaris/build/job/0qxc0cy0p5bd54g9

@TylerLeonhardt
Copy link
Member

I'll look at this :) p.s. tweeted at AppVeyor to save us! https://twitter.com/TylerLeonhardt/status/992997043433099264

@Tiberriver256
Copy link
Contributor Author

Thinking about this a bit more last night. If you like the concept I may just look into porting the module expressjs uses. It would give a more familiar syntax and theirs is pretty robust.

https://github.com/pillarjs/path-to-regexp

@TylerLeonhardt
Copy link
Member

@Tiberriver256 THIS IS AWESOME AND EXACTLY WHAT POLARIS NEEDS. Adopting what expressjs does sounds awesome for this - that's one of my favorite features of express actually.

@TylerLeonhardt
Copy link
Member

Do you still want me to review this or do you want to take a crack at following what expressjs does 😀

@Tiberriver256
Copy link
Contributor Author

Tiberriver256 commented May 7, 2018 via email

@TimCurwick
Copy link
Contributor

Nice enhancement, but the implementation makes certain assumptions about our desired end state that may make sense in the context of the this enhancement, but may not be valid for other desired behaviors. I would prefer if we take the time to discuss and agree on a set of behaviors and design requirements, so that we can design an elegant, comprehensive solution, rather than hoping we get there through a series of small breaking bug fixes and enhancements taking us in random directions.

@Tiberriver256
Copy link
Contributor Author

@TimCurwick - Agreed, I think the goal of my PR here (which was to get the discussion started) is not really standard. I opened #120 to start the discussion. Really glad people are getting into this!

@Tiberriver256
Copy link
Contributor Author

I updated the code here as a first stab at RFC001 (#120). Everything is working and has a test case for the examples I gave. It does not have a method for switching from case sensitive to being insensitive yet.

*EDIT: Looks like I've actually got one test to fix on the linux side but the code should still be okay to look at.

@TylerLeonhardt
Copy link
Member

I'm waiting on how the RFC turns out before reviewing :) just so you know I haven't forgotten about this!

Copy link
Member

@TylerLeonhardt TylerLeonhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tiberriver256 I owe you an apology for disappearing! Some other work has taken priority but I still deeply care about this project.

If fact, I'm going to be giving a talk that uses Polaris in the coming months :) ... more on that later.

I have a few comments on this PR. This is the last thing I want to get in before I push it out to the Gallery. I've got the signing process almost set up... and then in the future, it will be 100% automated and we'll be able to iterate much faster.

Again, I'm sorry for disappearing and hope that you still have an interest in Polaris!

PS. The RFC time period is over and I think we should get this in.

@@ -48,7 +48,8 @@ class Polaris {
# Run middleware in the order in which it was added
foreach ($Middleware in $Polaris.RouteMiddleware) {
$InformationVariable += $Polaris.InvokeRoute(
$Middleware.Scriptblock,
$Middleware.Scriptblock,
$Null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: some weird indenting!

@@ -68,7 +75,8 @@ class Polaris {
try {

$InformationVariable += $Polaris.InvokeRoute(
$Routes[$MatchingRoute][$Request.Method],
$Routes[$MatchingRoute][$Request.Method],
$Parameters,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit some more weird indents

@@ -192,6 +201,23 @@ class Polaris {
return $SanitizedPath
}

static [RegEx] ConvertPathToRegex([string]$Path){
Write-Debug "Path: $path"
$path = $path -replace '\.', '\.'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this replace doing anything?

$path = $path -replace '-', '\-'
$path = $path -replace '\*', '.*'
$path = "^$path$"
$path = $path -replace ":(\w+)(\?{0,1})", '(?<$1>.+)$2'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a couple comments in this function due to the complexity?

# Searching for the first route that matches by the most specific route paths first.
#
$MatchingRoute = $Routes.keys | Sort-Object -Property Length -Descending | Where-Object { $Route -match [Polaris]::ConvertPathToRegex($_) } | Select-Object -First 1
$Request.Parameters = ([PSCustomObject]$Matches)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does $Matches get defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TylerLeonhardt
Copy link
Member

Also that test failure is bogus...

Expected strings to be the same, but they were different.
String lengths are both 47.
Strings differ at index 2.
Expected: '@{bookId=123; userId=12; 0=/users/12/books/123}'
But was:  '@{userId=12; bookId=123; 0=/users/12/books/123}'

Maybe do a different kind of comparison rather than a string compare.

@Tiberriver256
Copy link
Contributor Author

Yeah, no worries on the timing. My life kind of blew up for a bit there too but seems to be close to returning to normalcy. It might be a bit before I can get to cleaning this up but super excited to do it.

@TylerLeonhardt
Copy link
Member

@Tiberriver256 Got all the release stuff done so as soon as we get this PR in I'll throw Polaris up on the Gallery!

@TylerLeonhardt
Copy link
Member

Gentle ping 😊

I'd like to get this on the Gallery.

@Tiberriver256
Copy link
Contributor Author

Tiberriver256 commented Aug 6, 2018 via email

@Tiberriver256
Copy link
Contributor Author

Should be all set sir :)

Copy link
Member

@TylerLeonhardt TylerLeonhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@TylerLeonhardt TylerLeonhardt merged commit 6156823 into PowerShell:master Aug 8, 2018
@TylerLeonhardt
Copy link
Member

Whooooooo!!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants