linuxf#.net-coresuave

Suave with netcoreapp2.0


I've been following this Suave tutorial: https://legacy.gitbook.com/book/theimowski/suave-music-store/details

And in general this looks good. However, I was trying to make it work with Linux and for some reason I was unable to compile it with code when TargetFramework was set to "net461" (Target Framework not found), so I tried changing it to "netcoreapp2.0". It compiled, but I hit a problem later on: https://theimowski.gitbooks.io/suave-music-store/content/en/css.html

At the end it says to add a WebPart: pathRegex "(.*)\.(css|png)" >=> Files.browseHome

which fails for me. All compiles, but I get

This page isn’t working localhost didn’t send any data.

I took a look at Suave's source code and it turned out that "Files.browseHome" searches for a file under "ctx.runtime.homeDirectory".

I noticed that this is set on my machine to: /home/<my_user_name>/.nuget/packages/suave/2.2.1/lib/netstandard1.6 and obviously, that's not my project directory, so no wonder it couldn't find the file.

My question here is: what shall I do in order to make my Suave app handle my css/png files correctly using Files.browseHome ?

EDIT: Just found out that replacing the WebPart with: pathRegex "(.*)\.(css|png)" >=> Files.browse "/home/<my_username>/<path_to_my_project>/bin/Debug/netcoreapp2.0/" works fine, but it looks ugly. Any idea how to make it better ? Basically I don't want to be forced to hardcode the absolute path anywhere.


Solution

  • I think the answer is to be found in https://suave.io/files.html, which suggests creating a config as follows:

    let config =
      { defaultConfig with homeFolder = Some (Path.GetFullPath "./public") }
    

    The path returned by Path.GetFullPath will depend on the current working directory that your app sees when it's started (which will, in turn, depend on how you start the app: from the command line, from a systemd unit file, etc.) There are too many possible variables here for me to be able to give you exact instructions, but if your startup method can cd into an appropriate folder before starting your Suave server, then that should solve your problem.

    If you run into difficulties with getting the current working directory set correctly, then you could just hardcode the full path in the config:

    let config =
      { defaultConfig with homeFolder = Some "/home/<your_username>/<path_to_your_project>/bin/Debug/netcoreapp2.0/" }
    

    But that's kind of ugly, and not really portable (you'd have to modify that path again once you deploy the app). So I'd recommend the approach of making sure your start script does a cd to the right location, then using Path.GetFullPath with a relative path. Alternately, you could have a config file that gets read in at app startup where you specify the home path, or pass it in as an environment variable... All kinds of possibilities. Just make sure that your code can be handed some information on startup that specifies the correct "home" folder, and then put that in your Suave config as I've shown, and that should solve it.