from fastcaddy.core import *
from fastcore.test import *
Test add_sub_reverse_proxy
import json
def print_json(j): print(json.dumps(j))
pcfg({})
= 'DUMMY_TOKEN' cf_token
setup_caddy(cf_token)
At this point our Caddy config is:
print(gcfg())
{ 'apps': { 'http': {'servers': {'srv0': {'listen': [':80', ':443'], 'routes': []}}}, 'tls': { 'automation': { 'policies': [{'issuers': [{'challenges': {'dns': {'provider': {'api_token': 'DUMMY_TOKEN', 'name': 'cloudflare'}}}, 'module': 'acme'}]}] } } } }
We can confirm that by going to http://localhost:2019/config/
Setup: add wildcard route *.something.example.com.
This is needed in order to add subroutes to it.
'something.example.com') add_wildcard_route(
Now our config should include the wildcard route:
print(gcfg())
{ 'apps': { 'http': { 'servers': { 'srv0': { 'listen': [':80', ':443'], 'routes': [{'@id': 'wildcard-something.example.com', 'handle': [{'handler': 'subroute', 'routes': []}], 'match': [{'host': ['*.something.example.com']}], 'terminal': True}] } } }, 'tls': { 'automation': { 'policies': [{'issuers': [{'challenges': {'dns': {'provider': {'api_token': 'DUMMY_TOKEN', 'name': 'cloudflare'}}}, 'module': 'acme'}]}] } } } }
At this point there are no subroutes associated with that *.something.example.com
wildcard. When that is matched, the handlers list is empty until…
Add subroute 1: foo.something.example.com
'something.example.com', 'foo', 5001) add_sub_reverse_proxy(
print(gcfg('/apps/http/servers/srv0/routes/0/handle'))
[{'handler': 'subroute', 'routes': [{'@id': 'foo.something.example.com', 'handle': [{'handler': 'reverse_proxy', 'upstreams': [{'dial': 'localhost:5001'}]}], 'match': [{'host': ['foo.something.example.com']}]}]}]
Now we can see the handle
config with the list of sub-routes and the sub-route handler.
print(gcfg('/apps/http/servers/srv0/routes/0/handle/0/routes'))
[{'@id': 'foo.something.example.com', 'handle': [{'handler': 'reverse_proxy', 'upstreams': [{'dial': 'localhost:5001'}]}], 'match': [{'host': ['foo.something.example.com']}]}]
We can see that:
- A route with id
foo.something.example.com
was created.
'/apps/http/servers/srv0/routes/0/handle/0/routes/0/@id'), 'foo.something.example.com') test_eq(gcfg(
- It matches requests for
foo.something.example.com
'apps/http/servers/srv0/routes/0/handle/0/routes/0/match/0/host/0'), 'foo.something.example.com') test_eq(gcfg(
- When a request for
foo.something.example.com
is matched, its handler is a reverse proxy tolocalhost:5001
.
'apps/http/servers/srv0/routes/0/handle/0/routes/0/handle/0/upstreams/0/dial'), 'localhost:5001') test_eq(gcfg(
Add subroute 2: bar.something.example.com
'something.example.com', 'bar', 5002) add_sub_reverse_proxy(
print(gcfg('/apps/http/servers/srv0/routes/0/handle'))
[{'handler': 'subroute', 'routes': [{'@id': 'foo.something.example.com', 'handle': [{'handler': 'reverse_proxy', 'upstreams': [{'dial': 'localhost:5001'}]}], 'match': [{'host': ['foo.something.example.com']}]}, {'@id': 'bar.something.example.com', 'handle': [{'handler': 'reverse_proxy', 'upstreams': [{'dial': 'localhost:5002'}]}], 'match': [{'host': ['bar.something.example.com']}]}]}]
Now we see a single sub-route handler containing two routes.
print(gcfg('/apps/http/servers/srv0/routes/0/handle/0/routes'))
[{'@id': 'foo.something.example.com', 'handle': [{'handler': 'reverse_proxy', 'upstreams': [{'dial': 'localhost:5001'}]}], 'match': [{'host': ['foo.something.example.com']}]}, {'@id': 'bar.something.example.com', 'handle': [{'handler': 'reverse_proxy', 'upstreams': [{'dial': 'localhost:5002'}]}], 'match': [{'host': ['bar.something.example.com']}]}]
Here we see that:
- The
foo.something.example.com
subroute is still present.
'/apps/http/servers/srv0/routes/0/handle/0/handler'), 'subroute') test_eq(gcfg(
- A route with id
bar.something.example.com
was created.
'/apps/http/servers/srv0/routes/0/handle/0/routes/1/@id'), 'bar.something.example.com') test_eq(gcfg(
- It matches requests for
bar.something.example.com
'/apps/http/servers/srv0/routes/0/handle/0/routes/1/match/0/host/0'),'bar.something.example.com') test_eq(gcfg(
- When a request for
bar.something.example.com
is matched, its handler is a reverse proxy tolocalhost:5002
.
'/apps/http/servers/srv0/routes/0/handle/0/routes/1/handle/0/upstreams/0/dial'), 'localhost:5002') test_eq(gcfg(
Add multi-port subroute
'something.example.com', 'multiport', [5003, 5004]) add_sub_reverse_proxy(
print(gcfg('/apps/http/servers/srv0/routes/0/handle/0/routes/2/handle/0/upstreams'))
[{'dial': 'localhost:5003'}, {'dial': 'localhost:5004'}]
'dial': 'localhost:5003'}, {'dial': 'localhost:5004'}] test_eq[{