{"id":5874,"date":"2018-02-18T16:24:07","date_gmt":"2018-02-18T16:24:07","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5874"},"modified":"2018-02-18T21:27:08","modified_gmt":"2018-02-18T21:27:08","slug":"using-giraffe-as-a-service-pipeline-in-kestrel","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/using-giraffe-as-a-service-pipeline-in-kestrel\/","title":{"rendered":"Using Giraffe as a service pipeline in Kestrel"},"content":{"rendered":"<p>In the previous post we looked at using Kestrel to run an ASP.NET Core application, now we&#8217;re going to use Giraffe to build some services.<\/p>\n<p>Giraffe is an F# library to let get started by creating our application code.<\/p>\n<ul>\n<li>Create Visual F# | .NET Core | Console App<\/li>\n<li>My project is named GiraffeTest<\/li>\n<li>Add via NuGet Giraffe and Microsoft.AspNetCore<\/li>\n<\/ul>\n<p>Replace Program.fs code with the following<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nopen Giraffe\r\nopen Microsoft.Extensions.DependencyInjection\r\nopen Microsoft.AspNetCore.Builder\r\nopen Microsoft.AspNetCore.Hosting\r\nopen Microsoft.AspNetCore\r\n\r\nlet webApp =\r\n    choose &#x5B;\r\n        GET &gt;=&gt;\r\n            choose &#x5B;\r\n                routeCif &quot;\/hello\/%s&quot; (fun name -&gt; text (sprintf &quot;Hello %s&quot; name))\r\n            ]\r\n    ]\r\n\r\ntype Startup() =\r\n    member this.ConfigureServices (services : IServiceCollection) =\r\n        services.AddGiraffe() |&gt; ignore\r\n\r\n    member this.Configure (app : IApplicationBuilder) =\r\n        app.UseGiraffe webApp\r\n\r\n&#x5B;&lt;EntryPoint&gt;]\r\nlet main _ =\r\n    WebHost.CreateDefaultBuilder()\r\n        .UseKestrel()\r\n        .UseStartup&lt;Startup&gt;()\r\n        .Build()\r\n        .Run()\r\n    0\r\n<\/pre>\n<p>In the previous post we saw how to create the Kestrel server, so the code in main is exactly the same (apart from obviously being F#) which creates the server and calls the Startup class to configure our middleware. In this case we add Giraffe to the services and runs the Giraffe webApp HTTP handler.<\/p>\n<p>The webApp HTTP handler is basically our filter and routing function.<\/p>\n<p>Run this code up and navigate to localhost:5000\/hello\/World and we should get <em>Hello World<\/em> displayed.<\/p>\n<p>We didn&#8217;t actually need the GET handler as by default the GET will be used, but it&#8217;s shown here to be a little more explicit in what is being implemented. We can also support POST methods using the same syntax as our GET code.<\/p>\n<p><strong>Extending our routes<\/strong><\/p>\n<p>Giraffe allows us to declare multiple routes which can include static pages. Let&#8217;s start off my adding an index.html page to the &#8220;Content root path&#8221; as displayed when running the application, in my case this is the folder containing Program.fs. I&#8217;m using the following index.html<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;html&gt;\r\n    &lt;body&gt;\r\n        Hello World\r\n    &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>Now add a new route to the route section of the webApp. i.e.<\/p>\n<pre class=\"brush: fsharp; highlight: [3]; title: ; notranslate\" title=\"\">\r\nchoose &#x5B;\r\n    routeCif &quot;\/hello\/%s&quot; (fun name -&gt; text (sprintf &quot;Hello %s&quot; name))\r\n    route &quot;\/&quot; &gt;=&gt; htmlFile &quot;index.html&quot;\r\n]\r\n<\/pre>\n<p>This has now added a route for localhost:5000\/ which returns the contents of the index.html file.<\/p>\n<p>A list of different routes can be seen in the source for <a href=\"https:\/\/github.com\/giraffe-fsharp\/Giraffe\/blob\/master\/src\/Giraffe\/Routing.fs\" rel=\"noopener\" target=\"_blank\">Routing.fs<\/a>.<\/p>\n<p>Below is an example of using some different routes<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet helloName name = text (sprintf &quot;Hello %s&quot; name)\r\n\r\nlet webApp =\r\n    choose &#x5B;\r\n        GET &gt;=&gt;\r\n            choose &#x5B;\r\n                \/\/ case insensitive using anonymous function\r\n                routeCif &quot;\/hello\/%s&quot; (fun name -&gt; text (sprintf &quot;Hello %s&quot; name))\r\n                route &quot;\/&quot;       &gt;=&gt; htmlFile &quot;index.html&quot; \r\n                route &quot;\/error&quot;  &gt;=&gt; setStatusCode 404\r\n                route &quot;\/ping&quot;   &gt;=&gt; text &quot;pong&quot;\r\n                \/\/ case sensitive use function\r\n                routef &quot;\/hello2\/%s&quot; helloName\r\n            ]\r\n    ]\r\n<\/pre>\n<p>Return from the fish (>=>) operator can be marked as different types of result types.<br \/>\nThe functions text, htmlFile and other response writes available here <a href=\"https:\/\/github.com\/giraffe-fsharp\/Giraffe\/blob\/master\/src\/Giraffe\/ResponseWriters.fs\" rel=\"noopener\" target=\"_blank\">ResponseWriters.fs<\/a>. These include the usual suspects such as XML and JSON. Giraffe also supports Razor, see the NuGet package <a href=\"https:\/\/github.com\/giraffe-fsharp\/Giraffe.Razor\" rel=\"noopener\" target=\"_blank\">Giraffe.Razor<\/a>.<\/p>\n<p><strong>References<\/strong><\/p>\n<p><a href=\"https:\/\/github.com\/giraffe-fsharp\/Giraffe\" rel=\"noopener\" target=\"_blank\">Giraffe GitHub repos.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous post we looked at using Kestrel to run an ASP.NET Core application, now we&#8217;re going to use Giraffe to build some services. Giraffe is an F# library to let get started by creating our application code. Create Visual F# | .NET Core | Console App My project is named GiraffeTest Add via [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[200,6,201,202],"tags":[],"class_list":["post-5874","post","type-post","status-publish","format-standard","hentry","category-asp-net-core","category-f","category-giraffe","category-kestrel"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5874","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5874"}],"version-history":[{"count":10,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5874\/revisions"}],"predecessor-version":[{"id":5896,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5874\/revisions\/5896"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}