F#Web服务器库

Muh*_*uri 8 f# http

是否有F#的Web服务器库,类似于Python中的SimpleHTTPServer?

安装像IIS这样的完整服务器对于我想要的东西来说太过分了,这是一个可以使用Web浏览器查询的简单应用程序,有效地使用HTTP作为监视方法.理想情况下,对地址的请求/engines/id/state将映射到get_state(engine_id)我提供的功能.

Bri*_*ian 12

自托管WCF服务并不是一个糟糕的开始; 这对于初学者来说是一个小小的:

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
Run Code Online (Sandbox Code Playgroud)