|
568 | 568 | "outputs": [], |
569 | 569 | "source": [ |
570 | 570 | "#| export\n", |
571 | | -"def add_reverse_proxy(from_host, to_url):\n", |
| 571 | +"def add_reverse_proxy(from_host, to_url, st_delay='1m', encode:bool=True):\n", |
572 | 572 | "\"Create a reverse proxy handler\"\n", |
573 | 573 | " if has_id(from_host): del_id(from_host)\n", |
| 574 | +" res = []\n", |
| 575 | +" if encode: res.append({\"handler\":\"encode\",\n", |
| 576 | +"\"encodings\": {\"gzip\":{},\"zstd\":{}},\n", |
| 577 | +"\"prefer\": [\"zstd\",\"gzip\"]})\n", |
| 578 | +" proxy = {\"handler\":\"reverse_proxy\",\n", |
| 579 | +"\"upstreams\": [{\"dial\": to_url}]}\n", |
| 580 | +" if st_delay: proxy[\"stream_close_delay\"] = st_delay\n", |
| 581 | +" res.append(proxy)\n", |
574 | 582 | " route = {\n", |
575 | | -"\"handle\": [{\"handler\":\"reverse_proxy\",\n", |
576 | | -"\"upstreams\": [{\"dial\": to_url}] }],\n", |
| 583 | +"\"handle\": res,\n", |
577 | 584 | "\"match\": [{\"host\": [from_host]}],\n", |
578 | 585 | "\"@id\": from_host,\n", |
579 | 586 | "\"terminal\": True\n", |
|
648 | 655 | " domain,\n", |
649 | 656 | " subdomain,\n", |
650 | 657 | " port:str|int|Sequence, # A single port or list of ports\n", |
651 | | -" host='localhost'\n", |
| 658 | +" host='localhost',\n", |
| 659 | +" st_delay='1m',\n", |
| 660 | +" encode:bool=True\n", |
652 | 661 | " ):\n", |
653 | 662 | "\"Add a reverse proxy to a wildcard subdomain supporting multiple ports\"\n", |
654 | 663 | " wildcard_id = f\"wildcard-{domain}\"\n", |
655 | 664 | " route_id = f\"{subdomain}.{domain}\"\n", |
656 | 665 | " if isinstance(port, (int,str)): port = [port]\n", |
657 | 666 | " upstreams = [{\"dial\": f\"{host}:{p}\"} for p in port]\n", |
| 667 | +" res = []\n", |
| 668 | +" if encode: res.append({\"handler\":\"encode\",\n", |
| 669 | +"\"encodings\": {\"gzip\":{},\"zstd\":{}},\n", |
| 670 | +"\"prefer\": [\"zstd\",\"gzip\"]})\n", |
| 671 | +" proxy = {\"handler\":\"reverse_proxy\",\"upstreams\": upstreams}\n", |
| 672 | +" if st_delay: proxy[\"stream_close_delay\"] = st_delay\n", |
| 673 | +" res.append(proxy)\n", |
658 | 674 | " new_route = {\n", |
659 | 675 | "\"@id\": route_id,\n", |
660 | 676 | "\"match\": [{\"host\": [route_id]}],\n", |
661 | | -"\"handle\":[{\"handler\":\"reverse_proxy\",\"upstreams\": upstreams }]\n", |
| 677 | +"\"handle\":res\n", |
662 | 678 | " }\n", |
663 | 679 | " pid([new_route], f\"{wildcard_id}/handle/0/routes/...\")" |
664 | 680 | ] |
|