More and more .NET developers come to appreciate functional programming, and this is really good news for F#. A couple weeks ago Scott Hanselman ran a short article on running Suave.io apps as Azure web apps deployed via GitHub. His setup was the following:
- You configure your Azure web app to be sourced from a GitHub repository. This sets up a GitHub hook that notifies Azure on every commit.
- Your suave.io app consists of an F# script (
site\webserver.fsx) inside an empty ASP.NET project with a
- On a commit, Azure executes a deployment script (
.deployment) found in your project.
- This script uses Paket to restore FAKE and Suave, and uses FAKE to execute an F# script (
build.fsx) by passing it to
fsi.exeshipped with FAKE.
- This FAKE script can optionally execute various build tasks, if necessary, to build a site - in Scott's example it was just used as a placeholder script.
- Finally, the web app is copied to the right folder in your Azure web app.
- When starting, the app executes a "setup" step, configured as an
<httpPlatform>directive. This in turn uses FAKE to execute
site\webserver.fsx, which starts Suave.io and listens on a given port that is then mapped to the standard HTTP port.
This sequence is more complicated than it needs to be because it has to work around the lack of the F# tools and core libraries when setting up a new Azure web app deployment. Since Scott's article, I filed a ticket to bundle the F# compiler and tools in a Nuget, and thanks to Don Syme and Steffen Forkmann, that Nuget was out within a couple days.
Armed with that, we quickly put together a similar deployment setup for WebSharper client-server apps (other types of WebSharper apps are equally possible.) This has the obvious advantage that:
- It produces an ASP.NET app that serves fine in Azure without the need to run a separate server process like Suave.io (although you can also create OWIN-based self-hosted WebSharper applications and run them as in Scott's scenario.)
- It no longer needs FAKE to compile F# apps or run F# scripts.
Loic blogged the technical details in his WebSharper: From Zero to an Azure-deployed Web Application article, using a WebSharper implementation/clone of the "2048" game. I also put together a simpler template that you can use for client-server applications, and it's available in...
this GitHub repository
You can also get started quickly by clicking here:
There is a single
main.fs file, and it contains both the server and the client functionality:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
This uses basic WebSharper templating and a master
main.html template and implements a two page sitelet application:
You can run it directly here.