Comments (5)
Remember, an HttpHandler looks like this:
HttpContext -> Async<HttpContext option>
So noParameterFunction()
is executed, logs "Hello" and returns an HttpHandler that will print "Hello" when it is executed
Try this
let noParameterFunction ctx =
async {
printfn "Hello"
return! text "Hello" ctx
}
from giraffe.
@JonCanning is spot on
It can be very useful though for when you want to pre-compile data before returning the handler as the code body is only executed once at startup and does not need to be run each time.
If you remove currying, it is easy to see why the body only runs once as per below which is the same as yours only wrapping a ctx function:
let noParameterFunction () =
printfn "Hello"
/// val text : string -> HttpContext -> Async<HttpContext option>
(fun ctx -> text "Hello" ctx)
although Jon's example is fine, you can just as easily avoid async wrap with
let noParameterFunction ctx =
printfn "Hello"
text "Hello" ctx
As there is no async binding other than the return, which is itself producing an async result. You only need to use async binding when your handler is itself doing some waiting, otherwise just return the async of the internal handler being used.
from giraffe.
Thank you for the explanation and workarounds. I am still not clear on why the code body only runs once. Is it because the HttpHandler is returning an Async result so all non async code is only evaluated once? I look at the curry-less version and I don't see why it would print "Hello" each time the function is called.
from giraffe.
This might help explain.
Every HttpHandler is a function, it takes in a HttpContext
and returns Async<HttpContext option>
, I find that a nice way to write all handlers, regardless of multiple inputs are
let functionValue =
fun (ctx : HttpContext) -> // route execution runs from here
printfn "Hello"
text "Hello" ctx
let myText (txt:string) =
fun (ctx: HttpContext) ->
printfn txt
text txt ctx
let preCompiled () =
// only evaluted once when funtion called
let serverStart = DateTime.Now.ToString()
// now return a HttpHandler Function that contains reference to our value evaluated on startup
fun ctx -> text serverStart ctx // route execution runs from here
The value webApp
that contains all the routes and compositions is fully evaluated into a tree of HttpHandler functions that bind together, therefore expressions in your pipeline will evaluate till they return HttpHandlers to bind & Compose to the rest of the pipeline. It is similar to using the >>
operator, composing two normal functions together into a pipeline, if you did fn1 = fn2 () >> fn3
, provided the result of fn2 ()
is a function of the same type for the input of fn3
, we can compose together into one fn1
function, but fn2
is never run again after initial execution as it has returned its function result.
from giraffe.
I got it now. The functions are evaluated so that they can be passed on to form the HttpHandler function. Thank you.
from giraffe.
Related Issues (20)
- Sample `EndpointRoutingApp` structure is weird HOT 1
- NullReferenceException in requiresAuthentication funciton HOT 1
- Run tests on CI HOT 2
- Add output caching middleware after migrating to .NET 7 HOT 2
- Add output cache for `VaryByValue`
- 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 3
- 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
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.