Просмотр исходного кода

explorer/site: Add Makefile for simplified Flask Explorer site management

Introduced a Makefile to streamline the setup, startup, and shutdown of the Flask Explorer site across different networks (localnet, testnet, mainnet). It simplifies server management and cleanup with straightforward commands.

In future iterations, `testnet` and `mainnet` environments may transition to production-ready setups (e.g., running behind a reverse proxy). This implementation serves as a starting point and is expected to be expanded upon.

Summary of Updates:
- Automatic virtual environment setup and dependency installation (`all`, ``install` targets)
- Added targets to start the Flask server for localnet, testnet, and mainnet networks
- Implemented the use of a `flask.pid` file to track the Flask server’s process ID (PID), enabling proper process termination during the `stop-server` target
- Provided a `clean` target to remove the virtual environment and reset the workspace
kalm 1 год назад
Родитель
Сommit
a839603b2c
1 измененных файлов с 49 добавлено и 0 удалено
  1. 49 0
      bin/explorer/site/Makefile

+ 49 - 0
bin/explorer/site/Makefile

@@ -0,0 +1,49 @@
+BIN := venv/bin/activate
+PYTHON := /opt/tools/python/bin/python3.12
+
+all: $(BIN)
+
+# Create the virtual environment and install dependencies
+$(BIN): requirements.txt
+	@echo "Installing dependencies..."
+	@if [ ! -d venv ]; then \
+		$(PYTHON) -m venv venv; \
+	fi; \
+	. venv/bin/activate && pip install -r requirements.txt
+
+# Start the Flask server for the specified network (localnet, testnet, mainnet)
+start-%: all
+	@if [ "$*" != "localnet" ] && [ "$*" != "testnet" ] && [ "$*" != "mainnet" ]; then \
+		echo "Error: Unsupported environment '$*'. Supported values are 'localnet', 'testnet', and 'mainnet'."; \
+		exit 1; \
+	fi
+	@if [ -f flask.pid ]; then \
+		echo "Flask server is already running (PID=$$(cat flask.pid)). Stop it first before starting."; \
+		exit 1; \
+	fi
+	@. venv/bin/activate && \
+		FLASK_ENV=$* python -m flask run & PID=$$!; \
+		echo $$PID > flask.pid; \
+		echo "Started flask server on $* network (PID=$$PID)"
+
+# Stop the Flask server if running
+stop-server:
+	@if [ -f flask.pid ]; then \
+		PID=$$(cat flask.pid); \
+		kill $$PID; \
+		rm -f flask.pid; \
+		echo "Stopped Flask server"; \
+	else \
+		echo "Flask server is not running, nothing to stop."; \
+	fi
+
+# Install dependencies (alias for 'all')
+install: all
+
+# Remove virtual environment
+clean:
+	@rm -rf venv
+	@echo "Cleaned the virtual environment!"
+
+# Declare PHONY targets
+.PHONY: all start-% install clean stop-server