=> OK "Hel-6ren">
gpt4 book ai didi

f# - WebPart 用于使用 Suave 将请求转发到另一台服务器?

转载 作者:行者123 更新时间:2023-12-04 13:33:21 25 4
gpt4 key购买 nike

我想创建一个 WebPart将所有请求转发到我指定的另一个 Web 服务器。
用法可能如下所示:

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

startWebServer defaultConfig app
我应该如何在 Suave 中实现它?

我找到了 this snippet ,但它似乎适用于旧版本的 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

最佳答案

这现在已添加到 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

关于f# - WebPart 用于使用 Suave 将请求转发到另一台服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63688690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com