Serving static files, HTTP Compression and MIME types
A typical static file serving Suave application would look somewhat like this,
placed in files.fsx
and serving from ./public
relative to your script file.
#!/usr/bin/env fsharpi #r "./packages/Suave/lib/net40/Suave.dll" open System.IO open Suave open Suave.Filters open Suave.Operators let app : WebPart = choose [ GET >=> path "/" >=> Files.file "index.html" GET >=> Files.browseHome RequestErrors.NOT_FOUND "Page not found." ] let config = { defaultConfig with homeFolder = Some (Path.GetFullPath "./public") } startWebServer config app
The main file combinators are file
, browseHome
and variations of these. To
learn about all of them check out the Files module
documentation
file
will take the relative or absolute path for the file we want to serve to
the client. It will set MIME-type headers based on the file extension.
browseHome
will match existing files in the homeFolder
based on the Url
property and will serve them via the file
combinator; homeFolder
is a
configuration parameter and can be set in the configuration record.
startWebServer { defaultConfig with homeFolder = Some @"C:\MyFiles" } app
Suave supports gzip and deflate http compression encodings. Http compression is configured via the MIME types map in the server configuration record.
By default Suave does not serve files with extensions not registered in the mime types map.
The default mime types map defaultMimeTypesMap
looks like this.
let defaultMimeTypesMap = function | ".css" -> createMimeType "text/css" true | ".gif" -> createMimeType "image/gif" false | ".png" -> createMimeType "image/png" false | ".htm" | ".html" -> createMimeType "text/html" true | ".jpe" | ".jpeg" | ".jpg" -> createMimeType "image/jpeg" false | ".js" -> createMimeType "application/x-javascript" true // ... some stuff left out | _ -> None
You can register additional MIME extensions by creating a new mime map in the following fashion.
// Adds a new mime type to the default map let mimeTypes = defaultMimeTypesMap @@ (function | ".avi" -> createMimeType "video/avi" false | _ -> None) let webConfig = { defaultConfig with mimeTypesMap = mimeTypes }
from Suave
from Suave
Full name: temp.app
module WebPart
from Suave
--------------------
type WebPart = WebPart<HttpContext>
Full name: Suave.Http.WebPart
--------------------
type WebPart<'a> = 'a -> Async<'a option>
Full name: Suave.WebPart.WebPart<_>
Full name: Suave.WebPart.choose
Full name: Suave.Filters.GET
Full name: Suave.Filters.path
from Suave
Full name: Suave.Files.file
Full name: Suave.Files.browseHome
from Suave
Full name: Suave.RequestErrors.NOT_FOUND
Full name: temp.config
Full name: Suave.Web.defaultConfig
static val InvalidPathChars : char[]
static val AltDirectorySeparatorChar : char
static val DirectorySeparatorChar : char
static val PathSeparator : char
static val VolumeSeparatorChar : char
static member ChangeExtension : path:string * extension:string -> string
static member Combine : [<ParamArray>] paths:string[] -> string + 3 overloads
static member GetDirectoryName : path:string -> string
static member GetExtension : path:string -> string
static member GetFileName : path:string -> string
...
Full name: System.IO.Path
Full name: Suave.Web.startWebServer
Full name: temp.defaultMimeTypesMap
Full name: temp.mimeTypes
Full name: temp.webConfig