我正在写一个Suave应用程序,如果原始ip不在路由的授权列表中,我想停止。为此,我写了一个小过滤器:
let RemoteIp (ipList: System.Net.IPAddress List) (x: Http.HttpContext) =
if (ipList |> List.map (fun ip -> x.clientIpTrustProxy.Equals ip ) |> List.contains true)
then
async.Return (Some x)
else
async.Return None
然后我装上
Filters.path "/cache" >=> RemoteIp authorizedIps >=> Filters.GET >=> Successful.OK ""
所以我只有在来自我的授权列表中的IP时才能处理该呼叫,如果不是它只是继续。然而,我真正想做的是返回403.现在我只是短路路线搜索。
有没有像分支组合器那样的东西?
答案 0 :(得分:2)
我努力写一个分支函数:
let Branch (x:WebPart) (y:WebPart) (z:WebPart): WebPart =
fun arg -> async {
let! res = x arg
match res with
| Some v -> return! y arg
| None -> return! z arg
}
所以现在我有类似
的东西Filters.path "/cache" >=> Branch (RemoteIp authorizedIps) (Successful.OK "Yea!") (RequestErrors.FORBIDDEN "Nope")
它可能会在某个时候派上用场,但实际上,我之前应该想到的是Fyodor的建议,我认为这个建议更具可读性:
Filters.path "/cache" >=> choose [
RemoteIp authorizedIps >=> Successful.OK "Yea!"
RequestErrors.FORBIDDEN "Nope"
]