Comments (6)
Hi,
In theory this is still possible with routef
. Here's an example:
let browseHandler (path : string) =
fun (ctx : HttpContext) ->
text path ctx
let webApp =
choose [
GET >=>
choose [
route "/" >=> text "Hello world"
routef "/browse/%s" browseHandler
]
setStatusCode 404 >=> text "Not Found" ]
The path
variable in the browseHandler
is greedy enough to capture everything that is followed by /browse/
in the path.
e.g.:
http://localhost:5000/browse/dir1/dir2/file.txt
will return dir1/dir2/file.txt
and http://localhost:5000/browse/a/b/c/d/e/f/g
will return a/b/c/d/e/f/g
.
Inside the browseHandler
you can further process the path as much as you like, e.g.:
open System.Text.RegularExpressions
let browseHandler (path : string) =
fun (ctx : HttpContext) ->
let pattern = "" //Define your pattern here
let result = Regex.Match(path, pattern)
match result.Success with
| false -> async.Return None
| true ->
// Do something before returning a successful response
text path ctx
In this example I simply return None
if the path didn't match a custom pattern which means that this handler didn't match the request and another handler will try to process this request in the pipeline.
from giraffe.
Hi, if I understand your question correctly then I believe you should be able to use routef
. Please have a look and let me know if that is what you were looking for otherwise maybe you can show me a little sample in Nancy so I can fully follow what you are after :)
from giraffe.
Hi @JanDotNet did my previous answer help you or were you looking for something else?
from giraffe.
Hey dustinmoris,
thanks for the fast response. I didn't checked the actual behavior, but my understanding from the documentation is, that routef
allows to define routes of fixed length with typed values.
For example:
/browse/%s/%s
matches /browse/dir1/dir2
/browse/%s/%s/%s
matches /browse/dir1/dir2/dir3
My question was: Is it possible to define one route that matches both of the examples above and other paths with more directories?
NancyFX for example provides so called RegEx Segments for defining routes: https://github.com/NancyFx/Nancy/wiki/Defining-routes
Something like browse^(?<path>((/(\w|\d|\s)+)*)/?)$
would do it in NancyFX.
Hopefully this question is better explained than my initial one ;)
from giraffe.
If this is a recurring thing then you might as well turn this handler into a more re-usable handler which you can plug into your pipeline. For example like this:
open System.Text.RegularExpressions
let browseHandler (customRegexPattern : string) (path : string) =
fun (ctx : HttpContext) ->
let result = Regex.Match(path, customRegexPattern)
match result.Success with
| false -> async.Return None
| true -> async.Return (Some ctx)
let webApp =
choose [
GET >=>
choose [
route "/" >=> text "Hello world"
routef "/browse/%s" (browseHandler "<custon-regex>") >=> text "Regex match 1"
routef "/browse/%s" (browseHandler "<another-custon-regex>") >=> text "Regex match 2"
]
setStatusCode 404 >=> text "Not Found" ]
or after evaluating that the path is correct you can pass the path on to a new http handler which will do something with it only on a match:
open System.Text.RegularExpressions
let browseHandler (customRegexPattern : string)
(successHandler : string -> HttpHandler)
(path : string) =
fun (ctx : HttpContext) ->
let result = Regex.Match(path, customRegexPattern)
match result.Success with
| false -> async.Return None
| true -> successHandler path ctx
let doSomething (path : string) =
fun (ctx : HttpContext) ->
text path ctx
let webApp =
choose [
GET >=>
choose [
route "/" >=> text "Hello world"
routef "/browse/%s" (browseHandler "<custon-regex>" doSomething)
]
setStatusCode 404 >=> text "Not Found" ]
Giraffe is extremely flexible. You can compose http handler functions as much you like to fit your custom needs. If you need some further help please let me know!
from giraffe.
I'll close this issue, because I haven't heard back since my last answer. If there's anything else you would like to discuss feel free to re-open it.
from giraffe.
Related Issues (20)
- Migrate to System.Text.Json HOT 1
- Participate on hacktoberfest 2023? HOT 1
- Never decalre reader with `use` on `ctx.Request.Body` HOT 4
- Upgrade to .NET 8 HOT 6
- LinkGenerator doesn't work with routef HOT 1
- Giraffe 6.2 is breaks against Microsoft.IO.RecyclableMemoryStream 3.0.0 HOT 11
- Returning streams, either with WriteStreamAsync or WriteFileStreamAsync or their handler equivalents, is extremely slow HOT 4
- Question: How to approach outside in testing of a micro service? HOT 2
- Guid pattern in endpoint router matches invalid values, throws System.FormatException HOT 6
- Follow-up from "Minor code optimisation #567"
- Update SECURITY.md
- Start using fantomas to validate code submissions with CI HOT 6
- [README] Suggestion for "Getting Started"'s "Doing it manually" HOT 2
- [question] Would it be possible to make `Giraffe.EndpointRouting` case-sensitive? HOT 3
- Remove NuGet API key from the repository HOT 3
- 6.4.0 release references PR for updating to .NET 7 instead of .NET 8 HOT 1
- EndpointRouting - Create endpoint for multiple http verbs
- Request/discussion: WriteStreamAsync buffer size autotuning and/or increase default buffer size HOT 1
- Update `.vscode` debug configuration to point to existent projects
- Returning 406 when mustAccept fails HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from giraffe.