Compare commits
43 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e82425a829 | |||
| e480aba2f1 | |||
| feee40c34b | |||
| c4cdb31529 | |||
| e6e89a1f4d | |||
| 8b8cb2f580 | |||
| a1c0cc090d | |||
| 4d359ff021 | |||
| cd1c6737e8 | |||
| 7df96a2273 | |||
| f27fc2622c | |||
| 0433fe8c0f | |||
| d3b890b739 | |||
| 9da4d50883 | |||
| c99ca82302 | |||
| 3f069eeb79 | |||
| e292453905 | |||
| 0263e51407 | |||
| 8676751ed6 | |||
| b4935ed880 | |||
| 9a6f0defd1 | |||
| a4f5312069 | |||
| ee0147869e | |||
| 740382fca1 | |||
| c76e5a19eb | |||
| 25f6631049 | |||
| 180acc92a0 | |||
| 58b7b32cdd | |||
| 689062fc87 | |||
| e6fd110073 | |||
| 0fdabcce86 | |||
| 5e1344af42 | |||
| 79de47de2c | |||
| 45963154d9 | |||
| aabaa2ffb0 | |||
| d5dcb08ec6 | |||
| 20a36eeb9e | |||
| 32a179c24a | |||
| 6bc026de19 | |||
| 0ae526b72e | |||
| 8221aef605 | |||
| f8487c43a0 | |||
| 36b99dd9e2 |
@@ -172,11 +172,12 @@ jobs:
|
||||
- name: Touch HTML placeholder files
|
||||
run: touch src/index.html src/about.html src/terms.html src/enterprise-terms.html
|
||||
|
||||
- name: Create soul-demo-image.tar placeholder
|
||||
# Dockerfile.stage COPYs this file (used by k3s at runtime).
|
||||
# We only need the COPY to succeed here; real tar is built by
|
||||
# build-stage.sh in the deploy pipeline.
|
||||
run: touch dist/soul-demo-image.tar
|
||||
- name: Create soul-demo placeholder
|
||||
# Dockerfile.stage COPYs dist/soul-demo. We only need the binary to exist
|
||||
# for the Docker build to succeed; the real binary is compiled in stage CI.
|
||||
run: |
|
||||
touch dist/soul-demo
|
||||
chmod +x dist/soul-demo
|
||||
|
||||
- name: Build Docker image (local only — no push)
|
||||
run: |
|
||||
|
||||
+48
-13
@@ -148,6 +148,46 @@ jobs:
|
||||
--runtime="$EL_RUNTIME"
|
||||
echo "Binary: $(ls -lh dist/neuron-landing)"
|
||||
|
||||
- name: Relink neuron-web with HAVE_CURL
|
||||
# elb does not pass -DHAVE_CURL when compiling el_runtime.c, so
|
||||
# http_get/http_post return {"error":"not built with HAVE_CURL"}.
|
||||
# Fix: after elb generates all intermediate .c files in dist/, recompile
|
||||
# el_runtime.c with -DHAVE_CURL and relink the whole binary manually.
|
||||
# All component .c files (nav.c, hero.c, etc.) are generated by elb and
|
||||
# remain in dist/ after the build — we collect them here, exclude the
|
||||
# separate soul-demo.c binary, and relink with libcurl.
|
||||
if: steps.changetype.outputs.asset_only != 'true'
|
||||
run: |
|
||||
set -euo pipefail
|
||||
|
||||
# Compile el_runtime.c with full curl support
|
||||
cc -O2 -DHAVE_CURL -c runtime/el_runtime.c -I runtime/ -o /tmp/el_runtime_curl.o
|
||||
echo "el_runtime_curl.o compiled: $(ls -lh /tmp/el_runtime_curl.o)"
|
||||
|
||||
# Collect every neuron-web .c file elb deposited in dist/
|
||||
# (both committed stubs and freshly-generated component files)
|
||||
mapfile -t C_SRCS < <(find dist/ -maxdepth 1 -name '*.c' ! -name 'soul-demo.c')
|
||||
echo "Relinking ${#C_SRCS[@]} C files..."
|
||||
|
||||
cc -O2 -rdynamic \
|
||||
-I runtime/ -I dist/ \
|
||||
-o dist/neuron-landing \
|
||||
"${C_SRCS[@]}" /tmp/el_runtime_curl.o \
|
||||
-lcurl -lpthread -ldl -lm -lssl -lcrypto
|
||||
|
||||
echo "Relinked: $(ls -lh dist/neuron-landing)"
|
||||
# Verification: if compiled WITHOUT HAVE_CURL the stub string
|
||||
# "not built with HAVE_CURL" is baked into the binary's rodata.
|
||||
# Its absence confirms curl code is compiled in.
|
||||
if strings dist/neuron-landing | grep -q 'not built with HAVE_CURL'; then
|
||||
echo "ERROR: no-curl stub string still in binary — HAVE_CURL not compiled"
|
||||
exit 1
|
||||
fi
|
||||
# Confirm curl symbols visible in dynamic table
|
||||
nm -D dist/neuron-landing | grep curl_easy_init || \
|
||||
nm dist/neuron-landing | grep curl || true
|
||||
echo "HAVE_CURL verified ✓"
|
||||
|
||||
# ── Compile JS client sources ─────────────────────────────────────────
|
||||
|
||||
- name: Compile JS El sources
|
||||
@@ -173,15 +213,15 @@ jobs:
|
||||
|
||||
# ── Docker build + push ───────────────────────────────────────────────
|
||||
|
||||
- name: Build soul-demo image tar
|
||||
# Dockerfile.stage COPYs dist/soul-demo-image.tar so k3s can import
|
||||
# soul-demo:local at runtime. We compile soul-demo from source on the
|
||||
# host runner (ci-base has gcc), build a minimal OCI image, and save it.
|
||||
- name: Build soul-demo binary
|
||||
# Compile soul-demo directly on the host runner (ci-base has gcc).
|
||||
# Cloud Run runs soul-demo as a direct subprocess with a watchdog loop —
|
||||
# no k3s, no OCI image needed. One binary per container; Cloud Run
|
||||
# handles horizontal scaling.
|
||||
# Moved AFTER JS compilation to avoid Docker memory pressure killing elc.
|
||||
if: steps.changetype.outputs.asset_only != 'true'
|
||||
run: |
|
||||
set -euo pipefail
|
||||
# Compile el_runtime.o and soul-demo on the host runner
|
||||
cc -O2 -DHAVE_CURL -c runtime/el_runtime.c -I runtime/ -o /tmp/el_runtime.o
|
||||
cc -O2 -rdynamic -DEL_SOUL_DEMO_BUILD \
|
||||
-I runtime/ \
|
||||
@@ -189,20 +229,15 @@ jobs:
|
||||
dist/soul-demo.c dist/vessel_stubs.c /tmp/el_runtime.o \
|
||||
-lcurl -lpthread -ldl -lm -lssl -lcrypto
|
||||
echo "soul-demo compiled: $(ls -lh dist/soul-demo)"
|
||||
# Package as minimal OCI image for k3s import
|
||||
# --no-cache: prevents reuse of corrupted overlay2 layers from prior failed runs
|
||||
docker build --no-cache -f dist/Dockerfile.soul-demo -t soul-demo:local dist/
|
||||
docker save soul-demo:local -o dist/soul-demo-image.tar
|
||||
echo "soul-demo-image.tar: $(du -sh dist/soul-demo-image.tar | cut -f1)"
|
||||
docker rmi soul-demo:local 2>/dev/null || true
|
||||
|
||||
- name: Build and tag image
|
||||
if: steps.changetype.outputs.asset_only != 'true'
|
||||
run: |
|
||||
set -euo pipefail
|
||||
# --no-cache: prevents reuse of corrupted overlay2 layers from prior failed runs.
|
||||
# Dockerfile.stage is now single-stage (no builder) so build is fast even without cache.
|
||||
docker build \
|
||||
--build-arg BUILDKIT_INLINE_CACHE=1 \
|
||||
--cache-from us-central1-docker.pkg.dev/neuron-785695/neuron-marketing/marketing:stage-latest \
|
||||
--no-cache \
|
||||
-f Dockerfile.stage \
|
||||
-t "marketing:${{ steps.tag.outputs.tag }}" \
|
||||
.
|
||||
|
||||
+15
-64
@@ -4,81 +4,36 @@
|
||||
# - neuron-web on port 8080 (landing page server)
|
||||
# - soul-demo on port 7772 (demo chat, localhost only)
|
||||
#
|
||||
# neuron-web is built by `elb build` in CI (not here). elb compiles each
|
||||
# .el source independently and links the result — no combined mega-file,
|
||||
# no exponential memory growth. The binary lands at dist/neuron-landing
|
||||
# (linux/amd64) and is COPY'd directly into the runtime image.
|
||||
# All binaries (neuron-web, soul-demo) are pre-built by CI on the host runner
|
||||
# before this Dockerfile runs. This keeps the Docker build single-stage with
|
||||
# no compilation and no network downloads.
|
||||
#
|
||||
# soul-demo.c is pre-committed (small, no OOM risk) and compiled here.
|
||||
# CI pre-build steps (in stage.yaml):
|
||||
# - neuron-web: built by `elb build` → dist/neuron-landing
|
||||
# - soul-demo: compiled by cc on host → dist/soul-demo
|
||||
|
||||
# ── Stage 1: compile soul-demo ────────────────────────────────────────────────
|
||||
FROM debian:bookworm-slim AS builder
|
||||
FROM ubuntu:24.04
|
||||
|
||||
ENV DEBIAN_FRONTEND=noninteractive
|
||||
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends \
|
||||
build-essential \
|
||||
curl \
|
||||
libcurl4-openssl-dev \
|
||||
libssl-dev \
|
||||
ca-certificates \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
WORKDIR /build
|
||||
|
||||
COPY runtime/el_runtime.c runtime/el_runtime.h ./
|
||||
|
||||
# Pre-compile el_runtime as a separate cached layer.
|
||||
# el_runtime.c changes rarely; main.c changes every run.
|
||||
# Splitting this out means el_runtime.o is cached across builds when only main.c changes.
|
||||
# -DHAVE_CURL: the staged el_runtime.c (from el.git) guards the OTLP observability
|
||||
# section (emit_metric, emit_log, trace_span_*) behind #ifdef HAVE_CURL.
|
||||
# libcurl IS installed above, so define HAVE_CURL to enable those functions.
|
||||
RUN cc -O2 -DHAVE_CURL -c el_runtime.c -I. -o el_runtime.o
|
||||
|
||||
COPY dist/soul-demo.c dist/vessel_stubs.c ./
|
||||
|
||||
RUN cc -O2 -rdynamic \
|
||||
-DEL_SOUL_DEMO_BUILD \
|
||||
-o soul-demo \
|
||||
soul-demo.c vessel_stubs.c el_runtime.o \
|
||||
-lcurl -lpthread -ldl -lm -lssl -lcrypto
|
||||
|
||||
# ── Download k3s binary ───────────────────────────────────────────────────────
|
||||
RUN curl -fL --retry 3 --retry-delay 10 https://github.com/k3s-io/k3s/releases/download/v1.32.4%2Bk3s1/k3s -o /usr/local/bin/k3s \
|
||||
&& chmod +x /usr/local/bin/k3s
|
||||
|
||||
# ── Stage 2: runtime image ────────────────────────────────────────────────────
|
||||
FROM debian:bookworm-slim
|
||||
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends \
|
||||
libcurl4 \
|
||||
libssl3 \
|
||||
libcurl4t64 \
|
||||
libssl3t64 \
|
||||
ca-certificates \
|
||||
&& rm -rf /var/lib/apt/lists/* \
|
||||
&& groupadd -r landing && useradd -r -g landing landing \
|
||||
&& mkdir -p /srv/landing/assets /srv/landing/js /srv/landing/shares \
|
||||
&& mkdir -p /srv/soul/engram-demo \
|
||||
&& chown -R landing:landing /srv/landing /srv/soul \
|
||||
&& mkdir -p /var/lib/rancher/k3s /tmp/k3s \
|
||||
&& chown -R landing:landing /var/lib/rancher /tmp/k3s
|
||||
&& chown -R landing:landing /srv/landing /srv/soul
|
||||
|
||||
# neuron-web binary — produced by `elb build` in CI (linux/amd64)
|
||||
COPY dist/neuron-landing /usr/local/bin/neuron-web
|
||||
RUN chmod +x /usr/local/bin/neuron-web
|
||||
|
||||
COPY --from=builder /build/soul-demo /usr/local/bin/soul-demo
|
||||
|
||||
# k3s binary
|
||||
COPY --from=builder /usr/local/bin/k3s /usr/local/bin/k3s
|
||||
|
||||
# soul-demo OCI image tar — k3s imports this at startup (no registry needed)
|
||||
RUN mkdir -p /var/lib/rancher/k3s/agent/images
|
||||
COPY dist/soul-demo-image.tar /var/lib/rancher/k3s/agent/images/soul-demo.tar
|
||||
|
||||
# k3s manifests — auto-applied when k3s starts
|
||||
RUN mkdir -p /var/lib/rancher/k3s/server/manifests
|
||||
COPY dist/k3s-soul-demo.yaml /var/lib/rancher/k3s/server/manifests/soul-demo.yaml
|
||||
# soul-demo binary — compiled by cc on host runner in CI
|
||||
COPY dist/soul-demo /usr/local/bin/soul-demo
|
||||
RUN chmod +x /usr/local/bin/soul-demo
|
||||
|
||||
# Engram snapshot — baked in so soul has memory from cold start
|
||||
COPY dist/engram-snapshot.json /srv/soul/engram-demo/snapshot.json
|
||||
@@ -102,11 +57,7 @@ ENV LANDING_ROOT=/srv/landing
|
||||
ENV PORT=8080
|
||||
ENV NEURON_HOME=/srv/soul/engram-demo
|
||||
ENV NEURON_PORT=7772
|
||||
ENV K3S_DATA_DIR=/var/lib/rancher/k3s
|
||||
ENV KUBECONFIG=/var/lib/rancher/k3s/server/cred/admin.kubeconfig
|
||||
|
||||
# k3s requires root to create network namespaces and mount cgroups.
|
||||
# Cloud Run gen2 sandbox is the security boundary here.
|
||||
EXPOSE 8080
|
||||
|
||||
CMD ["/usr/local/bin/entrypoint.sh"]
|
||||
|
||||
Vendored
+16
-33
@@ -1,43 +1,26 @@
|
||||
#!/bin/sh
|
||||
set -e
|
||||
|
||||
# SKIP_K3S=1 — bypass k3s/soul-demo startup and go straight to neuron-web.
|
||||
# Used by the dev CI smoke test where the container runtime doesn't support
|
||||
# the kernel capabilities k3s requires (overlayfs / privileged mode).
|
||||
if [ "${SKIP_K3S:-0}" = "1" ]; then
|
||||
echo "[entrypoint] SKIP_K3S=1: starting neuron-web directly (no k3s/soul-demo)."
|
||||
echo "[entrypoint] SKIP_K3S=1: starting neuron-web directly (no soul-demo)."
|
||||
exec /usr/local/bin/neuron-web
|
||||
fi
|
||||
|
||||
echo "[entrypoint] Starting k3s server (embedded soul-demo orchestrator)..."
|
||||
|
||||
# k3s server — single-node mode, disable unused components
|
||||
# --disable traefik,servicelb: we don't need an ingress or LB
|
||||
# --disable metrics-server: saves ~50MB RAM
|
||||
# --write-kubeconfig-mode=644: allow non-root reads
|
||||
# --data-dir: use the pre-chowned dir
|
||||
k3s server \
|
||||
--disable traefik \
|
||||
--disable servicelb \
|
||||
--disable metrics-server \
|
||||
--write-kubeconfig-mode=644 \
|
||||
--data-dir /var/lib/rancher/k3s \
|
||||
--node-name soul-node &
|
||||
|
||||
K3S_PID=$!
|
||||
|
||||
echo "[entrypoint] Waiting for k3s to become ready..."
|
||||
until k3s kubectl get nodes --no-headers 2>/dev/null | grep -q "Ready"; do
|
||||
sleep 2
|
||||
done
|
||||
echo "[entrypoint] k3s ready. soul-demo Deployment will be applied automatically from manifests."
|
||||
|
||||
# Wait for soul-demo pod to be Running before starting neuron-web
|
||||
echo "[entrypoint] Waiting for soul-demo pod..."
|
||||
until k3s kubectl get pods -l app=soul-demo --no-headers 2>/dev/null | grep -q "Running"; do
|
||||
sleep 3
|
||||
done
|
||||
echo "[entrypoint] soul-demo is running."
|
||||
# Soul-demo watchdog: start soul-demo and restart it automatically on crash.
|
||||
# Cloud Run gen2 doesn't reliably provide eth0 with a unicast IP, so k3s flannel
|
||||
# fails at startup. Running soul-demo directly is simpler, lighter, and fully
|
||||
# self-healing. Cloud Run handles horizontal scaling — no HPA needed.
|
||||
echo "[entrypoint] Starting soul-demo watchdog on :${NEURON_PORT:-7772}..."
|
||||
(
|
||||
while true; do
|
||||
echo "[soul-watchdog] starting soul-demo (NEURON_HOME=${NEURON_HOME})"
|
||||
/usr/local/bin/soul-demo 2>&1 || true
|
||||
echo "[soul-watchdog] soul-demo exited, restarting in 3s..."
|
||||
sleep 3
|
||||
done
|
||||
) &
|
||||
|
||||
# Start neuron-web immediately — do NOT block.
|
||||
# Cloud Run startup probe requires port 8080 to answer within the timeout.
|
||||
echo "[entrypoint] Starting neuron-web on port ${PORT:-8080}..."
|
||||
exec /usr/local/bin/neuron-web
|
||||
|
||||
Vendored
+25
@@ -6,6 +6,31 @@
|
||||
#include <unistd.h>
|
||||
#include "el_runtime.h"
|
||||
|
||||
/* Pre-register the El HTTP handler so it is found by http_lookup_active()
|
||||
* regardless of whether the binary was linked with -rdynamic.
|
||||
*
|
||||
* el_runtime's http_set_handler resolves handler names via:
|
||||
* dlsym(RTLD_DEFAULT, "handle_request")
|
||||
* but dlsym only searches the dynamic symbol table, which only contains
|
||||
* user-defined symbols when the executable is linked with -rdynamic.
|
||||
* elb does not add -rdynamic, so dlsym returns NULL and routes return
|
||||
* "el-runtime: no http handler registered" even though http_serve is called.
|
||||
*
|
||||
* The fix: forward-declare handle_request here and register it directly
|
||||
* via el_runtime_register_handler before main() runs. This populates the
|
||||
* handler registry so http_lookup_active() finds it without needing dlsym.
|
||||
*/
|
||||
extern el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body);
|
||||
/* el_runtime_register_handler is intentionally not declared in el_runtime.h
|
||||
* ("extern lookup works since C symbols are global" — runtime comment). */
|
||||
extern void el_runtime_register_handler(const char* name,
|
||||
el_val_t (*fn)(el_val_t, el_val_t, el_val_t));
|
||||
|
||||
__attribute__((constructor))
|
||||
static void pre_register_http_handlers(void) {
|
||||
el_runtime_register_handler("handle_request", handle_request);
|
||||
}
|
||||
|
||||
el_val_t http_get_auth(el_val_t url, el_val_t tok) {
|
||||
char bearer[2048]; snprintf(bearer, sizeof(bearer), "Bearer %s", EL_CSTR(tok));
|
||||
el_val_t hdr_val = EL_STR(bearer);
|
||||
|
||||
@@ -16,4 +16,12 @@ build {
|
||||
c_source "dist/page_css.c"
|
||||
c_source "dist/page_ga.c"
|
||||
c_source "dist/page_schema.c"
|
||||
// NOTE: neuron-web requires el_runtime.c to be compiled with -DHAVE_CURL
|
||||
// so that http_get/http_post forward to libcurl instead of returning
|
||||
// {"error":"not built with HAVE_CURL"}. The elb binary in ci-base:dev
|
||||
// hardcodes -DHAVE_CURL in its cc invocation, but older elb versions may
|
||||
// not. manifest.el does not support c_flags or link_flags directives —
|
||||
// if upgrading elb breaks HTTP, ensure ci-base:dev ships an elb built
|
||||
// with HAVE_CURL enabled in its hardcoded cc command, or pre-compile
|
||||
// el_runtime.o with -DHAVE_CURL on the host and pass it as a c_source.
|
||||
}
|
||||
|
||||
+98
-30
@@ -1331,12 +1331,19 @@ static void http_emit_headers_from_map(JsonBuf* b, el_val_t headers_map,
|
||||
|
||||
/* Parse the envelope produced by http_response(). On success returns 1 and
|
||||
* populates *out_status, *out_headers_map (an ElMap el_val_t — caller must
|
||||
* el_release), and *out_body (allocated). On failure returns 0.
|
||||
* el_release via out_parsed_root), and *out_body (malloc'd, caller frees).
|
||||
* On failure returns 0.
|
||||
*
|
||||
* Implementation: feeds the entire envelope through the recursive-descent
|
||||
* JSON parser (which builds proper ElMap/ElList values), then pulls the
|
||||
* three top-level fields by name. Avoids re-stringifying the headers map
|
||||
* since json_stringify() does not support nested objects. */
|
||||
* Implementation: manual field scanner — does NOT run json_parse on the full
|
||||
* envelope. Running the recursive-descent JSON parser on a 40–50 KB envelope
|
||||
* (common when the body contains minified/obfuscated JavaScript) fails because
|
||||
* the parser allocates intermediate ElMap nodes for the whole structure.
|
||||
* Instead we scan directly:
|
||||
* • "status" — strtol scan
|
||||
* • "headers" — brace-depth scan to extract the object literal, then
|
||||
* json_parse only that small substring (always < 1 KB)
|
||||
* • "body" — jp_parse_string_raw to unescape the JSON string in one pass,
|
||||
* without building any intermediate data structures */
|
||||
static int http_parse_envelope(const char* s, int* out_status,
|
||||
el_val_t* out_headers_map, char** out_body,
|
||||
el_val_t* out_parsed_root) {
|
||||
@@ -1344,37 +1351,91 @@ static int http_parse_envelope(const char* s, int* out_status,
|
||||
if (strncmp(s, EL_HTTP_RESPONSE_TAG,
|
||||
sizeof(EL_HTTP_RESPONSE_TAG) - 1) != 0) return 0;
|
||||
|
||||
el_val_t parsed = json_parse(EL_STR(s));
|
||||
if (parsed == EL_NULL) return 0;
|
||||
|
||||
int status = 200;
|
||||
el_val_t hmap = 0;
|
||||
char* body = NULL;
|
||||
|
||||
el_val_t sv = el_map_get(parsed, EL_STR("status"));
|
||||
if (sv != 0) {
|
||||
/* status comes back as an integer — el_val_t holds it directly. */
|
||||
long sc = (long)sv;
|
||||
if (sc >= 100 && sc <= 599) status = (int)sc;
|
||||
/* ── status ──────────────────────────────────────────────────────────── */
|
||||
int status = 200;
|
||||
{
|
||||
const char* sp = strstr(s, "\"status\":");
|
||||
if (sp) {
|
||||
const char* np = sp + 9;
|
||||
while (*np == ' ' || *np == '\t') np++;
|
||||
long sc = strtol(np, NULL, 10);
|
||||
if (sc >= 100 && sc <= 599) status = (int)sc;
|
||||
}
|
||||
}
|
||||
|
||||
el_val_t hv = el_map_get(parsed, EL_STR("headers"));
|
||||
if (hv != 0) {
|
||||
ElMap* hm = (ElMap*)(uintptr_t)hv;
|
||||
if (hm && hm->hdr.magic == EL_MAGIC_MAP) hmap = hv;
|
||||
/* ── headers ─────────────────────────────────────────────────────────── */
|
||||
el_val_t hmap = 0;
|
||||
el_val_t parsed_hdrs = EL_NULL;
|
||||
{
|
||||
const char* hp = strstr(s, "\"headers\":");
|
||||
if (hp) {
|
||||
hp += 10;
|
||||
while (*hp == ' ' || *hp == '\t') hp++;
|
||||
if (*hp == '{') {
|
||||
/* Scan for matching '}', honouring nested objects and strings */
|
||||
const char* hobj_start = hp;
|
||||
const char* cp = hp + 1;
|
||||
int depth = 1, in_str = 0;
|
||||
while (*cp && depth > 0) {
|
||||
if (in_str) {
|
||||
if (*cp == '\\' && *(cp + 1)) { cp += 2; continue; }
|
||||
if (*cp == '"') in_str = 0;
|
||||
} else {
|
||||
if (*cp == '"') in_str = 1;
|
||||
else if (*cp == '{') depth++;
|
||||
else if (*cp == '}') { if (--depth == 0) break; }
|
||||
}
|
||||
cp++;
|
||||
}
|
||||
if (depth == 0) {
|
||||
/* cp points at the closing '}'; extract the object literal */
|
||||
size_t hlen = (size_t)(cp - hobj_start + 1);
|
||||
char* hobj = malloc(hlen + 1);
|
||||
if (hobj) {
|
||||
memcpy(hobj, hobj_start, hlen);
|
||||
hobj[hlen] = '\0';
|
||||
/* Headers are always simple k/v string pairs — json_parse
|
||||
* is safe on this small substring (typically < 1 KB). */
|
||||
parsed_hdrs = json_parse(EL_STR(hobj));
|
||||
free(hobj);
|
||||
if (parsed_hdrs != EL_NULL) {
|
||||
ElMap* hm = (ElMap*)(uintptr_t)parsed_hdrs;
|
||||
if (hm && hm->hdr.magic == EL_MAGIC_MAP) hmap = parsed_hdrs;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
el_val_t bv = el_map_get(parsed, EL_STR("body"));
|
||||
if (bv != 0) {
|
||||
const char* bs = EL_CSTR(bv);
|
||||
if (bs) body = el_strdup(bs);
|
||||
/* ── body ────────────────────────────────────────────────────────────── */
|
||||
/* Search forward so we don't accidentally match "body": inside a header
|
||||
* value. http_response() always appends the body field last. */
|
||||
char* body = NULL;
|
||||
{
|
||||
const char* bp = strstr(s, "\"body\":");
|
||||
if (bp) {
|
||||
bp += 7;
|
||||
while (*bp == ' ' || *bp == '\t') bp++;
|
||||
if (*bp == '"') {
|
||||
/* jp_parse_string_raw unescapes a JSON string in one pass,
|
||||
* producing a plain malloc'd C string. Caller frees it. */
|
||||
JsonParser jp = { .p = bp, .end = bp + strlen(bp), .err = 0 };
|
||||
char* parsed = jp_parse_string_raw(&jp);
|
||||
if (!jp.err) {
|
||||
body = parsed;
|
||||
} else {
|
||||
free(parsed);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!body) body = strdup("");
|
||||
}
|
||||
if (!body) body = el_strdup("");
|
||||
|
||||
*out_status = status;
|
||||
*out_headers_map = hmap;
|
||||
*out_body = body;
|
||||
*out_parsed_root = parsed; /* caller releases to free hmap + entries */
|
||||
*out_status = status;
|
||||
*out_headers_map = hmap;
|
||||
*out_body = body;
|
||||
*out_parsed_root = parsed_hdrs; /* caller el_release()s to free hmap */
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -1900,6 +1961,13 @@ el_val_t http_response(el_val_t status, el_val_t headers_json, el_val_t body) {
|
||||
const char* b = EL_CSTR(body);
|
||||
if (!b) b = "";
|
||||
|
||||
/* Clear the fs_read binary-length hint: the envelope we're about to build
|
||||
* is a fresh JSON string, not the raw file bytes. Without this reset,
|
||||
* http_worker would use the stale _tl_fs_read_len (= original file size)
|
||||
* to copy the response — truncating the larger envelope before it reaches
|
||||
* http_send_response and http_parse_envelope. */
|
||||
_tl_fs_read_len = 0;
|
||||
|
||||
JsonBuf out; jb_init(&out);
|
||||
jb_puts(&out, EL_HTTP_RESPONSE_TAG); /* {"el_http_response":1 */
|
||||
jb_puts(&out, ",\"status\":");
|
||||
|
||||
+18
-2
@@ -29,12 +29,19 @@ fn main() -> Void {
|
||||
el.style.color = isError ? '#c0392b' : '#2ecc71';
|
||||
}
|
||||
|
||||
var _formRevealed = false;
|
||||
function revealPaymentForm(user) {
|
||||
if (_formRevealed) return;
|
||||
_formRevealed = true;
|
||||
if (user && user.id) { window._neuronSupaId = user.id; }
|
||||
var auth = document.getElementById('auth-section');
|
||||
if (auth) auth.style.display = 'none';
|
||||
var isFree = (window.NEURON_CFG || {}).plan === 'free';
|
||||
if (!isFree) {
|
||||
if (isFree) {
|
||||
// Free plan: show the success panel (user is signed in or just signed up)
|
||||
var freeSuccess = document.getElementById('free-success');
|
||||
if (freeSuccess) freeSuccess.style.display = '';
|
||||
} else {
|
||||
var payment = document.getElementById('payment-section');
|
||||
if (payment) payment.style.display = '';
|
||||
}
|
||||
@@ -68,7 +75,16 @@ fn main() -> Void {
|
||||
function checkExistingSession() {
|
||||
initSupabase(function() {
|
||||
supabaseClient.auth.getUser().then(function(res) {
|
||||
if (res.data && res.data.user) { revealPaymentForm(res.data.user); }
|
||||
if (res.data && res.data.user) {
|
||||
revealPaymentForm(res.data.user);
|
||||
} else {
|
||||
// No existing session — for paid plans, init Stripe immediately.
|
||||
// Auth is optional on paid plans; the user can link their account later.
|
||||
var isFree = (window.NEURON_CFG || {}).plan === 'free';
|
||||
if (!isFree && typeof window.initStripe === 'function') {
|
||||
window.initStripe('', '');
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
+18
-1
@@ -907,6 +907,10 @@ fn handle_request_inner(method: String, path: String, body: String) -> String {
|
||||
// ── Compiled client-side JS: /js/* ───────────────────────────────────────
|
||||
// Served from dist/js/ (compiled by elc --target=js at build time).
|
||||
// LANDING_ROOT/js maps to the dist/js output directory in the image.
|
||||
// Returns an http_response envelope with explicit Content-Type so the
|
||||
// browser executes the file as JavaScript — http_detect_content_type()
|
||||
// mis-identifies minified/obfuscated JS as JSON because many obfuscated
|
||||
// bundles start with '[' (which is also a JSON array opener).
|
||||
if str_starts_with(path, "/js/") {
|
||||
let rel: String = str_slice(path, 4, str_len(path))
|
||||
let abs: String = src_dir + "/js/" + rel
|
||||
@@ -914,7 +918,7 @@ fn handle_request_inner(method: String, path: String, body: String) -> String {
|
||||
if str_eq(content, "") {
|
||||
return "{\"__status__\":404,\"error\":\"not found\"}"
|
||||
}
|
||||
return content
|
||||
return http_response(200, js_headers_json(), content)
|
||||
}
|
||||
|
||||
// ── Brand assets: /brand/* ────────────────────────────────────────────────
|
||||
@@ -1936,6 +1940,19 @@ fn sec_headers_json() -> String {
|
||||
+ "\"Content-Security-Policy\":\"default-src 'self'; script-src 'self' 'unsafe-inline' https://challenges.cloudflare.com https://cdn.jsdelivr.net https://www.googletagmanager.com https://www.google-analytics.com; style-src 'self' 'unsafe-inline'; frame-src https://challenges.cloudflare.com; connect-src 'self' https://api.stripe.com https://*.supabase.co; img-src 'self' data: https:; font-src 'self' data:\"}"
|
||||
}
|
||||
|
||||
// Headers for compiled JS assets. Explicitly sets Content-Type so the browser
|
||||
// treats them as JavaScript regardless of what http_detect_content_type()
|
||||
// infers from the content (minified/obfuscated JS can trip the JSON heuristic).
|
||||
fn js_headers_json() -> String {
|
||||
"{\"Content-Type\":\"application/javascript; charset=utf-8\","
|
||||
+ "\"Cache-Control\":\"public, max-age=3600\","
|
||||
+ "\"Strict-Transport-Security\":\"max-age=63072000; includeSubDomains; preload\","
|
||||
+ "\"X-Content-Type-Options\":\"nosniff\","
|
||||
+ "\"X-Frame-Options\":\"SAMEORIGIN\","
|
||||
+ "\"Referrer-Policy\":\"strict-origin-when-cross-origin\","
|
||||
+ "\"Permissions-Policy\":\"geolocation=(), microphone=(), camera=()\"}"
|
||||
}
|
||||
|
||||
fn handle_request(method: String, path: String, body: String) -> String {
|
||||
let inner_resp: String = handle_request_inner(method, path, body)
|
||||
// Detect envelope already set by inner handler (starts with
|
||||
|
||||
Reference in New Issue
Block a user