-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
188 lines (160 loc) · 6.41 KB
/
Makefile
File metadata and controls
188 lines (160 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
.PHONY: help \
r b br rr t tt f fc l ck c \
builder builder-rm bd bdp rd rdd rdp sd ld cd tf
.DELETE_ON_ERROR:
MAKEFLAGS += --warn-undefined-variables
.DEFAULT_GOAL := r
PROJECT_NAME ?= $(shell cargo metadata --no-deps --format-version 1 | sed -n 's/.*"name":"\([^"]*\)".*/\1/p' | head -n1)
PROJECT_VERSION ?= $(shell cargo metadata --no-deps --format-version 1 | sed -n 's/.*"version":"\([^"]*\)".*/\1/p' | head -n1)
BUILD_TIME := $(shell date -u '+%Y-%m-%d_%H:%M:%S_UTC')
IMAGE ?= nadzu
TAG ?= local
MODE ?= debug
BIN ?= nadzu
PORT ?= 8080
CONTAINER_NAME ?= nadzu-local
PLATFORMS ?= linux/amd64,linux/arm64
PLATFORM ?= linux/amd64
BUILDER_NAME ?= zstd-builder
PUSH ?= false
TF_STACK_DIR ?= infra/digitalocean/accounts/naduns-team
VERBOSE ?= true
RED := \033[0;31m
GREEN := \033[0;32m
BLUE := \033[0;34m
NC := \033[0m
ifeq ($(VERBOSE),true)
Q :=
SAY := @echo -e
else
Q := @
SAY := @echo -e
endif
ifeq ($(OS),Windows_NT)
NPROCS := $(NUMBER_OF_PROCESSORS)
else ifeq ($(shell uname -s),Darwin)
NPROCS := $(shell sysctl -n hw.physicalcpu)
else
NPROCS := $(shell nproc)
endif
help: ## Show available targets
$(SAY) "$(BLUE)Available targets for $(PROJECT_NAME) v$(PROJECT_VERSION):$(NC)"
$(Q)grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = ":.*?## "}; {printf " $(GREEN)%-15s$(NC) %s\n", $$1, $$2}'
# -----------------------
# Local (non-Docker)
# -----------------------
t: ## Run all tests (locked + all targets)
$(SAY) "$(BLUE)Running tests...$(NC)"
$(Q)cargo test --locked --all-targets
tt: ## TDD loop entrypoint (usage: make ltdd TEST=<name>)
$(SAY) "$(BLUE)Running focused test for TDD...$(NC)"
$(Q)test -n "$(TEST)" || (echo "TEST is required. Example: make ltdd TEST=resolve_mp4_best_selector" && exit 1)
$(Q)cargo test --locked -- --nocapture $(TEST)
f: ## Format code
$(SAY) "$(BLUE)Formatting code...$(NC)"
$(Q)cargo fmt
fc: ## Check formatting
$(SAY) "$(BLUE)Checking format...$(NC)"
$(Q)cargo fmt -- --check
l: ## Lint code (clippy)
$(SAY) "$(BLUE)Linting with clippy...$(NC)"
$(Q)cargo clippy --all-targets --all-features -- -D warnings
c: ## Run format check + type check + lint
$(SAY) "$(BLUE)Checking format...$(NC)"
$(Q)cargo fmt -- --check
$(SAY) "$(BLUE)Running cargo check...$(NC)"
$(Q)cargo check --locked
$(SAY) "$(BLUE)Running clippy...$(NC)"
$(Q)cargo clippy --locked --all-targets --all-features -- -D warnings
$(SAY) "$(BLUE)Running tests...$(NC)"
$(Q)cargo test --locked --all-targets
$(SAY) "$(GREEN):::All local checks passed:::$(NC)"
r: c ## Run the app locally (non-Docker)
$(SAY) "$(BLUE)Running $(BIN) locally...$(NC)"
$(Q)cargo run --locked --bin $(BIN)
# -----------------------
# Docker - builder
# -----------------------
builder: ## Create/use dedicated buildx builder and bootstrap it
$(SAY) "$(BLUE)Setting up Docker buildx builder $(BUILDER_NAME)...$(NC)"
-$(Q)docker buildx create --name $(BUILDER_NAME) --use
$(Q)docker buildx use $(BUILDER_NAME)
$(Q)docker buildx inspect --bootstrap
builder-rm: ## Remove dedicated buildx builder
$(SAY) "$(RED)Removing Docker buildx builder $(BUILDER_NAME)...$(NC)"
-$(Q)docker buildx rm $(BUILDER_NAME)
# -----------------------
# Docker - local development
# -----------------------
bd: builder ## Local image build (BuildKit --load + zstd)
$(SAY) "$(BLUE)Building Docker image $(IMAGE):$(TAG) [$(MODE)]...$(NC)"
$(Q)DOCKER_BUILDKIT=1 docker buildx build \
--builder $(BUILDER_NAME) \
--load \
--output type=docker,compression=zstd \
--platform $(PLATFORM) \
--progress=plain \
--build-arg MODE=$(MODE) \
--build-arg BIN=$(BIN) \
--tag $(IMAGE):$(TAG) \
.
bdp: builder ## Multi-platform release image build
$(SAY) "$(BLUE)Building production Docker image $(IMAGE):$(TAG) for $(PLATFORMS)...$(NC)"
$(Q)DOCKER_BUILDKIT=1 docker buildx build \
--builder $(BUILDER_NAME) \
--platform linux/amd64 \
--progress=plain \
--build-arg MODE=release \
--build-arg BIN=$(BIN) \
--output type=image,name=$(IMAGE):$(TAG),push=$(PUSH),compression=zstd,oci-mediatypes=true \
.
rd: ## Run local Docker Compose stack (Dev Environment)
$(SAY) "$(GREEN)Cleaning up old containers and volumes...$(NC)"
$(Q)docker compose -f docker-compose.dev.yml down -v
$(SAY) "$(GREEN)Running local Compose dev stack...$(NC)"
$(Q)docker compose -f docker-compose.dev.yml up -d --build
$(Q)docker compose -f docker-compose.dev.yml logs app -f
rdd: ## Run Docker Compose stack (prod Environment)
$(SAY) "$(GREEN)Preparing to run Docker Compose prod stack...$(NC)"
$(SAY) "$(GREEN)Running local Compose prod stack...$(NC)"
$(Q)docker compose -f docker-compose.yml up -d
rdp: ## Run local image as production simulation (uses $(IMAGE):$(TAG))
$(SAY) "$(GREEN)Running $(IMAGE):$(TAG) as production simulation on port $(PORT)...$(NC)"
-$(Q)docker rm -f $(CONTAINER_NAME)-prod >/dev/null 2>&1 || true
$(Q)docker run -d \
--name $(CONTAINER_NAME)-prod \
--restart unless-stopped \
-p $(PORT):$(PORT) \
-e APP_HOST=0.0.0.0 \
-e APP_PORT=$(PORT) \
-e APP_ENV=production \
-e DOWNLOAD_DIR=/home/app/downloads \
-e MAX_CONCURRENT_DOWNLOADS=3 \
-v $(PWD)/downloads:/home/app/downloads \
$(IMAGE):$(TAG)
sd: ## Stop running local Docker Compose stack
$(SAY) "$(RED)Stopping local dev Compose stack...$(NC)"
$(Q)docker compose -f docker-compose.dev.yml down
logsa: ## Tail logs of running local dev app container
$(Q)docker compose -f docker-compose.dev.yml logs -f app
logsc: ## Tail logs of running local dev app container
$(Q)docker compose -f docker-compose.dev.yml logs -f caddy
cd: ## Remove dangling Docker build cache and images
$(SAY) "$(RED)Cleaning Docker system artifacts...$(NC)"
$(Q)docker system prune -af
# -----------------------
# Terraform
# -----------------------
tf: ## use this to spawn a loaded shell
$(SAY) "$(BLUE)Entering $(TF_STACK_DIR) with environment loaded from root .env$(NC)"
$(Q)bash -lc "\
set -a && \
source <(tr -d '\r' < .env | sed -E 's/^[[:space:]]*([A-Za-z_][A-Za-z0-9_]*)[[:space:]]*=[[:space:]]*(.*)$$/\1=\2/' | grep -E '^[A-Za-z_][A-Za-z0-9_]*=') && \
set +a && \
aws s3 cp infra/common/browse.html \"s3://\$$AWS_S3_BUCKET_NAME/terraform/data/browse.html\" --endpoint-url \"\$$AWS_ENDPOINT_URL_S3\" && \
export TF_VAR_CADDY_CUSTOM_BROWSE_FILE_URL=\$$(aws s3 presign \"s3://\$$AWS_S3_BUCKET_NAME/terraform/data/browse.html\" --endpoint-url \"\$$AWS_ENDPOINT_URL_S3\" --expires-in 3600 | tr -d '\r') && \
export MSYS_NO_PATHCONV=1 && \
cd $(TF_STACK_DIR) && \
unset PROMPT_COMMAND && \
exec bash -l"