Bindings & Configuration
Configure Suave server bindings, ports, HTTPS, and server behavior.
Default Configuration
Start with the default configuration:
open Suave
open Suave.Successful
let app = OK "Hello World"
[<EntryPoint>]
let main argv =
startWebServer defaultConfig app
0
// Starts server on http://localhost:8080
Custom Port
Configure the server to listen on a specific port:
open Suave
open Suave.Successful
let config =
{ defaultConfig with
bindings = [ HttpBinding.createSimple HTTP "0.0.0.0" 3000 ] }
let app = OK "Hello on port 3000"
[<EntryPoint>]
let main argv =
startWebServer config app
0
HTTPS / TLS
Enable HTTPS with a certificate:
open Suave
open Suave.Successful
open System.Security.Cryptography.X509Certificates
let cert = new X509Certificate2("./server.pfx", "password")
let config =
{ defaultConfig with
bindings =
[ HttpBinding.createSimple (HTTPS cert) "0.0.0.0" 8443 ] }
let app = OK "Secure connection"
[<EntryPoint>]
let main argv =
startWebServer config app
0
Multiple Bindings
Listen on multiple ports and protocols:
open Suave
open Suave.Successful
let config =
{ defaultConfig with
bindings =
[ HttpBinding.createSimple HTTP "0.0.0.0" 8080
HttpBinding.createSimple HTTP "127.0.0.1" 3000 ] }
let app = OK "Listening on multiple ports"
[<EntryPoint>]
let main argv =
startWebServer config app
0
Server Settings
Configure server behavior:
open Suave
open Suave.Successful
let config =
{ defaultConfig with
bindings = [ HttpBinding.createSimple HTTP "0.0.0.0" 8080 ]
maxContentLength = 1024 * 1024 * 100 // 100 MB
listenTimeout = System.TimeSpan.FromSeconds(60.0)
cancellationToken = System.Threading.CancellationToken.None
hideHeader = false }
let app = OK "Custom server settings"
[<EntryPoint>]
let main argv =
startWebServer config app
0
Environment-Based Configuration
Configure differently based on environment:
open Suave
open Suave.Successful
open System
let getConfig () =
let isDev = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") = "Development"
let port = if isDev then 5000 else 8080
let host = if isDev then "127.0.0.1" else "0.0.0.0"
{ defaultConfig with
bindings = [ HttpBinding.createSimple HTTP host port ]
}
let app = OK "Environment-aware configuration"
[<EntryPoint>]
let main argv =
let config = getConfig()
startWebServer config app
0
Graceful Shutdown
Handle server shutdown gracefully:
open Suave
open Suave.Successful
open System.Threading
let app = OK "Graceful shutdown demo"
[<EntryPoint>]
let main argv =
let cts = new CancellationTokenSource()
// Cancel on Ctrl+C
System.Console.CancelKeyPress.Add(fun _ ->
printfn "Shutting down gracefully..."
cts.Cancel()
)
let config =
{ defaultConfig with
cancellationToken = cts.Token }
let _, shutdown = startWebServerAsync config app
Async.RunSynchronously shutdown
0
Compression Configuration
Enable gzip compression for responses:
open Suave
open Suave.Successful
open Suave.Operators
open Suave.Writers
let config =
{ defaultConfig with
bindings = [ HttpBinding.createSimple HTTP "0.0.0.0" 8080] }
let app =
setHeader "Content-Encoding" "gzip" >=>
OK "Compressed response"
[<EntryPoint>]
let main argv =
startWebServer config app
0
Custom Mime Types
Add custom MIME types:
open Suave
open Suave.Successful
open Suave.Operators
open Suave.Writers
let mimeTypes =
Writers.defaultMimeTypesMap
@@ (function
| ".webp" -> Writers.createMimeType "image/webp" false
| ".woff2" -> Writers.createMimeType "font/woff2" false
| _ -> None)
let config =
{ defaultConfig with
bindings = [ HttpBinding.createSimple HTTP "0.0.0.0" 8080 ]
mimeTypesMap = mimeTypes }
let app = OK "Custom MIME types configured"
[<EntryPoint>]
let main argv =
startWebServer config app
0