Suave serve static files
Asked Answered
L

1

10

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
Laquitalar answered 27/1, 2016 at 19:54 Comment(3)
the doc should contain everything you asked forGeodynamics
Sorry suave.io/files.html doesn't really help that muchLaquitalar
well ok - another short query to google/bing yields this .... better?Geodynamics
P
11

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.

Perfectible answered 28/1, 2016 at 9:52 Comment(1)
Thanks that helped, turns out my homeDirectory needs to an absolute path.Laquitalar

© 2022 - 2024 — McMap. All rights reserved.