from pprint import pprintAPI
Caddy admin
Initial functions
get_id
def get_id(
path
):
Get a ID full URL from a path
host = 'jph.answer.ai'get_id('jph.answer.ai')'http://localhost:2019/id/jph.answer.ai/'
get_path
def get_path(
path
):
Get a config full URL from a path
get_path('/apps/tls/automation/policies')'http://localhost:2019/config/apps/tls/automation/policies/'
gid
def gid(
path:str='/'
):
Gets the id at path
has_id
def has_id(
id
):
Check if id is set up
gcfg
def gcfg(
path:str='/', method:str='get'
):
Gets the config at path
has_path
def has_path(
path
):
Check if path is set up
# gcfg()pid
def pid(
d, path:str='/', method:str='post'
):
Puts the config d into path
pcfg
def pcfg(
d, path:str='/', method:str='post'
):
Puts the config d into path
# pcfg({})nested_setdict
def nested_setdict(
sd, value, keys:VAR_POSITIONAL
):
Returns sd updated to set value at the path keys
nested_setdict({'a':'b'}, {'c':'d'}, 'apps', 'http', 'servers', 'srv0'){'a': 'b', 'apps': {'http': {'servers': {'srv0': {'c': 'd'}}}}}
path2keys
def path2keys(
path
):
Split path by ‘/’ into a list
path2keys('/apps/tls/automation/policies')['apps', 'tls', 'automation', 'policies']
keys2path
def keys2path(
keys:VAR_POSITIONAL
):
Join keys into a ‘/’ separated path
keys2path('apps', 'tls', 'automation', 'policies')'/apps/tls/automation/policies'
nested_setcfg
def nested_setcfg(
value, keys:VAR_POSITIONAL
):
Call self as a function.
init_path
def init_path(
path, skip:int=0
):
Call self as a function.
Automation setup
cf_token = os.environ.get('CADDY_CF_TOKEN', 'XXX')get_acme_config
def get_acme_config(
token
):
Call self as a function.
add_tls_internal_config
def add_tls_internal_config(
):
Call self as a function.
add_acme_config
def add_acme_config(
cf_token, subjects:NoneType=None
):
Call self as a function.
add_acme_config(cf_token)# gcfg('/apps/tls/automation/policies')[0]caddy_docs
def caddy_docs(
):
Call self as a function.
get_schema
def get_schema(
path:str
):
Get the caddy schema node at path (e.g. ‘/definitions/tls.automation.OnDemandConfig’)
search_schema
def search_schema(
term:str, path:str='', max_results:int=20
):
Recursively search caddy schema caddy_docs for keys/values containing term
Think you can use &[search_schema,get_schema] to find info about on_demand, and tell us all about it? If anything isn’t convenient, stop, so we can fix the tools. BTW you do have a tool to import modules you can use.
Route setup
init_routes
def init_routes(
srv_name:str='srv0', skip:int=1
):
Create basic http server/routes config
init_routes()setup_pki_trust
def setup_pki_trust(
install_trust
):
Configure PKI certificate authority trust installation
setup_caddy
def setup_caddy(
cf_token:NoneType=None, # Cloudflare API token
srv_name:str='srv0', # Server name in the Caddyfile
local:bool=False, # Whether or not this is for localdev or deployment
install_trust:bool=None, # Install trust store?
subjects:NoneType=None, # Subject names to restrict ACME cert issuance to
):
Create SSL config and HTTP app skeleton
# pcfg({})
setup_caddy(cf_token)# gcfg(srvs_path)add_route
def add_route(
route
):
Add route dict to config
del_id
def del_id(
id
):
Delete route for id (e.g. a host)
# del_id(host)add_reverse_proxy
def add_reverse_proxy(
from_host, to_url, st_delay:str='1m', compress:bool=True
):
Create a reverse proxy handler
host = 'foo.fast.ai'add_reverse_proxy(host, "localhost:5001")gid(host){ '@id': 'foo.fast.ai',
'handle': [{'encodings': {'gzip': {'level': 1}, 'zstd': {'level': 'fastest'}}, 'handler': 'encode', 'prefer': ['zstd', 'gzip']}, {'handler': 'reverse_proxy', 'stream_close_delay': '1m', 'upstreams': [{'dial': 'localhost:5001'}]}],
'match': [{'host': ['foo.fast.ai']}],
'terminal': True}add_on_demand_tls
def add_on_demand_tls(
endpoint
):
Call self as a function.
# pcfg({})
# setup_caddy(cf_token=cf_token, install_trust=True, subjects=["*.fast.ai","fast.ai"])
# add_on_demand_tls("http://localhost:5431/verifydom")# pcfg({})
# setup_caddy(cf_token=cf_token, install_trust=True)
# add_on_demand_tls("http://localhost:5431/verifydom")
# add_route({"handle": [{"handler": "reverse_proxy", "upstreams": [{"dial": "localhost:5001"}]}]})
# pcfg([{}], f"{srvs_path}/srv0/tls_connection_policies", method='put')
# pcfg({"logs": {"default": {"level": "DEBUG"}}}, "/logging", method='put')add_wildcard_route
def add_wildcard_route(
domain
):
Add a wildcard subdomain
add_wildcard_route('something.fast.ai')add_sub_reverse_proxy
def add_sub_reverse_proxy(
domain, subdomain, port:Union, # A single port or list of ports
host:str='localhost', st_delay:str='1m', encode:bool=True
):
Add a reverse proxy to a wildcard subdomain supporting multiple ports
add_sub_reverse_proxy('something.fast.ai', 'foo', 5001)del_id('foo.something.fast.ai')