2010-09-22 47 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