Files
moloapp/example/example.mk
T

167 lines
7.0 KiB
Makefile
Raw Normal View History

# 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))
# 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/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
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:8080} --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 ?= 8080
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