Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suave serve static files

Tags:

f#

suave

I'll like to serve all files in my 'public' folder with suave

Inside my public I have:

/index.html
/styles/main.css
/scripts/app.js
/images/*.(png|jpg)

Do I use a homeFolder? Or how does this work? Does the public folder need to be copied next to the executable in my bin folder? A code snippet would be much appreciated. Thanks.

Edit:

The solution looks like this:

open Suave
open Suave.Filters
open Suave.Operators
open Suave.Successful
open System.IO

let app = 
    choose [
        GET >=> choose
            [path "/" >=> OK "test" ; Files.browseHome]
        POST >=> choose
            [path "/foo" >=> OK "thanks"]
    ]

let myHomeFolder = Path.Combine(Directory.GetCurrentDirectory(), "public") 

let cfg = { 
    defaultConfig with
        homeFolder = Some(myHomeFolder)
    }

[<EntryPoint>]
let main argv = 
    printfn "%A" argv
    startWebServer cfg app
    System.Console.ReadLine() |> ignore
    0 // return an integer exit code
like image 812
user1613512 Avatar asked Jan 27 '16 19:01

user1613512


1 Answers

I found this quite hard to do too as it's not really documented. I didn't find the blog that Carsten linked to above so I managed to come up with this after trawling through the Suave source code and the examples in its GitHub repo:

open Suave
open Suave.Operators

let app =
    choose
        [ Filters.GET >=> choose

            [ Filters.path "/" >=> (
                  "My main page"
                  |> Successful.OK)

              Files.browseHome ] ] // <-- The important part

[<EntryPoint>]
let main x =
    Web.startWebServer Web.defaultConfig app |> ignore
    0

I used browseHome because this is a console app and I just wanted to serve files from the exe directory. I think you'll want to use browse (source code)

And here are some examples you may find useful.

like image 196
TheQuickBrownFox Avatar answered Oct 16 '22 16:10

TheQuickBrownFox