Files
moloapp/example/example.mk
T
retoorandClaude Haiku 5.5 a2a737e21d fix: bind living example on all interfaces, default port 8000
The example server bound 127.0.0.1 only, so it was unreachable from
other hosts, and defaulted to 8080. It now binds 0.0.0.0 on 8000
(README, Makefile deploy and locust targets updated to match).

Also: a leading --flag no longer gets parsed as the port, so
'living-server --no-locust' starts instead of exiting with 'bad port'.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
2026-10-26 04:15:09 +01:00

170 lines
7.1 KiB
Makefile

# example/example.mk: living example part (plan 10-workflow-example.md).
# Included by the root Makefile through per-part subdir .mk includes, or run
# standalone from the repo root:
# make -f example/example.mk example # build lib + server
# make -f example/example.mk example-test # build + run example suite
# make -f example/example.mk example-clean # remove build/example
# make -f example/example.mk example-locust # headless locust run
# Portable make: variables plus sh loops only, no GNU-only functions.
CXX ?= g++
EXAMPLE_CXXFLAGS ?= -std=c++23 -Wall -Wextra -O1
EXAMPLE_DIR ?= example
EXAMPLE_BUILD ?= build/example
EXAMPLE_TESTDIR ?= tests/example
EXAMPLE_SRCS = \
$(EXAMPLE_DIR)/fw/Http.cpp \
$(EXAMPLE_DIR)/fw/Router.cpp \
$(EXAMPLE_DIR)/fw/App.cpp \
$(EXAMPLE_DIR)/fw/View.cpp \
$(EXAMPLE_DIR)/fw/Validate.cpp \
$(EXAMPLE_DIR)/fw/Templates.cpp \
$(EXAMPLE_DIR)/fw/Negotiate.cpp \
$(EXAMPLE_DIR)/fw/Cookies.cpp \
$(EXAMPLE_DIR)/fw/StaticFiles.cpp \
$(EXAMPLE_DIR)/fw/Tls.cpp \
$(EXAMPLE_DIR)/fw/Kv.cpp \
$(EXAMPLE_DIR)/fw/Stats.cpp \
$(EXAMPLE_DIR)/fw/SecurityExtra.cpp \
$(EXAMPLE_DIR)/fw/Platform.cpp \
$(EXAMPLE_DIR)/fw/Enterprise.cpp \
$(EXAMPLE_DIR)/fw/Resilience.cpp \
$(EXAMPLE_DIR)/fw/Observ.cpp \
$(EXAMPLE_DIR)/fw/Data.cpp \
$(EXAMPLE_DIR)/fw/Jobs.cpp \
$(EXAMPLE_DIR)/fw/Net.cpp \
$(EXAMPLE_DIR)/fw/Workflow.cpp \
$(EXAMPLE_DIR)/fw/TestClient.cpp \
$(EXAMPLE_DIR)/fw/Server.cpp \
$(EXAMPLE_DIR)/app/App.cpp \
$(EXAMPLE_DIR)/app/Seed.cpp \
$(EXAMPLE_DIR)/app/Templates.cpp \
$(EXAMPLE_DIR)/app/Embedded.cpp \
$(EXAMPLE_DIR)/app/Index.cpp \
$(EXAMPLE_DIR)/app/RoutingDemo.cpp \
$(EXAMPLE_DIR)/app/DataDemo.cpp \
$(EXAMPLE_DIR)/app/SecurityDemo.cpp \
$(EXAMPLE_DIR)/app/FormatsDemo.cpp \
$(EXAMPLE_DIR)/app/RealtimeDemo.cpp \
$(EXAMPLE_DIR)/app/OpsDemo.cpp \
$(EXAMPLE_DIR)/app/StatsDemo.cpp \
$(EXAMPLE_DIR)/app/SecurityExtraDemo.cpp \
$(EXAMPLE_DIR)/app/PlatformDemo.cpp \
$(EXAMPLE_DIR)/app/EnterpriseDemo.cpp \
$(EXAMPLE_DIR)/app/ResilienceDemo.cpp \
$(EXAMPLE_DIR)/app/VoiceDemo.cpp \
$(sort $(wildcard $(EXAMPLE_DIR)/app/agent/*.cpp)) \
$(sort $(wildcard $(EXAMPLE_DIR)/app/ai/*.cpp)) \
$(sort $(wildcard $(EXAMPLE_DIR)/app/google/*.cpp))
# Tests are discovered: every tests/example/**/*_test.cpp is one route test.
EXAMPLE_TEST_JOBS ?= 8
EXAMPLE_GEN = $(EXAMPLE_BUILD)/gen
EXAMPLE_EMBED_SRCS = \
src/cli/embed/asset_scanner.cpp \
src/cli/embed/embed_emitter.cpp \
src/cli/embed/gzip_stored.cpp \
src/cli/embed/mime.cpp \
src/cli/embed/sha256.cpp
# Every file in example/assets is embedded (agent modules add agent_<module>.js).
EXAMPLE_ASSETS = $(sort $(wildcard $(EXAMPLE_DIR)/assets/*))
EXAMPLE_INC = -I$(EXAMPLE_DIR) -I$(EXAMPLE_BUILD) -Isrc
# Real framework parts the example links: Google Cloud (src/google) over the
# HTTP client (src/clients, wolfSSL TLS) and the operability breaker.
EXAMPLE_REAL_LIBS = build/ai/libai.a build/google/libgoogle.a build/clients/libclients.a \
build/liboperability.a
.PHONY: example-deploy example-verify deploy verify example example-real-libs example-assets example-test example-clean example-locust
# Asset embedding is part of the build: example/assets (Chart.js included)
# becomes generated C++ under build/example/gen via the MoloApp embedder.
example-assets: $(EXAMPLE_GEN)/assets.cpp
$(EXAMPLE_GEN)/assets.cpp: $(EXAMPLE_ASSETS) $(EXAMPLE_DIR)/tools/embed_assets.cpp \
$(EXAMPLE_EMBED_SRCS)
mkdir -p $(EXAMPLE_GEN)
$(CXX) $(EXAMPLE_CXXFLAGS) -Isrc/cli -Isrc \
$(EXAMPLE_DIR)/tools/embed_assets.cpp $(EXAMPLE_EMBED_SRCS) \
-o $(EXAMPLE_BUILD)/embed_assets
$(EXAMPLE_BUILD)/embed_assets $(EXAMPLE_DIR)/assets \
$(EXAMPLE_GEN)/assets.cpp $(EXAMPLE_GEN)/assets.hpp molo::example::gen
example-real-libs:
$(MAKE) -f src/operability.mk build/liboperability.a
$(MAKE) -f src/clients/clients.mk clients
$(MAKE) -f src/google/google.mk google
$(MAKE) -f src/ai.mk ai
example: example-assets example-real-libs
mkdir -p $(EXAMPLE_BUILD)/obj
for s in $(EXAMPLE_SRCS); do \
o=$(EXAMPLE_BUILD)/obj/`echo $$s | tr '/' '_'`.o; \
$(CXX) $(EXAMPLE_CXXFLAGS) $(EXAMPLE_INC) -c $$s -o $$o || exit 1; \
done
$(CXX) $(EXAMPLE_CXXFLAGS) -c $(EXAMPLE_GEN)/assets.cpp \
-o $(EXAMPLE_BUILD)/obj/gen_assets.o
ar rcs $(EXAMPLE_BUILD)/libexample.a $(EXAMPLE_BUILD)/obj/*.o
$(MAKE) -f src/supervisor/supervisor.mk build/supervisor/libsupervisor.a
$(CXX) $(EXAMPLE_CXXFLAGS) -I$(EXAMPLE_DIR) -Isrc $(EXAMPLE_DIR)/main.cpp \
$(EXAMPLE_BUILD)/libexample.a build/supervisor/libsupervisor.a \
$(EXAMPLE_REAL_LIBS) -lwolfssl -o $(EXAMPLE_BUILD)/living-server
example-test: example
rm -rf $(EXAMPLE_BUILD)/tests
mkdir -p $(EXAMPLE_BUILD)/tests
find $(EXAMPLE_TESTDIR) -name '*_test.cpp' | sort > $(EXAMPLE_BUILD)/tests/list.txt
xargs -P $(EXAMPLE_TEST_JOBS) -I{} sh -c ' \
t={}; n=`echo $$t | sed "s|$(EXAMPLE_TESTDIR)/||; s|/|_|g; s|\\.cpp$$||"`; \
b=$(EXAMPLE_BUILD)/tests/$$n; \
if $(CXX) $(EXAMPLE_CXXFLAGS) -I$(EXAMPLE_DIR) -I$(EXAMPLE_TESTDIR) -Isrc \
$$t $(EXAMPLE_BUILD)/libexample.a $(EXAMPLE_REAL_LIBS) -lwolfssl -o $$b > $$b.log 2>&1; \
then MOLO_ENV_FILES=off $$b >> $$b.log 2>&1; else echo "COMPILE FAILED: $$t" >> $$b.log; false; fi; \
echo $$? > $$b.rc' < $(EXAMPLE_BUILD)/tests/list.txt
pass=0; fail=0; \
for l in `ls $(EXAMPLE_BUILD)/tests/*.log | sort`; do \
if [ "`cat $${l%.log}.rc`" = 0 ]; then pass=$$((pass+1)); \
else fail=$$((fail+1)); echo "FAIL: $$l"; cat $$l; fi; \
done; \
echo "example suite: $$pass passed, $$fail failed"; \
test $$fail -eq 0
example-locust:
python3 $(EXAMPLE_DIR)/check_locust_coverage.py
locust -f $(EXAMPLE_DIR)/locustfile.py --host $${LOCUST_HOST:-http://127.0.0.1:8000} --headless -u $${LOCUST_USERS:-10} -r $${LOCUST_RATE:-2} -t $${LOCUST_TIME:-30s}
example-clean:
rm -rf $(EXAMPLE_BUILD)
# Deploy: loopback single binary (AGENTS.md "Deploy"). Restarts the server from
# build/ with a pid file; fails if it is not healthy within 15 s. Runs from
# the repo root so .env.json(.local) and example/static resolve.
EXAMPLE_PORT ?= 8000
EXAMPLE_RUN = $(EXAMPLE_BUILD)/run
example-deploy: example
mkdir -p $(EXAMPLE_RUN)
if [ -f $(EXAMPLE_RUN)/server.pid ] && kill -0 `cat $(EXAMPLE_RUN)/server.pid` 2>/dev/null; then \
kill `cat $(EXAMPLE_RUN)/server.pid`; \
for i in 1 2 3 4 5 6 7 8 9 10; do kill -0 `cat $(EXAMPLE_RUN)/server.pid` 2>/dev/null || break; sleep 0.5; done; \
fi
setsid nohup $(EXAMPLE_BUILD)/living-server $(EXAMPLE_PORT) --no-locust \
> $(EXAMPLE_RUN)/server.log 2>&1 < /dev/null & echo $$! > $(EXAMPLE_RUN)/server.pid
ok=0; for i in `seq 1 30`; do \
if curl -fsS http://127.0.0.1:$(EXAMPLE_PORT)/health > /dev/null 2>&1; then ok=1; break; fi; sleep 0.5; \
done; \
if [ $$ok -ne 1 ]; then echo "deploy: server not healthy in 15 s"; cat $(EXAMPLE_RUN)/server.log; exit 1; fi; \
echo "deployed: http://127.0.0.1:$(EXAMPLE_PORT) pid `cat $(EXAMPLE_RUN)/server.pid`"
example-verify:
python3 -I tools/verify_deploy.py http://127.0.0.1:$(EXAMPLE_PORT)
deploy: example-deploy
verify: example-verify
# Root Makefile aggregation points (double-colon: safe with other parts).
all:: example
test:: example-test
clean:: example-clean