Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbb8d437

Browse files
committed
fixes#9
1 parent757b392 commitbb8d437

File tree

3 files changed

+83
-55
lines changed

3 files changed

+83
-55
lines changed

‎fastcaddy/_modidx.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'fastcaddy.core.add_reverse_proxy': ('core.html#add_reverse_proxy','fastcaddy/core.py'),
1010
'fastcaddy.core.add_route': ('core.html#add_route','fastcaddy/core.py'),
1111
'fastcaddy.core.add_sub_reverse_proxy': ('core.html#add_sub_reverse_proxy','fastcaddy/core.py'),
12+
'fastcaddy.core.add_tls_internal_config': ('core.html#add_tls_internal_config','fastcaddy/core.py'),
1213
'fastcaddy.core.add_wildcard_route': ('core.html#add_wildcard_route','fastcaddy/core.py'),
1314
'fastcaddy.core.del_id': ('core.html#del_id','fastcaddy/core.py'),
1415
'fastcaddy.core.gcfg': ('core.html#gcfg','fastcaddy/core.py'),

‎fastcaddy/core.py‎

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/00_core.ipynb.
44

55
# %% auto 0
6-
__all__= ['acme_path','srvs_path','rts_path','get_id','get_path','gid','has_id','gcfg','has_path','pid','pcfg',
6+
__all__= ['automation_path','srvs_path','rts_path','get_id','get_path','gid','has_id','gcfg','has_path','pid','pcfg',
77
'nested_setdict','path2keys','keys2path','nested_setcfg','init_path','get_acme_config',
8-
'add_acme_config','init_routes','setup_caddy','add_route','del_id','add_reverse_proxy',
9-
'add_wildcard_route','add_sub_reverse_proxy']
8+
'add_tls_internal_config','add_acme_config','init_routes','setup_caddy','add_route','del_id',
9+
'add_reverse_proxy','add_wildcard_route','add_sub_reverse_proxy']
1010

1111
# %% ../nbs/00_core.ipynb 3
1212
importos,subprocess,httpx,json
@@ -108,66 +108,73 @@ def init_path(path, skip=0):
108108
pcfg({},keys2path(*sp))
109109

110110
# %% ../nbs/00_core.ipynb 28
111-
acme_path='/apps/tls/automation'
111+
automation_path='/apps/tls/automation'
112112
defget_acme_config(token):
113113
prov= {"provider": {"name":"cloudflare","api_token":token } }
114-
return {
115-
"module":"acme",
116-
"challenges": {"dns":prov }
117-
}
114+
return {"module":"acme","challenges": {"dns":prov } }
118115

119116
# %% ../nbs/00_core.ipynb 29
117+
defadd_tls_internal_config():
118+
ifhas_path(automation_path):return
119+
pcfg({})
120+
init_path(automation_path)
121+
val= [{"issuers": [{"module":"internal"}]}]
122+
pcfg(val,automation_path+'/policies')
123+
124+
# %% ../nbs/00_core.ipynb 30
120125
defadd_acme_config(cf_token):
121-
ifhas_path(acme_path):return
126+
ifhas_path(automation_path):return
122127
pcfg({})
123-
init_path(acme_path)
128+
init_path(automation_path)
124129
val= [get_acme_config(cf_token)]
125130
pcfg([{'issuers':val}],acme_path+'/policies')
126131

127-
# %% ../nbs/00_core.ipynb33
132+
# %% ../nbs/00_core.ipynb34
128133
srvs_path='/apps/http/servers'
129134
rts_path=srvs_path+'/srv0/routes'
130135

131-
# %% ../nbs/00_core.ipynb34
132-
definit_routes(srv_name='srv0'):
136+
# %% ../nbs/00_core.ipynb35
137+
definit_routes(srv_name='srv0',skip=1):
133138
"Create basic http server/routes config"
134139
ifhas_path(srvs_path):return
135-
init_path(srvs_path,skip=1)
140+
init_path(srvs_path,skip=skip)
136141
ir= {'listen': [':80',':443'],'routes': [],'protocols': ['h1','h2']}
137142
pcfg(ir,f"{srvs_path}/{srv_name}")
138143

139-
# %% ../nbs/00_core.ipynb 36
140-
defsetup_caddy(cf_token,srv_name='srv0'):
144+
# %% ../nbs/00_core.ipynb 37
145+
defsetup_caddy(
146+
cf_token=None,# Cloudflare API token
147+
srv_name='srv0',# Server name in the Caddyfile
148+
local:bool=False):# Whether or not this is for localdev or deployment
141149
"Create SSL config and HTTP app skeleton"
142-
add_acme_config(cf_token)
150+
iflocal:add_tls_internal_config()
151+
else:add_acme_config(cf_token)
143152
init_routes(srv_name)
144153

145-
# %% ../nbs/00_core.ipynb39
154+
# %% ../nbs/00_core.ipynb40
146155
defadd_route(route):
147156
"Add `route` dict to config"
148157
returnpcfg(route,rts_path)
149158

150-
# %% ../nbs/00_core.ipynb40
159+
# %% ../nbs/00_core.ipynb41
151160
defdel_id(id):
152161
"Delete route for `id` (e.g. a host)"
153162
xdelete(get_id(id))
154163

155-
# %% ../nbs/00_core.ipynb42
164+
# %% ../nbs/00_core.ipynb43
156165
defadd_reverse_proxy(from_host,to_url):
157166
"Create a reverse proxy handler"
158167
ifhas_id(from_host):del_id(from_host)
159168
route= {
160-
"handle": [{
161-
"handler":"reverse_proxy",
162-
"upstreams": [{"dial":to_url}]
163-
}],
169+
"handle": [{"handler":"reverse_proxy",
170+
"upstreams": [{"dial":to_url}] }],
164171
"match": [{"host": [from_host]}],
165172
"@id":from_host,
166173
"terminal":True
167174
}
168175
add_route(route)
169176

170-
# %% ../nbs/00_core.ipynb46
177+
# %% ../nbs/00_core.ipynb47
171178
defadd_wildcard_route(domain):
172179
"Add a wildcard subdomain"
173180
route= {
@@ -180,11 +187,11 @@ def add_wildcard_route(domain):
180187
}
181188
add_route(route)
182189

183-
# %% ../nbs/00_core.ipynb48
190+
# %% ../nbs/00_core.ipynb49
184191
defadd_sub_reverse_proxy(
185192
domain,
186193
subdomain,
187-
port:str|int|Sequence[str|int],# A single port or list of ports
194+
port:str|int|Sequence,# A single port or list of ports
188195
host='localhost'
189196
):
190197
"Add a reverse proxy to a wildcard subdomain supporting multiple ports"
@@ -195,9 +202,6 @@ def add_sub_reverse_proxy(
195202
new_route= {
196203
"@id":route_id,
197204
"match": [{"host": [route_id]}],
198-
"handle": [{
199-
"handler":"reverse_proxy",
200-
"upstreams":upstreams
201-
}]
205+
"handle": [{"handler":"reverse_proxy","upstreams":upstreams }]
202206
}
203207
pid([new_route],f"{wildcard_id}/handle/0/routes/...")

‎nbs/00_core.ipynb‎

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@
360360
"cell_type":"markdown",
361361
"metadata": {},
362362
"source": [
363-
"##Acme setup"
363+
"##Automation setup"
364364
]
365365
},
366366
{
@@ -374,18 +374,30 @@
374374
},
375375
{
376376
"cell_type":"code",
377-
"execution_count":null,
377+
"execution_count":1,
378378
"metadata": {},
379379
"outputs": [],
380380
"source": [
381381
"#| export\n",
382-
"acme_path = '/apps/tls/automation'\n",
382+
"automation_path = '/apps/tls/automation'\n",
383383
"def get_acme_config(token):\n",
384384
" prov = {\"provider\": {\"name\":\"cloudflare\",\"api_token\": token } }\n",
385-
" return {\n",
386-
"\"module\":\"acme\",\n",
387-
"\"challenges\": {\"dns\": prov }\n",
388-
" }"
385+
" return {\"module\":\"acme\",\"challenges\": {\"dns\": prov } }"
386+
]
387+
},
388+
{
389+
"cell_type":"code",
390+
"execution_count":null,
391+
"metadata": {},
392+
"outputs": [],
393+
"source": [
394+
"#| export\n",
395+
"def add_tls_internal_config():\n",
396+
" if has_path(automation_path): return\n",
397+
" pcfg({})\n",
398+
" init_path(automation_path)\n",
399+
" val = [{\"issuers\": [{\"module\":\"internal\"}]}]\n",
400+
" pcfg(val, automation_path+'/policies')"
389401
]
390402
},
391403
{
@@ -396,9 +408,9 @@
396408
"source": [
397409
"#| export\n",
398410
"def add_acme_config(cf_token):\n",
399-
" if has_path(acme_path): return\n",
411+
" if has_path(automation_path): return\n",
400412
" pcfg({})\n",
401-
" init_path(acme_path)\n",
413+
" init_path(automation_path)\n",
402414
" val = [get_acme_config(cf_token)]\n",
403415
" pcfg([{'issuers':val}], acme_path+'/policies')"
404416
]
@@ -446,10 +458,10 @@
446458
"outputs": [],
447459
"source": [
448460
"#| export\n",
449-
"def init_routes(srv_name='srv0'):\n",
461+
"def init_routes(srv_name='srv0', skip=1):\n",
450462
"\"Create basic http server/routes config\"\n",
451463
" if has_path(srvs_path): return\n",
452-
" init_path(srvs_path, skip=1)\n",
464+
" init_path(srvs_path, skip=skip)\n",
453465
" ir = {'listen': [':80', ':443'], 'routes': [], 'protocols': ['h1', 'h2']}\n",
454466
" pcfg(ir, f\"{srvs_path}/{srv_name}\")"
455467
]
@@ -470,9 +482,13 @@
470482
"outputs": [],
471483
"source": [
472484
"#| export\n",
473-
"def setup_caddy(cf_token, srv_name='srv0'):\n",
485+
"def setup_caddy(\n",
486+
" cf_token=None, # Cloudflare API token\n",
487+
" srv_name='srv0', # Server name in the Caddyfile\n",
488+
" local:bool=False): # Whether or not this is for localdev or deployment\n",
474489
"\"Create SSL config and HTTP app skeleton\"\n",
475-
" add_acme_config(cf_token)\n",
490+
" if local: add_tls_internal_config()\n",
491+
" else: add_acme_config(cf_token)\n",
476492
" init_routes(srv_name)"
477493
]
478494
},
@@ -539,10 +555,8 @@
539555
"\"Create a reverse proxy handler\"\n",
540556
" if has_id(from_host): del_id(from_host)\n",
541557
" route = {\n",
542-
"\"handle\": [{\n",
543-
"\"handler\":\"reverse_proxy\",\n",
544-
"\"upstreams\": [{\"dial\": to_url}]\n",
545-
" }],\n",
558+
"\"handle\": [{\"handler\":\"reverse_proxy\",\n",
559+
"\"upstreams\": [{\"dial\": to_url}] }],\n",
546560
"\"match\": [{\"host\": [from_host]}],\n",
547561
"\"@id\": from_host,\n",
548562
"\"terminal\": True\n",
@@ -616,7 +630,7 @@
616630
"def add_sub_reverse_proxy(\n",
617631
" domain,\n",
618632
" subdomain,\n",
619-
" port:str|int|Sequence[str|int], # A single port or list of ports\n",
633+
" port:str|int|Sequence, # A single port or list of ports\n",
620634
" host='localhost'\n",
621635
" ):\n",
622636
"\"Add a reverse proxy to a wildcard subdomain supporting multiple ports\"\n",
@@ -627,10 +641,7 @@
627641
" new_route = {\n",
628642
"\"@id\": route_id,\n",
629643
"\"match\": [{\"host\": [route_id]}],\n",
630-
"\"handle\": [{\n",
631-
"\"handler\":\"reverse_proxy\",\n",
632-
"\"upstreams\": upstreams\n",
633-
" }]\n",
644+
"\"handle\": [{\"handler\":\"reverse_proxy\",\"upstreams\": upstreams }]\n",
634645
" }\n",
635646
" pid([new_route], f\"{wildcard_id}/handle/0/routes/...\")"
636647
]
@@ -662,7 +673,7 @@
662673
},
663674
{
664675
"cell_type":"code",
665-
"execution_count":null,
676+
"execution_count":2,
666677
"metadata": {},
667678
"outputs": [],
668679
"source": [
@@ -682,9 +693,21 @@
682693
],
683694
"metadata": {
684695
"kernelspec": {
685-
"display_name":"python3",
696+
"display_name":"Python 3 (ipykernel)",
686697
"language":"python",
687698
"name":"python3"
699+
},
700+
"language_info": {
701+
"codemirror_mode": {
702+
"name":"ipython",
703+
"version":3
704+
},
705+
"file_extension":".py",
706+
"mimetype":"text/x-python",
707+
"name":"python",
708+
"nbconvert_exporter":"python",
709+
"pygments_lexer":"ipython3",
710+
"version":"3.12.8"
688711
}
689712
},
690713
"nbformat":4,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp