Skip to content

Commit d3caeea

Browse files
authored
Merge pull request #3 from AnswerDotAI/handle-multiport-sub-reverse-proxy
Modify add_sub_reverse_proxy to be multi-port
2 parents 51fb056 + be2e929 commit d3caeea

File tree

3 files changed

+623
-10
lines changed

3 files changed

+623
-10
lines changed

fastcaddy/core.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import os, subprocess, httpx, json
1313
from fastcore.utils import *
1414
from httpx import HTTPStatusError, get as xget, post as xpost, patch as xpatch, put as xput, delete as xdelete, head as xhead
15+
from typing import Sequence
1516

1617
# %% ../nbs/00_core.ipynb 5
1718
def get_id(path):
@@ -177,16 +178,23 @@ def add_wildcard_route(domain):
177178
add_route(route)
178179

179180
# %% ../nbs/00_core.ipynb 48
180-
def add_sub_reverse_proxy(domain, subdomain, port, host='localhost'):
181-
"Add a reverse proxy to a wildcard subdomain"
181+
def add_sub_reverse_proxy(
182+
domain,
183+
subdomain,
184+
port:str|int|Sequence[str|int], # A single port or list of ports
185+
host='localhost'
186+
):
187+
"Add a reverse proxy to a wildcard subdomain supporting multiple ports"
182188
wildcard_id = f"wildcard-{domain}"
183189
route_id = f"{subdomain}.{domain}"
190+
if isinstance(port, (int,str)): port = [port]
191+
upstreams = [{"dial": f"{host}:{p}"} for p in port]
184192
new_route = {
185193
"@id": route_id,
186194
"match": [{"host": [route_id]}],
187195
"handle": [{
188196
"handler": "reverse_proxy",
189-
"upstreams": [{"dial": f"{host}:{port}"}]
197+
"upstreams": upstreams
190198
}]
191199
}
192200
pid([new_route], f"{wildcard_id}/handle/0/routes/...")

nbs/00_core.ipynb

+15-7
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"#| export\n",
3535
"import os, subprocess, httpx, json\n",
3636
"from fastcore.utils import *\n",
37-
"from httpx import HTTPStatusError, get as xget, post as xpost, patch as xpatch, put as xput, delete as xdelete, head as xhead"
37+
"from httpx import HTTPStatusError, get as xget, post as xpost, patch as xpatch, put as xput, delete as xdelete, head as xhead\n",
38+
"from typing import Sequence"
3839
]
3940
},
4041
{
@@ -599,7 +600,7 @@
599600
"metadata": {},
600601
"outputs": [],
601602
"source": [
602-
"# add_wildcard_route('something.fast.ai')"
603+
"add_wildcard_route('something.fast.ai')"
603604
]
604605
},
605606
{
@@ -609,16 +610,23 @@
609610
"outputs": [],
610611
"source": [
611612
"#| export\n",
612-
"def add_sub_reverse_proxy(domain, subdomain, port, host='localhost'):\n",
613-
" \"Add a reverse proxy to a wildcard subdomain\"\n",
613+
"def add_sub_reverse_proxy(\n",
614+
" domain,\n",
615+
" subdomain,\n",
616+
" port:str|int|Sequence[str|int], # A single port or list of ports\n",
617+
" host='localhost'\n",
618+
" ):\n",
619+
" \"Add a reverse proxy to a wildcard subdomain supporting multiple ports\"\n",
614620
" wildcard_id = f\"wildcard-{domain}\"\n",
615621
" route_id = f\"{subdomain}.{domain}\"\n",
622+
" if isinstance(port, (int,str)): port = [port]\n",
623+
" upstreams = [{\"dial\": f\"{host}:{p}\"} for p in port]\n",
616624
" new_route = {\n",
617625
" \"@id\": route_id,\n",
618626
" \"match\": [{\"host\": [route_id]}],\n",
619627
" \"handle\": [{\n",
620628
" \"handler\": \"reverse_proxy\",\n",
621-
" \"upstreams\": [{\"dial\": f\"{host}:{port}\"}]\n",
629+
" \"upstreams\": upstreams\n",
622630
" }]\n",
623631
" }\n",
624632
" pid([new_route], f\"{wildcard_id}/handle/0/routes/...\")"
@@ -630,7 +638,7 @@
630638
"metadata": {},
631639
"outputs": [],
632640
"source": [
633-
"# add_sub_reverse_proxy('something.fast.ai', 'foo', 5001)"
641+
"add_sub_reverse_proxy('something.fast.ai', 'foo', 5001)"
634642
]
635643
},
636644
{
@@ -639,7 +647,7 @@
639647
"metadata": {},
640648
"outputs": [],
641649
"source": [
642-
"# del_id('foo.something.fast.ai')"
650+
"del_id('foo.something.fast.ai')"
643651
]
644652
},
645653
{

0 commit comments

Comments
 (0)