fabiolb / fabio

Consul Load-Balancing made simple
https://fabiolb.net
MIT License
7.27k stars 616 forks source link

router by regular expression #723

Closed spiderDetective closed 4 years ago

spiderDetective commented 4 years ago

Hi, does fabio support regular expression in router path? if not, does it need to do so? Thanks.

pschultz commented 4 years ago

No, regular expressions are not supported. Available matchers are documented in fabio.properties: prefix and glob patterns (via https://github.com/gobwas/glob).

The globs are powerfull enough in my experience. Do you have a specific case in mind that's not covered, but possible with regular expressions (specifically RE2)?

spiderDetective commented 4 years ago

Thanks Peter for your nice reply.
fabio's prefix (iprefix) matcher is good and clear, here is my case:

my code has a http api such as : /api/(version)/register/login, /api/(version)/register/logout another system has a http api such as : /api/(version)/appinfo/sendmsg

every http api path has a version(number such as 1, 2, 3......), before system name(register or appinfo).

in this case , if i use prefix matcher, could be ->

register: "urlprefix-/api/1/register, urlprefix-/api/2/register"
appinfo: "urlprefix-/api/1/appinfo, urlprefix-/api/2/appinfo" 

i need to add urlprefix along with a new version online。

if i use glob matcher, could be ->

register: "urlprefix-/api/*/register"
appinfo: "urlprefix-/api/*/appinfo"

it's good, but relatively, if i use nginx, it could be ->

location ^/api/[1-9]/register
location ^/api/[10-20]/appinfo   

maybe i do not make myself clear, globs are powerfull. but regular expressions are more flexible. I know nginx use PCRE, and thanks for telling the RE2. @pschultz

(ps. I know it is good to change the path to /api/(system name)/(version)/(interface), but cannot fix at present)

pschultz commented 4 years ago

You can do numeric ranges with the globs as well. Arguably easier than with regular expressions1, actually. With glob patterns it's simply:

urlprefix-/api/{1..9}/register
urlprefix-/api/{10..20}/appinfo

maybe i do not make myself clear, globs are powerfull. but regular expressions are more flexible.

If we get a case that's not covered by globs we'll consider implementing regular expressions, but this isn't one of them as far as I can tell.


1 I don't think [10-20] does what you think it does: it's the same as [0-2] and matches exactly one of 0, 1, or 2. https://regex101.com/r/pYQIcw/1

spiderDetective commented 4 years ago

well, thanks Peter for your reply! I got it.