A Functional Web with ASP.NET Core and F#'s Giraffe
I was watching Ody Mbegbu's YouTube Channel - it's filled with .NET Core and ASP.NET Tutorial Videos - and was checking out one in particular, "Getting Started with ASP.NET Core Giraffe." Dane Vinson pointed me to it.
There is such a great open source renaissance happening right now with new framework's and libraries popping up in the .NET Core space. I hope you check them out AND support the creators by getting involved, writing docs, filing (kind) issues, and even doing pull requests and fixing bugs or writing tests.
Ody's video was about Dustin Morris' "Giraffe" web framework. Dustin's description is "A native functional ASP.NET Core web framework for F# developers." You can check it out over at https://github.com/dustinmoris/Giraffe.
Even better, it uses the "dotnet new" templating system so you can check it out and get started in seconds.
c:> md \mygiraffeeapp & cd \mygiraffeeapp
c:\mygiraffeeapp> dotnet new -i "giraffe-template::*"
c:\mygiraffeeapp> dotnet new giraffe
The template "Giraffe Web App" was created successfully.
c:\mygiraffeeapp> dotnet run
Hosting environment: Production
Content root path: C:\mygiraffeapp
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
Boom. Now I'm checking out Giraffe's "Hello World."
Because ASP.NET Core is very modular and built on "middleware" pipelines, that means that other frameworks like Giraffe can use the bits they want and remove the bits they down. Remembering that this is F#, not C#, here you can see Giraffe adding itself to the pipeline while still using the StaticFileMiddleware.
let configureApp (app : IApplicationBuilder) =
app.UseGiraffeErrorHandler errorHandler
app.UseStaticFiles() |> ignore
app.UseGiraffe webApp
The initial readme.md for Giraffe is the docs for now, and frankly, they are excellent and easy to read. The author says:
It is not designed to be a competing web product which can be run standalone like NancyFx or Suave, but rather a lean micro framework which aims to complement ASP.NET Core where it comes short for functional developers. The fundamental idea is to build on top of the strong foundation of ASP.NET Core and re-use existing ASP.NET Core building blocks so F# developers can benefit from both worlds.
Here is a smaller Hello World. Note the use of choose and the clear and terse nature of F#:
open Giraffe.HttpHandlers
open Giraffe.Middleware
let webApp =
choose [
route "/ping" >=> text "pong"
route "/" >=> htmlFile "/pages/index.html" ]
type Startup() =
member __.Configure (app : IApplicationBuilder)
(env : IHostingEnvironment)
(loggerFactory : ILoggerFactory) =
app.UseGiraffe webApp
Is terse an insult? Absolutely not, it's a feature! Check out this single line exampe...and the fish >=> operator! Some people don't like it but I think it's clever.
let app = route "/" >=> setStatusCode 200 >=> text "Hello World"
Making more complex:
let app =
choose [
GET >=> route "/foo" >=> text "GET Foo"
POST >=> route "/foo" >=> text "POST Foo"
route "/bar" >=> text "Always Bar"
]
Or requiring certain headers:
let app =
mustAccept [ "text/plain"; "application/json" ] >=>
choose [
route "/foo" >=> text "Foo"
route "/bar" >=> json "Bar"
]
And you can continue to use Razor views as you like, passing in models written in F#
open Giraffe.Razor.HttpHandlers
let model = { WelcomeText = "Hello World" }
let app =
choose [
// Assuming there is a view called "Index.cshtml"
route "/" >=> razorHtmlView "Index" model
]
There are samples at https://github.com/dustinmoris/Giraffe/tree/master/samples you can check out as well
* Giraffe photo by Kurt Thomas Hunt, used under CC
Sponsor: A third of teams don’t version control their database. Connect your database to your version control system with SQL Source Control and find out who made changes, what they did, and why. Learn more!
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
I wonder if fira code has a ligature for the fish operator....
You do a pull to get something out the Git repo right? You push back into the Git repo so others can later pull your changes right? So why isn't this called a "Push Request", i.e. a request for the Git repo maintainers to accept my push of new work?
Many thanks to you, Damian, David and the rest of the team for making ASP.NET Core such an amazing web stack!
"Pull request" makes sense when you look from the perspective of the repo owner. Some kind contributor has forked your repo and made some improvement, and now requests that you pull his changes into the master branch. Only approved collaborators are allowed to push changes to your repo.
You say reneissance, I say mess ;-)
Seriously, it's quite exhausting trying to keep up with this stuff. Seems like every two days, there's a new way of doing... well, the same thing. Websites. Most of which take some data from a user through a form, and present it as html. But it has to be created using something new.
I don't know. Maybe I'm just too old for this stuff, but sometimes I find it hard to see the reason behind all this stuff. I accept the fact that I may just be boring! :-D
You are correct! So to fix that:
"There is such a great open source MESS happening right now with new framework's and libraries popping up..." :D
... and I like it, as I feel that this mess or "manure" is inevitably letting some new brilliant technologies to grow into beautiful and reliable systems.
Do we have to keep up with all stuff? No.
Will all survive? No
But we can wait 4 years and look back what is grown up and read to be consumed. Someone will blog about what worked and what not.
And we can go out and pick some later on. Or we can stay with good old WIN32/C++/COM :D
Comments are closed.