.PHONY: dev build build-web run tidy test recover docker-build docker-push docker-push-prebuilt

export GOPROXY ?= https://goproxy.cn,direct
export GOSUMDB ?= sum.golang.google.cn
export GOPRIVATE ?= git.rc707blog.top
export IMAGE_PREFIX ?= docker.1panel.live/library/
export APK_MIRROR ?= mirrors.aliyun.com
export DOCKER_PLATFORM ?= linux/amd64
export LUMINARY_IMAGE ?= registry.rc707blog.top/rose_cat707/luminary:latest

dev:
	@echo "Start backend: go run ./cmd/luminary"
	@echo "Start frontend: cd web && npm run dev"

build-web:
	cd web && npm install --registry=https://registry.npmmirror.com && npm run build

build: build-web
	go build -o luminary ./cmd/luminary

recover:
	go build -o luminary-recover ./cmd/luminary-recover

run: build
	./luminary

tidy:
	go mod tidy

test:
	go test ./...

docker-build:
	docker buildx build --platform $(DOCKER_PLATFORM) \
		-t $(LUMINARY_IMAGE) \
		--build-arg IMAGE_PREFIX=$(IMAGE_PREFIX) \
		--build-arg APK_MIRROR=$(APK_MIRROR) \
		--build-arg GOPROXY=$(GOPROXY) \
		--build-arg GOSUMDB=$(GOSUMDB) \
		--load \
		.

docker-push:
	docker buildx build --platform $(DOCKER_PLATFORM) \
		-t $(LUMINARY_IMAGE) \
		--build-arg IMAGE_PREFIX=$(IMAGE_PREFIX) \
		--build-arg APK_MIRROR=$(APK_MIRROR) \
		--build-arg GOPROXY=$(GOPROXY) \
		--build-arg GOSUMDB=$(GOSUMDB) \
		--push \
		.

docker-push-prebuilt: build-web
	mkdir -p .docker-bin
	GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o .docker-bin/luminary ./cmd/luminary
	GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o .docker-bin/luminary-recover ./cmd/luminary-recover
	docker buildx build --platform $(DOCKER_PLATFORM) \
		-f docker/Dockerfile.prebuilt \
		-t $(LUMINARY_IMAGE) \
		--build-arg IMAGE_PREFIX=$(IMAGE_PREFIX) \
		--build-arg APK_MIRROR=$(APK_MIRROR) \
		--push \
		.
