Is there a web server library for F#, similar to SimpleHTTPServer in Python?
Installing a full server like IIS is overkill for what I want, which is a simple application that can be queried using a web browser, effectively using HTTP as a monitoring method. Ideally, a request to the address /engines/id/state
would map to a function get_state(engine_id)
which I provide.
A self-hosted WCF service is not a bad start; here's a tiny one for starters:
open System
open System.IO
// add reference to these two guys, need .NET full (not client profile)
open System.ServiceModel
open System.ServiceModel.Web
[<ServiceContract>]
type MyContract() =
[<OperationContract>]
[<WebGet(UriTemplate="{s}/{t}")>]
member this.Get(s:string, t:string) : Stream =
let html = sprintf @"
<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">
<html><head></head><body>Called with '%s' and '%s'</body></html>" s t
upcast new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html))
let Main() =
let address = "http://localhost:64385/"
let host = new WebServiceHost(typeof<MyContract>, new Uri(address))
host.AddServiceEndpoint(typeof<MyContract>, new WebHttpBinding(), "")
|> ignore
host.Open()
printfn "Server running at %s" address
printfn "Press a key to close server"
System.Console.ReadKey() |> ignore
host.Close()
Main()
// now go hit
// http://localhost:64385/foo/42
// in your browser
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With