2010-09-22 48 views
7

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

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

回答

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 
+0

执行后我得到'Exception DispatchOperation'*'对于合同'MyContract'需要调用者。 System.ServiceModel.Dispatcher.OperationInvokerHandler.EnsureValid(System.ServiceModel.Dispatcher.DispatchOperation操作)在服务器上的:0'上显示[0x00000],在浏览器上显示以“falsetrue1310 ..”开头的文本。我在OS X上与Mono一起使用F#,所以可能会有一个错误。任何提示什么寻找? – 2010-09-22 23:00:37

+0

不是。哇,我甚至都不知道WCF在Mono上跑过。 – Brian 2010-09-22 23:09:39

+0

您应该为您的问题添加“Mono”标签 – BlackTigerX 2010-09-22 23:10:04

4

我没有研究过这在所有的,但也许看了一眼

https://github.com/SuaveIO/suave/blob/master/README.md

倜傥是一个简单的Web开发F# 库提供一个轻量级web 服务器和一组组合器 操纵路由流和任务 组成。

+0

看起来很有趣。谢谢。 – 2010-09-22 23:34:10

1

看看frack(机架一样的界面),如果你需要一个更好的语法,frank(它建立在弗兰克•顶部)。

还有Kayak,它是用C#编写的,但你可以很容易地从F#中使用它。

+0

我认为正确的链接到Kayak现在是https://github.com/kayak/kayak/至少如果你关心的源代码。 – jocull 2015-08-25 18:45:35