2026-10-23 18:20:04 +02:00
|
|
|
# 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
|
2026-10-24 03:10:28 +02:00
|
|
|
# make -f example/example.mk example-locust # headless locust run
|
2026-10-23 18:20:04 +02:00
|
|
|
# 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 \
|
2026-10-24 01:59:27 +02:00
|
|
|
$(EXAMPLE_DIR)/fw/Stats.cpp \
|
2026-10-24 02:19:52 +02:00
|
|
|
$(EXAMPLE_DIR)/fw/SecurityExtra.cpp \
|
|
|
|
|
$(EXAMPLE_DIR)/fw/Platform.cpp \
|
|
|
|
|
$(EXAMPLE_DIR)/fw/Enterprise.cpp \
|
|
|
|
|
$(EXAMPLE_DIR)/fw/Resilience.cpp \
|
2026-10-23 18:20:04 +02:00
|
|
|
$(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 \
|
2026-11-06 04:36:00 +01:00
|
|
|
$(EXAMPLE_DIR)/app/IndexCatalog.cpp \
|
2026-10-23 18:20:04 +02:00
|
|
|
$(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 \
|
2026-10-24 01:59:27 +02:00
|
|
|
$(EXAMPLE_DIR)/app/OpsDemo.cpp \
|
2026-11-06 04:36:00 +01:00
|
|
|
$(EXAMPLE_DIR)/app/DoosDemo.cpp \
|
2026-10-24 02:19:52 +02:00
|
|
|
$(EXAMPLE_DIR)/app/StatsDemo.cpp \
|
|
|
|
|
$(EXAMPLE_DIR)/app/SecurityExtraDemo.cpp \
|
|
|
|
|
$(EXAMPLE_DIR)/app/PlatformDemo.cpp \
|
|
|
|
|
$(EXAMPLE_DIR)/app/EnterpriseDemo.cpp \
|
2026-10-24 04:05:41 +02:00
|
|
|
$(EXAMPLE_DIR)/app/ResilienceDemo.cpp \
|
2026-10-24 04:12:52 +02:00
|
|
|
$(EXAMPLE_DIR)/app/VoiceDemo.cpp \
|
2026-10-24 06:06:06 +02:00
|
|
|
$(sort $(wildcard $(EXAMPLE_DIR)/app/agent/*.cpp)) \
|
2026-10-25 09:14:26 +01:00
|
|
|
$(sort $(wildcard $(EXAMPLE_DIR)/app/ai/*.cpp)) \
|
|
|
|
|
$(sort $(wildcard $(EXAMPLE_DIR)/app/google/*.cpp))
|
2026-10-23 18:20:04 +02:00
|
|
|
|
2026-10-24 01:59:27 +02:00
|
|
|
# Tests are discovered: every tests/example/**/*_test.cpp is one route test.
|
|
|
|
|
EXAMPLE_TEST_JOBS ?= 8
|
2026-10-23 18:20:04 +02:00
|
|
|
|
2026-10-24 01:59:27 +02:00
|
|
|
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
|
2026-10-24 04:12:52 +02:00
|
|
|
# Every file in example/assets is embedded (agent modules add agent_<module>.js).
|
|
|
|
|
EXAMPLE_ASSETS = $(sort $(wildcard $(EXAMPLE_DIR)/assets/*))
|
2026-10-24 04:05:41 +02:00
|
|
|
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.
|
2026-10-24 06:06:06 +02:00
|
|
|
EXAMPLE_REAL_LIBS = build/ai/libai.a build/google/libgoogle.a build/clients/libclients.a \
|
2026-11-06 04:36:00 +01:00
|
|
|
build/liboperability.a build/doos/libdoos.a
|
2026-10-23 18:20:04 +02:00
|
|
|
|
2026-10-24 05:06:13 +02:00
|
|
|
.PHONY: example-deploy example-verify deploy verify example example-real-libs example-assets example-test example-clean example-locust
|
2026-10-24 01:59:27 +02:00
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
2026-10-24 04:05:41 +02:00
|
|
|
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
|
2026-10-24 06:06:06 +02:00
|
|
|
$(MAKE) -f src/ai.mk ai
|
2026-10-24 04:05:41 +02:00
|
|
|
|
|
|
|
|
example: example-assets example-real-libs
|
2026-10-23 18:20:04 +02:00
|
|
|
mkdir -p $(EXAMPLE_BUILD)/obj
|
|
|
|
|
for s in $(EXAMPLE_SRCS); do \
|
|
|
|
|
o=$(EXAMPLE_BUILD)/obj/`echo $$s | tr '/' '_'`.o; \
|
2026-10-24 01:59:27 +02:00
|
|
|
$(CXX) $(EXAMPLE_CXXFLAGS) $(EXAMPLE_INC) -c $$s -o $$o || exit 1; \
|
2026-10-23 18:20:04 +02:00
|
|
|
done
|
2026-10-24 01:59:27 +02:00
|
|
|
$(CXX) $(EXAMPLE_CXXFLAGS) -c $(EXAMPLE_GEN)/assets.cpp \
|
|
|
|
|
-o $(EXAMPLE_BUILD)/obj/gen_assets.o
|
2026-10-23 18:20:04 +02:00
|
|
|
ar rcs $(EXAMPLE_BUILD)/libexample.a $(EXAMPLE_BUILD)/obj/*.o
|
2026-10-24 03:10:28 +02:00
|
|
|
$(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 \
|
2026-10-24 04:05:41 +02:00
|
|
|
$(EXAMPLE_REAL_LIBS) -lwolfssl -o $(EXAMPLE_BUILD)/living-server
|
2026-10-23 18:20:04 +02:00
|
|
|
|
|
|
|
|
example-test: example
|
2026-10-24 01:59:27 +02:00
|
|
|
rm -rf $(EXAMPLE_BUILD)/tests
|
2026-11-06 04:36:00 +01:00
|
|
|
mkdir -p $(EXAMPLE_BUILD)/tests $(EXAMPLE_BUILD)/tmp
|
2026-10-24 01:59:27 +02:00
|
|
|
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; \
|
2026-10-24 04:05:41 +02:00
|
|
|
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; \
|
2026-11-06 04:36:00 +01:00
|
|
|
then MOLO_ENV_FILES=off TMPDIR=$${TMPDIR:-$(EXAMPLE_BUILD)/tmp} $$b >> $$b.log 2>&1; else echo "COMPILE FAILED: $$t" >> $$b.log; false; fi; \
|
2026-10-24 01:59:27 +02:00
|
|
|
echo $$? > $$b.rc' < $(EXAMPLE_BUILD)/tests/list.txt
|
2026-10-23 18:20:04 +02:00
|
|
|
pass=0; fail=0; \
|
2026-10-24 01:59:27 +02:00
|
|
|
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; \
|
2026-10-23 18:20:04 +02:00
|
|
|
done; \
|
|
|
|
|
echo "example suite: $$pass passed, $$fail failed"; \
|
|
|
|
|
test $$fail -eq 0
|
|
|
|
|
|
2026-10-24 03:10:28 +02:00
|
|
|
example-locust:
|
|
|
|
|
python3 $(EXAMPLE_DIR)/check_locust_coverage.py
|
2026-10-26 04:15:09 +01:00
|
|
|
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}
|
2026-10-24 03:10:28 +02:00
|
|
|
|
2026-10-23 18:20:04 +02:00
|
|
|
example-clean:
|
|
|
|
|
rm -rf $(EXAMPLE_BUILD)
|
|
|
|
|
|
2026-10-24 05:06:13 +02:00
|
|
|
# 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.
|
2026-10-26 04:15:09 +01:00
|
|
|
EXAMPLE_PORT ?= 8000
|
2026-10-24 05:06:13 +02:00
|
|
|
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
|
2026-11-06 04:36:00 +01:00
|
|
|
rm -f $(EXAMPLE_RUN)/server.pid
|
|
|
|
|
setsid sh -c 'echo $$$$ > $(EXAMPLE_RUN)/server.pid; exec $(EXAMPLE_BUILD)/living-server $(EXAMPLE_PORT) --no-locust > $(EXAMPLE_RUN)/server.log 2>&1 < /dev/null' &
|
|
|
|
|
for i in `seq 1 20`; do \
|
|
|
|
|
[ -f $(EXAMPLE_RUN)/server.pid ] && kill -0 `cat $(EXAMPLE_RUN)/server.pid` 2>/dev/null && break; sleep 0.25; \
|
|
|
|
|
done; \
|
|
|
|
|
if [ ! -f $(EXAMPLE_RUN)/server.pid ] || ! kill -0 `cat $(EXAMPLE_RUN)/server.pid` 2>/dev/null; then echo "deploy: server died on start"; cat $(EXAMPLE_RUN)/server.log; exit 1; fi
|
2026-10-24 05:06:13 +02:00
|
|
|
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
|
|
|
|
|
|
2026-10-23 18:20:04 +02:00
|
|
|
# Root Makefile aggregation points (double-colon: safe with other parts).
|
|
|
|
|
all:: example
|
|
|
|
|
test:: example-test
|
|
|
|
|
clean:: example-clean
|