- Introduced a new Lua-based REST API for darktable, allowing external applications to interact with the darktable library. - Implemented core functionalities including image listing, retrieval, and preview serving. - Added support for OAuth 2.0 authentication with token generation. - Created a simple HTTP router to handle API requests. - Included JSON encoding/decoding utilities for API responses. - Developed a script to launch the API server alongside darktable, ensuring seamless integration. - Added necessary configuration and setup for the API server, including CORS support and error handling.
110 lines
4.6 KiB
Bash
Executable File
110 lines
4.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# start-with-api.sh — Launch the darktable REST API server and darktable together.
|
|
#
|
|
# What it does:
|
|
# 1. Optionally configures darktable's luarc to load the bridge plugin.
|
|
# 2. Starts the standalone LuaJIT REST API server in the background.
|
|
# 3. Starts darktable in the foreground.
|
|
# 4. Stops the API server when darktable exits.
|
|
#
|
|
# Usage:
|
|
# ./start-with-api.sh [darktable-options...]
|
|
#
|
|
# Environment variables (all optional):
|
|
# DT_CLIENT_ID OAuth client ID (default: darktable-client)
|
|
# DT_CLIENT_SECRET OAuth client secret (default: changeme)
|
|
# DT_JWT_SECRET JWT signing secret (default: change-this-secret-in-production)
|
|
# DT_API_HOST Server bind address (default: 127.0.0.1)
|
|
# DT_API_PORT Server port (default: 7645)
|
|
# DT_LIBRARY darktable library DB (default: ~/.config/darktable/library.db)
|
|
# DT_REFRESH_QUEUE IPC queue file (default: /tmp/dt-api-refresh.queue)
|
|
|
|
set -euo pipefail
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
|
|
: "${DT_CLIENT_ID:=darktable-client}"
|
|
: "${DT_CLIENT_SECRET:=changeme}"
|
|
: "${DT_JWT_SECRET:=change-this-secret-in-production}"
|
|
: "${DT_API_HOST:=127.0.0.1}"
|
|
: "${DT_API_PORT:=7645}"
|
|
: "${DT_REFRESH_QUEUE:=/tmp/dt-api-refresh.queue}"
|
|
|
|
export DT_CLIENT_ID DT_CLIENT_SECRET DT_JWT_SECRET
|
|
export DT_API_HOST DT_API_PORT DT_REFRESH_QUEUE
|
|
|
|
# ── 1. Ensure the bridge plugin is in darktable's luarc ───────────────────────
|
|
|
|
LUARC="$HOME/.config/darktable/luarc"
|
|
BRIDGE_REQUIRE='require "contrib/darktable-api/darktable-api-bridge"'
|
|
|
|
if ! grep -qF "$BRIDGE_REQUIRE" "$LUARC" 2>/dev/null; then
|
|
echo "[start-with-api] Adding bridge plugin to $LUARC"
|
|
echo "$BRIDGE_REQUIRE" >> "$LUARC"
|
|
fi
|
|
|
|
# ── 2. Install the bridge plugin into darktable's lua directory ───────────────
|
|
# darktable searches $HOME/.config/darktable/lua/ for scripts.
|
|
# We copy (not symlink) the bridge plugin file there so darktable can
|
|
# require "contrib/darktable-api/darktable-api-bridge" without any extra path config.
|
|
|
|
DT_LUA_DIR="$HOME/.config/darktable/lua"
|
|
BRIDGE_DEST="$DT_LUA_DIR/contrib/darktable-api"
|
|
BRIDGE_SRC="$SCRIPT_DIR/darktable-api-bridge.lua"
|
|
|
|
mkdir -p "$BRIDGE_DEST"
|
|
if ! cmp -s "$BRIDGE_SRC" "$BRIDGE_DEST/darktable-api-bridge.lua" 2>/dev/null; then
|
|
cp "$BRIDGE_SRC" "$BRIDGE_DEST/darktable-api-bridge.lua"
|
|
echo "[start-with-api] Installed bridge plugin → $BRIDGE_DEST/"
|
|
fi
|
|
|
|
# ── 3. Clean up any stale queue file ─────────────────────────────────────────
|
|
|
|
rm -f "$DT_REFRESH_QUEUE" "${DT_REFRESH_QUEUE}.processing"
|
|
|
|
# ── 4. Start the LuaJIT REST API server ───────────────────────────────────────
|
|
|
|
API_LOG="${TMPDIR:-/tmp}/darktable-api.log"
|
|
echo "[start-with-api] Starting API server on http://${DT_API_HOST}:${DT_API_PORT}"
|
|
echo "[start-with-api] Log: $API_LOG"
|
|
|
|
luajit "$SCRIPT_DIR/server.lua" "$DT_API_PORT" "$DT_API_HOST" >"$API_LOG" 2>&1 &
|
|
API_PID=$!
|
|
|
|
# Give the server a moment to bind and confirm it's up.
|
|
sleep 0.5
|
|
if ! kill -0 "$API_PID" 2>/dev/null; then
|
|
echo "[start-with-api] ERROR: API server failed to start. Check $API_LOG" >&2
|
|
cat "$API_LOG" >&2
|
|
exit 1
|
|
fi
|
|
|
|
if ! curl -sf "http://${DT_API_HOST}:${DT_API_PORT}/health" >/dev/null 2>&1; then
|
|
echo "[start-with-api] WARNING: health check failed; server may still be starting"
|
|
fi
|
|
|
|
echo "[start-with-api] API server running (PID $API_PID)"
|
|
echo ""
|
|
echo " Get a token:"
|
|
echo " curl -X POST http://${DT_API_HOST}:${DT_API_PORT}/oauth/token \\"
|
|
echo " -d 'grant_type=client_credentials&client_id=${DT_CLIENT_ID}&client_secret=${DT_CLIENT_SECRET}'"
|
|
echo ""
|
|
echo " Trigger a cache refresh (replace TOKEN and IMAGE_ID):"
|
|
echo " curl -X POST http://${DT_API_HOST}:${DT_API_PORT}/api/v1/images/IMAGE_ID/refresh \\"
|
|
echo " -H 'Authorization: Bearer TOKEN'"
|
|
echo ""
|
|
|
|
# ── 5. Start darktable ────────────────────────────────────────────────────────
|
|
|
|
echo "[start-with-api] Starting darktable..."
|
|
darktable "$@" || true
|
|
|
|
# ── 6. Stop the API server on exit ───────────────────────────────────────────
|
|
|
|
echo ""
|
|
echo "[start-with-api] darktable exited; stopping API server (PID $API_PID)"
|
|
kill "$API_PID" 2>/dev/null || true
|
|
wait "$API_PID" 2>/dev/null || true
|
|
rm -f "$DT_REFRESH_QUEUE" "${DT_REFRESH_QUEUE}.processing"
|
|
echo "[start-with-api] done"
|