Switching from PHP to F# (using Rider) for Web Dev

Hi all! I’ve admired F# from afar for a long while and my latest project involved a lot of very nice, very functional Javascript and and I figure for a next project, I would attempt to use F# for it.

My usual development stack is PHP, MySQL/Postgres on the server and the usual HTML/JS/CSS on the front. I’m looking to make a swap from PHP to F#, whilst keeping everything in tact.

I downloaded Rider (use PHPStorm usually so I figure less of a learning curve, but I have Visual Studio installed too). I have used .NET before (C#) to develop Windows Form Apps, so it’s been a while. I’m not married to .NET either, but I want to spend as little time setting up as possible.

So I simply loaded up Rider’s default web app template and…I’m lost. I need a bit of a baby’s first step guide to this.

Here’s the code in startup.fs:

namespace TestWeb

open System
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Http
open Microsoft.Extensions.DependencyInjection
open Microsoft.Extensions.Hosting
open System
open System.IO

type Startup() =

    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    member _.ConfigureServices(services: IServiceCollection) =

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    member _.Configure(app: IApplicationBuilder, env: IWebHostEnvironment) =
        if env.IsDevelopment() then
            app.UseDeveloperExceptionPage() |> ignore

        let content = "Hello World"
           .UseEndpoints(fun endpoints ->
                endpoints.MapGet("/", fun context ->
                    context.Response.WriteAsync(content)) |> ignore
            ) |> ignore

So I can see what the routing, but how do I set the content to be HTML. I usually use my own templating system in PHP, which opens a html file, parses it for variables etc and then echos the result. How do I approach that in .NET web development? I did try a ReadAllLines from a file and got nowhere, as there were type conflicts even when casting as a string.

What’s the best way to structure my filesystem? How do I include other F# files

Is there a resource for ground up Web Applications in F#? Looking to start with basic templating, SQL connection and maybe some XML manipulation.

Thank you for any help you can provide!

I’d suggest 2 things

  1. Learn the language in a simpler context, just a simple console app. There are plenty of guides for this and as an experienced programmer I suspect this will be fairly quick for you.
  2. Check out the safe stack for web development in F#. It gives you a working project that shows back end and front end both in f# using some tools that make things really easy. If you just want the server side stuff, you can focus on that and implement the front end in something else.

Hi thanks for the response.

I unfortunately can’t learn much from tutorial exercises unless I have a practical project I want to complete, of which I have several, is a big barrier for entry for me. And I’m not particularly worried about the language itself, it seems fairly intuitive to me, I wrote a few hundred lines on the Fable REPL the other day and loved it.

It’s dealing with the large frameworks, server setup,projects and environments I’m struggling with (and have no interest in) -all the command line tools, package managers, dependency mangers etc and auto generated code . I am someone who very much likes plug and play on the environment but to write all the code myself and not touch frameworks except my own. I am willing to spend a weekend to get an F# project that I can work on set up though.

I have downloaded the SAFE stack and got it working pretty painlessly and it seems a little faster than the ASP.NET project but the example project still contains just a bunch of files and stuff that I’m not entirely sure what they all do, or why they’re structured in the way they are. Are there any tutorials on how to structure a document and a website with SAFE from the ground up.

From the SAFE stack, I think I just really want the S, and probably the F (I really like F# as a client JavaScript alternative though wasn’t in initial plans). Definitely don’t want Azure, and I don’t want server side code generating HTML for me, I’d much rather write my own (which is what the E seems to do).

Will keep playing around over this weekend if I have the time and next weekend if I don’t. Cheers.

Have you considered looking into the SAFE Dojo? GitHub - CompositionalIT/SAFE-Dojo: An introductory dojo to learn how to develop full stack web applications in F#

Beyond that, you’d have to say specifically which files are making you wonder how it all works. Some of it is going to be specific to .NET, some of it will be ASP.NET Core configuration.

Thanks! I did look at the Dojo but didn’t install it. My problem isn’t the language as I’ve being doing the online Fable REPL and had no problems figuring out what to actually type, but when it comes to setting it up on my machine I have not currently got anything that I can play around with.

I followed:

And whilst I was able to generate the template files and successfully build them, I couldn’t actually run them (localhost just timed out when I tried to access) and then when I went to create a new one in a new directory, dotnet restore or fake just didn’t work (cannot find manifest file, fake doesn’t exist).

I don’t like having to troubleshoot to such a high degree before I’ve even started coding as it doesn’t bode well. I have done a few tutorials in console apps in Visual Studio but while they’re moderately useful in syntax learning, they don’t teach too much about file structure (and they seem to be very differently laid out to the web templates I’ve got anyway. So they’re not really helping me build what I want).

I need a resource that helps me step by step build a website from the ground up (from an empty project) using probably just Saturn - I want to load a HTML file, parse it to add some variables and then spit it out. Nothing crazy. Since I am now moderately experienced in Fable I would also be open to manually compiling F# to client-side Javascript and using that on a PHP backend if such a tool exists. I love the F# language but I really can’t wrap my head around the tooling and templates for some reason (probably just a very different way from how I usually approach web dev).

I’m a bit more time poor than I thought I would be this weekend, so have only be able to be at the computer for about an hour at a time, but have not been able to find any true “ground up” tutorials that explain what files need to be where, what needs to be configured exactly. All the ones I’ve looked at have given templates with problems to fix - I already have my own problems I want to fix!

The ASP.NET project template I referenced in my first post does build and run, so I am very happy to understand how that should be laid out and how I should parse HTML files in it.

I do appreciate the responses, and apologize for what seems like vague rambling. I just really wanted to sit down and code in a cool language this weekend, not deal with configuration and server stuff that I don’t understand. I hope I can manage to figure it out!

Ok! Got the SAFE test app working, and opening the .sln file in rider this time gave me a very different file structure than last time. I have a working text HTML application that can take form inputs and manipulate the DOM. Turns out I prefer F# as an alternative to JS than to PHP, though will hopefully get to try the server side stuff soon!

Thanks for all the pointers, look forward to writing some big things! EDIT: Though as I tabbed away from the program to write this, it simply stopped working and won’t recompile giving no errors, so it might just be too unreliable for me to be bothered.