From a9ef0ba28bfc64693476a14f7c56174787b11136 Mon Sep 17 00:00:00 2001 From: FrankLaurance <34231927+FrankLaurance@users.noreply.github.com> Date: Wed, 17 Dec 2025 18:15:58 +0800 Subject: [PATCH 01/10] feat: add Docker deploy and optional RECIPES_URL --- .github/workflows/publish-ghcr.yml | 56 ++++++++++++++++++++++++++++++ Dockerfile | 7 ++++ README.md | 34 ++++++++++++++++++ docker-compose.yml | 15 ++++++++ server/routes/api/recipes.ts | 16 ++++++--- 5 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/publish-ghcr.yml create mode 100644 docker-compose.yml diff --git a/.github/workflows/publish-ghcr.yml b/.github/workflows/publish-ghcr.yml new file mode 100644 index 0000000..1cc6ae3 --- /dev/null +++ b/.github/workflows/publish-ghcr.yml @@ -0,0 +1,56 @@ +name: Publish Docker image (GHCR) + +on: + push: + branches: [main] + tags: + - 'v*' + workflow_dispatch: + +permissions: + contents: read + packages: write + +jobs: + build-and-push: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to GHCR + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Docker metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: | + ghcr.io/${{ github.repository_owner }}/whattoeat + tags: | + type=ref,event=tag + type=sha + type=raw,value=latest,enable={{is_default_branch}} + + - name: Build and push + uses: docker/build-push-action@v6 + with: + context: . + file: ./Dockerfile + push: true + platforms: linux/amd64,linux/arm64 + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max diff --git a/Dockerfile b/Dockerfile index 8d32591..aa182a9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,7 @@ FROM node:20-alpine AS build-stage WORKDIR /app + RUN corepack enable COPY .npmrc package.json pnpm-lock.yaml pnpm-workspace.yaml ./ @@ -15,6 +16,12 @@ FROM node:20-alpine AS production-stage WORKDIR /app +ENV NODE_ENV=production +ENV NITRO_HOST=0.0.0.0 +ENV NITRO_PORT=3000 + +LABEL org.opencontainers.image.source="https://github.com/ryanuo/whatToEat" + COPY --from=build-stage /app/.output ./.output EXPOSE 3000 diff --git a/README.md b/README.md index 7d05c01..fa91e4d 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,40 @@ pnpm preview [![Netlify](https://www.netlify.com/img/deploy/button.svg)](https://app.netlify.com/start/deploy?repository=https://github.com/ryanuo/whatToEat) [![Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/ryanuo/whatToEat) +### Docker 部署 + +默认使用公开菜单(与原项目一致):`https://eat.ryanuo.cc/recipes.json`。 + +如需部署到你自己的本地菜单/菜谱服务,启动时传入环境变量 `RECIPES_URL`(支持 base URL 或完整 `recipes.json` URL)。 + +```bash +docker compose up -d --build +``` + +### Docker 部署(服务器不构建:从 GHCR 拉镜像运行) + +仓库提交到 GitHub 后,会通过 GitHub Actions 自动构建并发布镜像到 GHCR。 + +在服务器上部署(无需构建): + +```bash +docker pull ghcr.io/ryanuo/whattoeat:latest + +docker run -d --name whattoeat \ + -p 3000:3000 \ + -e RECIPES_URL=http://your-recipes-host:5000 \ + ghcr.io/ryanuo/whattoeat:latest +``` + +不设置 `RECIPES_URL` 时会使用默认公开菜单。 + +使用你自己的菜单: + +```bash +RECIPES_URL=http://your-recipes-host:5000 docker compose up -d --build +# 或者:RECIPES_URL=http://your-recipes-host:5000/recipes.json docker compose up -d --build +``` + ## 数据来源 菜谱数据来源于远程 JSON 接口,通过 `server/api/recipes.ts` 进行获取和处理。 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..6b2af45 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,15 @@ +services: + whattoeat: + build: . + container_name: whattoeat + ports: + - "3000:3000" + environment: + # 可选:指向你自己的菜谱服务(支持 base URL 或完整 recipes.json URL) + # - RECIPES_URL=http://your-recipes-host:5000 + # - RECIPES_URL=http://your-recipes-host:5000/recipes.json + - RECIPES_URL=${RECIPES_URL:-} + # 容器内监听地址(Docker 环境建议显式设置) + - NITRO_HOST=0.0.0.0 + - NITRO_PORT=3000 + restart: unless-stopped diff --git a/server/routes/api/recipes.ts b/server/routes/api/recipes.ts index bdc1ce9..911c7c0 100644 --- a/server/routes/api/recipes.ts +++ b/server/routes/api/recipes.ts @@ -1,15 +1,23 @@ -// server/api/recipes.ts import type { Recipe } from '~/types' -// 获取远程菜谱 +// 获取菜谱:可选使用环境变量 RECIPES_URL 指向本地菜单;否则使用默认公开菜单 async function fetchRecipes(): Promise { try { + const override = process.env.RECIPES_URL?.trim() + + // 默认菜单(与原项目一致) const baseURL = import.meta.dev ? 'http://localhost:3000' : 'https://eat.ryanuo.cc' - const recipes = await $fetch(`${baseURL}/recipes.json`) + const defaultUrl = `${baseURL}/recipes.json` + + const url = override + ? (override.endsWith('.json') ? override : `${override.replace(/\/+$/, '')}/recipes.json`) + : defaultUrl + + const recipes = await $fetch(url) return recipes as Recipe[] } catch (error) { - console.error('获取远程菜谱数据失败:', error) + console.error('获取菜谱数据失败:', error) return [] } } From 7befa3505a6b72b385babe1d0acb02e3ccdb196b Mon Sep 17 00:00:00 2001 From: FrankLaurance <34231927+FrankLaurance@users.noreply.github.com> Date: Wed, 17 Dec 2025 19:36:24 +0800 Subject: [PATCH 02/10] chore: remove RECIPES_URL function --- README.md | 14 +------------- server/routes/api/recipes.ts | 14 ++------------ 2 files changed, 3 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index fa91e4d..d41e0d7 100644 --- a/README.md +++ b/README.md @@ -60,8 +60,6 @@ pnpm preview 默认使用公开菜单(与原项目一致):`https://eat.ryanuo.cc/recipes.json`。 -如需部署到你自己的本地菜单/菜谱服务,启动时传入环境变量 `RECIPES_URL`(支持 base URL 或完整 `recipes.json` URL)。 - ```bash docker compose up -d --build ``` @@ -77,22 +75,12 @@ docker pull ghcr.io/ryanuo/whattoeat:latest docker run -d --name whattoeat \ -p 3000:3000 \ - -e RECIPES_URL=http://your-recipes-host:5000 \ ghcr.io/ryanuo/whattoeat:latest ``` -不设置 `RECIPES_URL` 时会使用默认公开菜单。 - -使用你自己的菜单: - -```bash -RECIPES_URL=http://your-recipes-host:5000 docker compose up -d --build -# 或者:RECIPES_URL=http://your-recipes-host:5000/recipes.json docker compose up -d --build -``` - ## 数据来源 -菜谱数据来源于远程 JSON 接口,通过 `server/api/recipes.ts` 进行获取和处理。 +菜谱数据来源于远程 JSON 接口,通过 `server/routes/api/recipes.ts` 进行获取和处理。 ## 参考 diff --git a/server/routes/api/recipes.ts b/server/routes/api/recipes.ts index 911c7c0..86d3de0 100644 --- a/server/routes/api/recipes.ts +++ b/server/routes/api/recipes.ts @@ -1,19 +1,9 @@ import type { Recipe } from '~/types' -// 获取菜谱:可选使用环境变量 RECIPES_URL 指向本地菜单;否则使用默认公开菜单 +// 获取菜谱:统一使用默认公开菜单 async function fetchRecipes(): Promise { try { - const override = process.env.RECIPES_URL?.trim() - - // 默认菜单(与原项目一致) - const baseURL = import.meta.dev ? 'http://localhost:3000' : 'https://eat.ryanuo.cc' - const defaultUrl = `${baseURL}/recipes.json` - - const url = override - ? (override.endsWith('.json') ? override : `${override.replace(/\/+$/, '')}/recipes.json`) - : defaultUrl - - const recipes = await $fetch(url) + const recipes = await $fetch('https://eat.ryanuo.cc/recipes.json') return recipes as Recipe[] } catch (error) { From 72f90055728fcb56fdf470f8ad48bf62416ae28f Mon Sep 17 00:00:00 2001 From: FrankLaurance <34231927+FrankLaurance@users.noreply.github.com> Date: Thu, 18 Dec 2025 08:22:38 +0800 Subject: [PATCH 03/10] chore: Improve recipe data loading with retries and UI states --- app/components/Eat.vue | 56 ++++++++++++++++++++++++++++++++++-- server/routes/api/recipes.ts | 33 ++++++++++++++++----- 2 files changed, 80 insertions(+), 9 deletions(-) diff --git a/app/components/Eat.vue b/app/components/Eat.vue index 2c8a04a..b4003dd 100644 --- a/app/components/Eat.vue +++ b/app/components/Eat.vue @@ -6,7 +6,17 @@ import { emojiMap } from '~/constants' const isPlaying = ref(false) const currentFood = ref() const shakeTitle = ref(false) -const { data } = await useFetch('/api/recipes') +const { data, error, pending, refresh } = await useFetch('/api/recipes', { + retry: 3, + retryDelay: 1000, + timeout: 10000, +}) + +// 检查数据是否成功加载 +const isDataReady = computed(() => { + return !pending.value && !error.value && data.value && data.value.recipes && data.value.recipes.length > 0 +}) + const categories = computed(() => (data.value?.categories || []) as string[]) const selectedCategories = useStorage('selected-categories', [...categories.value]) const isAllSelected = computed(() => selectedCategories.value.length === categories.value.length) @@ -74,6 +84,12 @@ function startRandom() { if (!import.meta.client) return + // 确保数据已加载 + if (!data.value?.recipes || data.value.recipes.length === 0) { + console.warn('菜单数据未加载,无法开始随机') + return + } + currentFood.value = undefined shakeTitle.value = true @@ -171,7 +187,43 @@ onUnmounted(() => {
-
+ +
+
+ +

正在加载菜单数据...

+
+
+ + +
+
+

😞 菜单数据加载失败

+

{{ error.message }}

+ +
+
+ + +
+
+

📭 暂无菜单数据

+ +
+
+ + +