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 }
namespace System
namespace System.IO
namespace Suave
module Filters

from Suave
module Operators

from Suave
val app : WebPart

Full name: temp.app
Multiple items
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<_>
val choose : options:WebPart<'a> list -> WebPart<'a>

Full name: Suave.WebPart.choose
val GET : WebPart

Full name: Suave.Filters.GET
val path : pathAfterDomain:string -> WebPart

Full name: Suave.Filters.path
module Files

from Suave
val file : fileName:string -> WebPart

Full name: Suave.Files.file
val browseHome : WebPart

Full name: Suave.Files.browseHome
module RequestErrors

from Suave
val NOT_FOUND : body:string -> WebPart

Full name: Suave.RequestErrors.NOT_FOUND
val config : SuaveConfig

Full name: temp.config
val defaultConfig : SuaveConfig

Full name: Suave.Web.defaultConfig
union case Option.Some: Value: 'T -> Option<'T>
type Path =
  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
Path.GetFullPath(path: string) : string
val startWebServer : config:SuaveConfig -> webpart:WebPart -> unit

Full name: Suave.Web.startWebServer
val defaultMimeTypesMap : _arg1:string -> 'a option

Full name: temp.defaultMimeTypesMap
union case Option.None: Option<'T>
val mimeTypes : (string -> MimeType option)

Full name: temp.mimeTypes
val webConfig : SuaveConfig

Full name: temp.webConfig

results matching ""

    No results matching ""