Vapor 3: Route with arbitrary path segments

Viewed 348

I'm working on a Vapor 3 app that uses the requesting path as the argument for the method that responds. For instance:

http://localhost:8080/monitor/some/test/path

That can be handled by a single endpoint, the monitor endpoint in this case, which would then ideally give me access to the rest of the URL, /some/test/path.

I've composed a route in Vapor that doesn't work using wildcard variables, and tried the group mechanic as described in the docs, to no avail. My expectation from using other web frameworks is that something like "/monitor/*" should do the job but I can't find anything in the documentation to suggest it.

Any assistance would be appreciated!

1 Answers

If you want to match any single path parameter, use the any path component. If you want to match any number of path parameters, use the all path component.

For example, if I register a route with these path components:

"service", any, "api", "users"

It will match these routes:

  • /service/v1/api/users
  • /service/caleb/api/users

But it won't match these:

  • service/v1/beta/api/users
  • service/v1

However, if you use the all component, like this:

"service", "api", all

You can match any of the following:

  • /service/api/v1
  • /service/api/v1/users/caleb

But not /service/api.

Related