GithubHelp home page GithubHelp logo

Comments (6)

dustinmoris avatar dustinmoris commented on July 23, 2024 1

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.

dustinmoris avatar dustinmoris commented on July 23, 2024

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.

dustinmoris avatar dustinmoris commented on July 23, 2024

Hi @JanDotNet did my previous answer help you or were you looking for something else?

from giraffe.

JanDotNet avatar JanDotNet commented on July 23, 2024

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.

dustinmoris avatar dustinmoris commented on July 23, 2024

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.

dustinmoris avatar dustinmoris commented on July 23, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.