From c0d4ad668bea8d3b8efcce9087339c52df1ccbeb Mon Sep 17 00:00:00 2001 From: ge Date: Wed, 26 Apr 2023 16:46:36 +0300 Subject: [PATCH] upd --- README.md | 30 ++++++++++++++++++++++++++---- cursed-archivebox-api.service | 14 ++++++++++++++ cursed_archivebox_api.py | 1 + 3 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 cursed-archivebox-api.service diff --git a/README.md b/README.md index a9f5dba..4289663 100644 --- a/README.md +++ b/README.md @@ -2,20 +2,42 @@ ArchiveBox [doesn't have a web API](https://github.com/ArchiveBox/ArchiveBox/issues/496) yet. This is shitty single-endpoint API to automate page archiving. It uses subprocess to run archivebox CLI. archivebox CLI runs in new thread to avoid blocking main thread. -# Install and run +# Deployment -Install dependencies: +You must deploy API to server which ArchieveBox runs. + +Make working directory, create virtualenv and install dependencies: ``` +apt install -y git python3-venv +mkdir -p /opt/cursed_archievebox_api && cd /opt/cursed_archievebox_api +python -m venv env +source env/bin/activate pip install bottle gunicorn +git clone https://git.nxhs.cloud/ge/cursed-archivebox-api.git . ``` -Start API on server where ArchiveBox container is running. Set actual path to docker-compose.yml. +Start API. Set actual path to docker-compose.yml: ``` ARCHIVEBOX_BIN="docker compose -f /opt/archievebox/docker-compose.yml run archivebox" python cursed_archivebox_api.py ``` +See also [cursed-archivebox-api.service](cursed-archivebox-api.service). + +Install service: + +``` +cp cursed-archivebox-api.service /etc/systemd/system +systemctl enable --now cursed-archivebox-api.service +``` + +See logs: + +``` +journalctl -u cursed-archivebox-api.service -f +``` + # Environment | Variable | Default | @@ -33,7 +55,7 @@ Query parameters: * `depth`. Archive depth. Default: 0 (current page) * `tag`. List of comma separated tags e.g. `my_tag`, `my_tag,another_one`. -Example: +Example request: ``` curl -i 'http://localhost:9998/add?url=https://example.com&depth=0&tag=api,example' diff --git a/cursed-archivebox-api.service b/cursed-archivebox-api.service new file mode 100644 index 0000000..f1d8ac7 --- /dev/null +++ b/cursed-archivebox-api.service @@ -0,0 +1,14 @@ +[Unit] +Description=Cursed API for ArchiveBox +After=syslog.target +After=network.target + +[Service] +Type=simple +Environment=ARCHIVEBOX_BIN="docker compose -f /opt/archivebox/docker-compose.yml run archivebox" +WorkingDirectory=/opt/cursed_archivebox_api +ExecStart=/opt/cursed_archivebox_api/env/bin/python cursed_archivebox_api.py +Restart=on-failure + +[Install] +WantedBy=multi-user.target diff --git a/cursed_archivebox_api.py b/cursed_archivebox_api.py index bb4d3ff..d6335fa 100644 --- a/cursed_archivebox_api.py +++ b/cursed_archivebox_api.py @@ -45,6 +45,7 @@ def run_bg_task(cmd): @get('/add') def add_to_archive() -> str: + response.set_header('Content-Type', 'application/json') url = request.query.url or None depth = request.query.depth or None tag = request.query.tag or None