First SAFE app ¶
.Net Core 2, Node.js, Yarn. Make sure you’ve got it all. Then just open shell and let’s type.
Install the template ¶
dotnet new -i SAFE.Template
Create a new SAFE app ¶
dotnet new SAFE -lang 'F#' --Server suave --Fulma basic
I’ve chosen Suave as my back-end1 and used a basic Fulma template. More options described here
Now you should have the following directory structure:
├───.paket └───src ├───Client │ ├───obj │ └───public │ └───Images ├───Server │ └───obj └───Shared
The template uses Paket - a typical replacement for NuGet package manager in the world of F#. Like it or not it works just fine.
Let it run ¶
SAFE uses FAKE as its build script. Also like Paket - a typical choice for F# apps.2
It takes a while for the first time. Once it’s done it kicks off the server and a browser window navigating to
http://localhost:8080/. It displays a basic page with a counter we can increment and decrement.
Let’s modify ¶
SAFE uses webpack.js and benefits from its hot module replacement feature. It’s very cool (aka a must) for development.
Breaking the counter ¶
./src/Client/Client.fs and find the following method:
let update (msg : Msg) (model : Model) : Model * Cmd<Msg> = let model' = match model, msg with | Some x, Increment -> Some (x + 1) | Some x, Decrement -> Some (x - 1) | None, Init (Ok x) -> Some x | _ -> None model', Cmd.none
Now change it to increment 7 and decrement 3 (or whatever) and save it. In the console you should see something like the following:
i ｢wdm｣: Compiling... Parsing ./Client/Client.fs... fable: Compiled Client.fs
And then boom, go to the browser, DO NOT reload the page, and enjoy your counter being broken.
It has been a very quick overview I hope you enjoyed. I believe SAFE stack not only can boost productivity but also is a milestone in making F# a popular rather than an exotic choice.
If you want something more mainstream go for Giraffe and enjoy all the powers of ASP.NET Core. ↩︎
I like it much - we use it in the company I work for too. However, FAKE is undergoing a serious refactoring right now and the documentation is quite fragmented/incomplete. Unless you’re a researcher/fanatic I would not recommend to use it until it’s settled. ↩︎