Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Working self-contained getting-started sample for Suave Web Scripting
//==========================================
// Working fully self-contained getting-started example for Suave Web Server scripting
//
// Note you don't need to have _anything_ installed before starting with this script. Nothing
// but F# Interactive and this script.
//
// This script fetches the Paket.exe component which is referenced later in the script.
// Initially the #r "paket.exe" reference is shown as unresolved. Once it has been
// downloaded by the user (by executing the first part of the script) the reference
// shows as resolved and can be used.
//
// Paket is then used to fetch a set of F# packages, which are then used later in the script.
//
//------------------------------------------
// Step 0. Boilerplate to get the paket.exe tool
open System
open System.IO
Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
if not (File.Exists "paket.exe") then
let url = "https://github.com/fsprojects/Paket/releases/download/0.26.3/paket.exe"
use wc = new Net.WebClient()
let tmp = Path.GetTempFileName()
wc.DownloadFile(url, tmp);
File.Move(tmp,Path.GetFileName url)
// Step 1. Resolve and install the packages
#r "paket.exe"
Paket.Scripting.Install """
source https://nuget.org/api/v2
nuget Suave 0.16.0
nuget FSharp.Data
nuget FSharp.Charting
""";;
// Step 2. Use the packages
#r "packages/Suave/lib/Suave.dll"
#r "packages/FSharp.Data/lib/net40/FSharp.Data.dll"
#r "packages/FSharp.Charting/lib/net40/FSharp.Charting.dll"
let ctxt = FSharp.Data.WorldBankData.GetDataContext()
let data = ctxt.Countries.Algeria.Indicators.``GDP (current US$)``
open Suave // always open suave
open Suave.Http.Successful // for OK-result
open Suave.Web // for config
web_server default_config (OK (sprintf "Hello World! In 2010 Algeria earned %f " data.[2010]))

Usage

Run fsi.exe and specify the filename of the script, so that F# interactive reads the code and executes the script in real time:

fsi gistfile1.fsx

Output:

F:>fsi gistfile1.fsx
found: F:\paket.dependencies
Resolving packages:
    - exploring Suave 0.16.0
  - fetching versions for FSharp.Charting
    - exploring FSharp.Charting 0.90.9
  - fetching versions for FSharp.Data
    - exploring FSharp.Data 2.1.1
  - fetching versions for Zlib.Portable
    - exploring Zlib.Portable 1.10.0
Locked version resolutions written to F:\paket.lock
Suave 0.16.0 unzipped to F:\packages\Suave
FSharp.Charting 0.90.9 unzipped to F:\packages\FSharp.Charting
Zlib.Portable 1.10.0 unzipped to F:\packages\Zlib.Portable
FSharp.Data 2.1.1 unzipped to F:\packages\FSharp.Data

[I] 2015-02-02T10:38:02.8732541Z: started listener in: started in 12.999000 ms:
127.0.0.1:8083 [Tcp.tcp_ip_server]

Appendix

Why not #load the script inside F# interactive?

Loading the script inside an F# interactive session can result in the following error:

# Assuming that `paket.exe` is not downloaded.

> #load "gistfile1.fsx";;
[Loading F:\gistfile1.fsx]

error FS0082: Could not resolve this reference. Could not locate the assembly
"paket.exe". Check to make sure the assembly exists on disk. If this reference
is required by your code, you may get compilation errors. (Code=MSB3245)

Awesome! Here's the same gist using Paket 0.26.3, resulting into less lines of code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment