feat: set host with header

This commit is contained in:
2026-04-12 00:18:44 +02:00
parent 5c13f88d70
commit d749e5ee63
3 changed files with 24 additions and 7 deletions
+13 -6
View File
@@ -26,11 +26,18 @@ PUT /{page}/
```
```bash
# create archive from 'dist' dir and upload to /target/
# create archive from 'dist' dir and upload to /my-project/
tar -czC dist . | curl -X PUT \
--data-binary @- \
-H 'X-Token: <TOKEN>' \
http://stapler-host/target/
http://stapler-host/my-project/
# create archive from 'dist' dir and upload to /my-project/ and myproject.example.com
tar -czC dist . | curl -X PUT \
--data-binary @- \
-H 'X-Token: <TOKEN>' \
-H 'X-Host: myproject.example.com' \
http://stapler-host/my-project/
```
### Delete page
@@ -40,10 +47,10 @@ DELETE /{page}/
```
```bash
# delete /target/
# delete /my-project/
curl -X DELETE \
-H 'X-Token: <TOKEN>' \
http://stapler-host/target/
http://stapler-host/my-project/
```
## TODO
@@ -55,10 +62,10 @@ curl -X DELETE \
- [x] DELETE request
- [x] max file size
- [x] .host in /data/xxx can be translated as host in GET /
- [ ] header to setup .host file instead of in archive
- [x] header to setup .host file instead of in archive
- [ ] ignore .gitignore/.host etc at root
- [ ] log visits (and store accross sessions)
- [ ] deliver visits in /page/visits
- [ ] ignore gitignore/.host etc at root
- [ ] cerbot install in container + path env/arg
- [ ] redirect /.well-known/acme-challenge to specific path
- [ ] certbot/self-signed create/renew in specific dir
+2
View File
@@ -61,6 +61,8 @@ class StaplerRequestHandler(http.server.SimpleHTTPRequestHandler):
except Exception as e:
return self.send_error(http.HTTPStatus.INTERNAL_SERVER_ERROR, str(e))
self.send_status_only(http.HTTPStatus.CREATED, f"Resource /{sub_path}/ updated")
if self.headers["X-Host"]:
self.registry.set_host(sub_path, self.headers["X-Host"])
self.registry.add(sub_path)
def do_DELETE(self):
+9 -1
View File
@@ -2,6 +2,8 @@ import os
from . import params, page
_HOST_FILE = ".host"
class Registry:
def __init__(self, params: params.Parameters):
@@ -22,12 +24,18 @@ class Registry:
)
print("Updated: " + self.prefix + str(self.pages[path]))
def set_host(self, path: str, host: str):
path_host = os.path.join(self.data_dir, path, _HOST_FILE)
with open(path_host, mode="w") as host_file:
host_file.write(host)
self.pages[path].host = host
def __has_index(self, path: str) -> bool:
path_index = os.path.join(self.data_dir, path, "index.html")
return os.path.exists(path_index) and os.path.isfile(path_index)
def __get_host(self, path: str) -> str | None:
path_host = os.path.join(self.data_dir, path, ".host")
path_host = os.path.join(self.data_dir, path, _HOST_FILE)
if os.path.exists(path_host) and os.path.isfile(path_host):
try:
with open(path_host) as host_file: