Skip to content

Instantly share code, notes, and snippets.

@ImaginaryDevelopment
Last active August 13, 2016 18:27
Show Gist options
  • Save ImaginaryDevelopment/c06445f1cf83c502822248591d4e31f8 to your computer and use it in GitHub Desktop.
Save ImaginaryDevelopment/c06445f1cf83c502822248591d4e31f8 to your computer and use it in GitHub Desktop.
Suave works
// Suave is working!
// Learn more about F# at http://fsharp.org
// See the 'F# Tutorial' project for more help.
open System
open System.IO
open Suave
open Suave.Http
open Suave.Http.Applicatives
open Suave.Http.Successful
open Suave.Http.Files
open Suave.Types
open Suave.Web
let errorHandler ex msg =
printfn "error! %s:%A" msg ex
OK (sprintf "%s:%A" msg ex)
let print title (x) = // : Async<HttpContext option>=
printfn "%s" title
x
let printDiag (x:HttpContext) =
printfn "{Url=%O;HomeDirectory=%s}" x.request.url x.runtime.homeDirectory
x
let projectRoot = __SOURCE_DIRECTORY__
let aHref (l,title) = sprintf "<a href=\"%s\">%s</a>" l title
let ul items =
items
|> Seq.map(sprintf "<li>%s</li>")
|> Array.ofSeq
|> fun x -> String.Join("\r\n ", x)
let projRelativePath p = (Path.Combine(projectRoot,p))
let html = Writers.setMimeType "text/html"
let dynamicPage =
let htm =
[ "/","Home"
"/hello","Hello"
"/goodbye","Goodbye"
"/browseHome","Browse Home"
"/dirHome","DirHome"
]
|> Seq.map aHref
|> ul
|> sprintf
"""<html>
<head>
</head>
<body>
<ul>
%s
</ul>
<a href="/">Home</a>
<a href="/hello">Hello</a>
<a href="/goodbye">GoodBye</a>
<a href="/browseHome">BrowseHome</a>
<a href="/dirHome">DirHome</a>
</body>
"""
OK htm
let app =
printDiag >> choose
[ GET >>= choose
[ path "/hello" >>= OK "Hello GET"
path "/goodbye" >>= OK "Good bye GET"
//pathScan "/%s" (fun filename -> file (sprintf "./%s" filename))
path "/" >>= html >> (print "Using /") >>= file (projRelativePath "Homepage.htm")
// doesn't work
path "browseHome" >> (print "Using /browseHome") >>= browseHome
// doesn't work
path "/dirHome" >>= dirHome
path "/index" >>= dynamicPage
path "/index" >> (print "Using /index")
>>= html
>>= file "../Homepage.htm"
(fun x -> printfn "fellthrough %A" x; x) >> pathScan "/%s" (sprintf "./%s" >> file)
]
POST >>= choose
[ path "/hello" >>= OK "Hello POST"
path "/goodbye" >>= OK "Good bye POST" ] ]
[<EntryPoint>]
let main argv =
printfn "%A" argv
startWebServer {defaultConfig with errorHandler=errorHandler} app
0 // return an integer exit code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment