SaturnFramework / Saturn

Opinionated, web development framework for F# which implements the server-side, functional MVC pattern
https://saturnframework.org
MIT License
703 stars 108 forks source link

Stop router handler being run at compile time? #306

Closed dredgy closed 2 years ago

dredgy commented 3 years ago

I have a router set up, a la


let apiRouter = router {
        pipe_through browser //Sets a HttpContext accessible by controller functions
        not_found_handler (setStatusCode 404 >=> text "404")
        get "/" (PageController.loadHomePage())
        getf "/about/%i" (PageController.loadAboutPage)
    }

Where loadHomePage() returns a htmlString

However the function does depend on side effects (namely cookies) which is currently accessed through a mutable HttpContext that is set in the browser pipeline.

In my use case, I do not want loadHomePage to be executed at compile time, only when "/" is accessed, else it will generate an error as the HttpContext it's trying to access won't be set. Since this isn't a very functional way of doing things, I'm perfectly happy to pass a context from the router to the function but I cannot figure out how to do it.

Thank you

64J0 commented 2 years ago

Can you provide an example of what you're trying to do @dredgy?