Package a self-contained macOS app and harden serving
- Add macos/build.mjs: stages a universal .app outside the checkout, bundles and checksums Node, installs production deps per architecture, compiles the launcher shim, signs ad-hoc or with a Developer ID, and writes a zip and dmg with an optional notarization path. - Serve precompressed brotli/gzip assets and split admin panels and the public pages out of the main bundle. - Support HTTP range requests, and fix upload and quota accounting, gallery download counts and unreadable-file handling. - Add server HTTP integration tests, a Gitea CI workflow, a catalog backup script and a crash screen for the UI.
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
#!/bin/bash
|
||||
# Stop the Luminary server started by Luminary.app.
|
||||
# Double-clicking a .command file opens Terminal for as long as this runs.
|
||||
set -u
|
||||
|
||||
APP="/Applications/Luminary.app"
|
||||
if [ ! -d "$APP" ]; then
|
||||
APP="$(cd "$(dirname "$0")" && pwd)/Luminary.app"
|
||||
fi
|
||||
|
||||
if [ ! -d "$APP" ]; then
|
||||
echo "Luminary.app was not found in /Applications. Move it there and try again."
|
||||
read -r -p "Press Return to close this window." _
|
||||
exit 1
|
||||
fi
|
||||
|
||||
"$APP/Contents/Resources/launcher.sh" --stop
|
||||
read -r -p "Press Return to close this window." _
|
||||
@@ -0,0 +1,248 @@
|
||||
#!/bin/bash
|
||||
# Luminary.app launcher.
|
||||
#
|
||||
# Called by the native shim in Contents/MacOS/Luminary. Keeps the same rules as
|
||||
# the Windows launcher: pick a loopback port, start the bundled node exactly like
|
||||
# `node server/index.mjs`, wait for /api/health, then open the browser. The
|
||||
# server is detached and keeps running after the app exits, so launching again
|
||||
# reuses it instead of starting a second copy.
|
||||
#
|
||||
# launcher.sh start (or reuse) and open the window
|
||||
# launcher.sh --stop stop the server recorded in runtime.json
|
||||
# launcher.sh --status print pid, port, health and data directory
|
||||
# launcher.sh --verify check that this is a complete bundle (exit 3 if not)
|
||||
# launcher.sh --browser open a normal browser tab instead of an app window
|
||||
set -u
|
||||
|
||||
HERE="$(cd "$(dirname "$0")" && pwd)"
|
||||
APP="$HERE/app"
|
||||
NODE="$HERE/runtime/node"
|
||||
DATA="${LUMINARY_DATA:-$HOME/Library/Application Support/Luminary/data}"
|
||||
STATE="$DATA/runtime.json"
|
||||
LOGS="$DATA/logs"
|
||||
WINDOW="${LUMINARY_WINDOW:-app}"
|
||||
|
||||
say() { printf 'Luminary: %s\n' "$*"; }
|
||||
err() { printf 'Luminary: %s\n' "$*" >&2; }
|
||||
die() { err "$*"; exit 1; }
|
||||
|
||||
# A blocking dialog for launches from Finder, where stderr goes nowhere.
|
||||
dialog() {
|
||||
local message
|
||||
message=$(printf '%s' "$1" | tr '"' "'" | tr '\n' ' ')
|
||||
if command -v osascript >/dev/null 2>&1; then
|
||||
osascript -e "display dialog \"$message\" with title \"Luminary\" buttons {\"OK\"} default button 1 with icon caution" >/dev/null 2>&1 || true
|
||||
fi
|
||||
}
|
||||
|
||||
version_line() {
|
||||
if [ -f "$HERE/version.txt" ]; then head -1 "$HERE/version.txt"; else echo "Luminary (version unknown)"; fi
|
||||
}
|
||||
|
||||
state_port() {
|
||||
[ -f "$STATE" ] || return 0
|
||||
sed -n 's/.*"port"[[:space:]]*:[[:space:]]*\([0-9][0-9]*\).*/\1/p' "$STATE" | head -1
|
||||
}
|
||||
|
||||
state_pid() {
|
||||
[ -f "$STATE" ] || return 0
|
||||
sed -n 's/.*"pid"[[:space:]]*:[[:space:]]*\([0-9][0-9]*\).*/\1/p' "$STATE" | head -1
|
||||
}
|
||||
|
||||
healthy() {
|
||||
curl -fs -m 2 "http://127.0.0.1:$1/api/health" >/dev/null 2>&1
|
||||
}
|
||||
|
||||
# Ask node rather than nc: the bundle always has one, and nc's flags differ
|
||||
# between the BSD tool macOS ships and builds found elsewhere.
|
||||
port_in_use() {
|
||||
"$NODE" -e 'const net=require("net");const s=net.connect(+process.argv[1],"127.0.0.1");s.on("connect",()=>{s.destroy();process.exit(0)});s.on("error",()=>process.exit(1))' "$1" >/dev/null 2>&1
|
||||
}
|
||||
|
||||
tail_logs() {
|
||||
[ -f "$LOGS/server.err.log" ] || return 0
|
||||
err "last lines from $LOGS/server.err.log:"
|
||||
tail -n 20 "$LOGS/server.err.log" >&2 || true
|
||||
}
|
||||
|
||||
open_window() {
|
||||
local url="$1"
|
||||
if [ "$WINDOW" = "browser" ]; then
|
||||
open "$url"
|
||||
return
|
||||
fi
|
||||
if [ -d "/Applications/Google Chrome.app" ] || [ -d "$HOME/Applications/Google Chrome.app" ]; then
|
||||
open -na "Google Chrome" --args \
|
||||
--app="$url" \
|
||||
--user-data-dir="$DATA/window-profile-chrome" \
|
||||
--window-size=1500,950 --no-first-run --no-default-browser-check
|
||||
elif [ -d "/Applications/Microsoft Edge.app" ] || [ -d "$HOME/Applications/Microsoft Edge.app" ]; then
|
||||
open -na "Microsoft Edge" --args \
|
||||
--app="$url" \
|
||||
--user-data-dir="$DATA/window-profile-edge" \
|
||||
--window-size=1500,950 --no-first-run --no-default-browser-check
|
||||
else
|
||||
open "$url"
|
||||
fi
|
||||
}
|
||||
|
||||
verify() {
|
||||
local status=0 file size
|
||||
for file in "$NODE" "$APP/server/index.mjs" "$APP/dist/index.html"; do
|
||||
if [ ! -f "$file" ]; then
|
||||
err "missing: ${file#"$HERE"/}"
|
||||
status=3
|
||||
elif [ ! -s "$file" ]; then
|
||||
err "empty: ${file#"$HERE"/}"
|
||||
status=3
|
||||
else
|
||||
size=$(wc -c <"$file" | tr -d ' ')
|
||||
say "ok: ${file#"$HERE"/} ($size bytes)"
|
||||
fi
|
||||
done
|
||||
say "bundle: $HERE"
|
||||
say "version: $(version_line)"
|
||||
return $status
|
||||
}
|
||||
|
||||
start() {
|
||||
[ -x "$NODE" ] || chmod +x "$NODE" 2>/dev/null || true
|
||||
|
||||
if [ -f "$STATE" ]; then
|
||||
local existing
|
||||
existing=$(state_port)
|
||||
if [ -n "$existing" ] && healthy "$existing"; then
|
||||
say "already running on port $existing"
|
||||
open_window "http://127.0.0.1:$existing"
|
||||
exit 0
|
||||
fi
|
||||
rm -f "$STATE"
|
||||
fi
|
||||
|
||||
if ! verify >/dev/null 2>&1; then
|
||||
verify >&2 || true
|
||||
dialog "This copy of Luminary is incomplete. Copy Luminary.app out of the disk image or zip and try again. If it still fails, download the app again."
|
||||
exit 3
|
||||
fi
|
||||
|
||||
mkdir -p "$DATA" "$LOGS" 2>/dev/null || {
|
||||
dialog "Luminary cannot write to $DATA. Set LUMINARY_DATA to a folder you can write to and try again."
|
||||
exit 1
|
||||
}
|
||||
|
||||
local port=""
|
||||
local candidate
|
||||
# Brace expansion instead of seq: macOS does not ship seq.
|
||||
for candidate in {8787..8887}; do
|
||||
if ! port_in_use "$candidate"; then
|
||||
port="$candidate"
|
||||
break
|
||||
fi
|
||||
done
|
||||
[ -n "$port" ] || { dialog "No free port between 8787 and 8887. Quit the app using one and try again."; exit 1; }
|
||||
|
||||
LUMINARY_DATA="$DATA" \
|
||||
LUMINARY_LOCAL=1 \
|
||||
HOST=127.0.0.1 \
|
||||
PORT="$port" \
|
||||
NODE_ENV=production \
|
||||
nohup "$NODE" --disable-warning=ExperimentalWarning "$APP/server/index.mjs" \
|
||||
>>"$LOGS/server.out.log" 2>>"$LOGS/server.err.log" &
|
||||
local pid=$!
|
||||
|
||||
printf '{\n "pid": %s,\n "port": %s,\n "url": "http://127.0.0.1:%s",\n "data": "%s",\n "started": "%s"\n}\n' \
|
||||
"$pid" "$port" "$port" "$DATA" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" >"$STATE"
|
||||
|
||||
local waited=0
|
||||
while [ "$waited" -lt 240 ]; do
|
||||
if healthy "$port"; then
|
||||
open_window "http://127.0.0.1:$port"
|
||||
exit 0
|
||||
fi
|
||||
if ! kill -0 "$pid" 2>/dev/null; then
|
||||
rm -f "$STATE"
|
||||
tail_logs
|
||||
dialog "Luminary's server stopped while starting. Log: $LOGS/server.err.log"
|
||||
exit 1
|
||||
fi
|
||||
sleep 0.5
|
||||
waited=$((waited + 1))
|
||||
done
|
||||
|
||||
kill "$pid" 2>/dev/null || true
|
||||
rm -f "$STATE"
|
||||
tail_logs
|
||||
dialog "Luminary did not start within 120 seconds. Log: $LOGS/server.err.log"
|
||||
exit 1
|
||||
}
|
||||
|
||||
stop() {
|
||||
if [ ! -f "$STATE" ]; then
|
||||
say "not running"
|
||||
return 0
|
||||
fi
|
||||
local pid
|
||||
pid=$(state_pid)
|
||||
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
|
||||
kill -TERM "$pid" 2>/dev/null || true
|
||||
local tick
|
||||
for tick in {1..20}; do
|
||||
kill -0 "$pid" 2>/dev/null || break
|
||||
sleep 0.25
|
||||
done
|
||||
kill -0 "$pid" 2>/dev/null && kill -KILL "$pid" 2>/dev/null || true
|
||||
say "stopped (pid $pid)"
|
||||
else
|
||||
say "not running (removed a stale runtime.json)"
|
||||
fi
|
||||
rm -f "$STATE"
|
||||
}
|
||||
|
||||
status() {
|
||||
local port pid
|
||||
port=$(state_port)
|
||||
pid=$(state_pid)
|
||||
say "version: $(version_line)"
|
||||
say "bundle: $HERE"
|
||||
say "data: $DATA"
|
||||
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
|
||||
say "pid: $pid"
|
||||
elif [ -n "$pid" ]; then
|
||||
say "pid: $pid (not running)"
|
||||
else
|
||||
say "pid: -"
|
||||
fi
|
||||
if [ -n "$port" ] && healthy "$port"; then
|
||||
say "health: ok on http://127.0.0.1:$port"
|
||||
elif [ -n "$port" ]; then
|
||||
say "health: no response on port $port"
|
||||
else
|
||||
say "health: not started"
|
||||
fi
|
||||
}
|
||||
|
||||
case "${1:-}" in
|
||||
--stop) stop ;;
|
||||
--status) status ;;
|
||||
--verify) verify; exit $? ;;
|
||||
--browser) WINDOW=browser; start ;;
|
||||
--help | -h)
|
||||
cat <<'USAGE'
|
||||
Luminary.app launcher
|
||||
|
||||
No argument starts the app (or reuses a running copy) and opens the window.
|
||||
--browser open a normal browser tab instead of a Chrome/Edge app window
|
||||
--stop stop the server this bundle started
|
||||
--status print version, pid, port, health and the data directory
|
||||
--verify check that the bundle is complete
|
||||
|
||||
Data: ~/Library/Application Support/Luminary/data (override with LUMINARY_DATA)
|
||||
Logs: <data>/logs/server.out.log and server.err.log
|
||||
USAGE
|
||||
;;
|
||||
"") start ;;
|
||||
*)
|
||||
err "unknown argument: $1 (try --help)"
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Luminary.app main executable.
|
||||
*
|
||||
* The bundle needs a Mach-O main executable so it can be code signed and, if a
|
||||
* certificate is ever bought, notarized: Apple's notary service rejects a
|
||||
* bundle whose CFBundleExecutable is a shell script. This shim resolves its own
|
||||
* path, finds Contents/Resources/launcher.sh next to it, and execs bash on it,
|
||||
* passing every argument through. All the actual launcher logic lives in that
|
||||
* script, where it can be changed without recompiling anything.
|
||||
*/
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#ifdef __APPLE__
|
||||
#include <mach-o/dyld.h>
|
||||
#endif
|
||||
|
||||
/** Write this process's executable path into buf, or return -1. */
|
||||
static int executable_path(char *buf, size_t size) {
|
||||
#ifdef __APPLE__
|
||||
uint32_t needed = (uint32_t)size;
|
||||
if (_NSGetExecutablePath(buf, &needed) != 0) return -1;
|
||||
return 0;
|
||||
#else
|
||||
/* Not used in the bundle; kept so the shim can be built and tested when
|
||||
* packaging is exercised from a non-Apple host. */
|
||||
ssize_t length = readlink("/proc/self/exe", buf, size - 1);
|
||||
if (length < 0) return -1;
|
||||
buf[length] = '\0';
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
char raw[PATH_MAX];
|
||||
char exe[PATH_MAX];
|
||||
char script[PATH_MAX];
|
||||
|
||||
if (executable_path(raw, sizeof raw) != 0) {
|
||||
fprintf(stderr, "luminary: cannot resolve the application path\n");
|
||||
return 1;
|
||||
}
|
||||
if (realpath(raw, exe) == NULL) {
|
||||
fprintf(stderr, "luminary: cannot resolve %s\n", raw);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* exe is .../Luminary.app/Contents/MacOS/Luminary */
|
||||
char *slash = strrchr(exe, '/');
|
||||
if (slash == NULL) {
|
||||
fprintf(stderr, "luminary: unexpected executable path %s\n", exe);
|
||||
return 1;
|
||||
}
|
||||
*slash = '\0';
|
||||
|
||||
if (snprintf(script, sizeof script, "%s/../Resources/launcher.sh", exe) >= (int)sizeof script) {
|
||||
fprintf(stderr, "luminary: application path is too long\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
char *args[argc + 2];
|
||||
args[0] = "/bin/bash";
|
||||
args[1] = script;
|
||||
for (int i = 1; i < argc; i++) args[i + 1] = argv[i];
|
||||
args[argc + 1] = NULL;
|
||||
|
||||
execv("/bin/bash", args);
|
||||
perror("luminary: could not start the launcher");
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user