4

我想创建一个WebPart将所有请求转发到我指定的另一个 Web 服务器。

用法可能如下所示:

let app = 
  choose
    [
      path "/" >=> OK "Hello, world. "
      path "/graphql" >=> createProxy "localhost:5000"
      RequestErrors.NOT_FOUND "Not found"
    ]

startWebServer defaultConfig app

我应该如何在 Suave 中实现这一点?


我找到了这个片段,但它似乎适用于旧版本的 Suave:

let build_proxy_resolver (fwd_to_host : String) fwd_to_port = 
  let heserver = System.Net.Dns.GetHostEntry(fwd_to_host)
  let ipaddr = heserver.AddressList.[0]
  fun (request : HttpRequest) ->
    Some (ipaddr, fwd_to_port)

let build_headers ctx =
  //add and remove headers from the ctx, return the header list
  ctx.request.headers

let proxy_app (ctx:HttpContext) = 
  let new_headers = build_headers ctx
  let fwd_ctx = {ctx with request={ctx.request with headers=new_headers}}
  let pxy = proxy (build_proxy_resolver "PROXY_TO.com" 80us) fwd_ctx
  {ctx with response = { ctx.response with status=Suave.Types.Codes.HTTP_200; content=SocketTask pxy }} |> Some
4

1 回答 1

1

现在已将其添加到 Suave:

open System
open Suave
open Suave.Proxy

let app = 
  choose
    [
      path "/" >=> OK "Hello, world. "
      path "/graphql" >=> proxy (Uri "http://localhost:5000")
      RequestErrors.NOT_FOUND "Not found"
    ]

startWebServer defaultConfig app
于 2020-09-13T14:45:44.817 回答