ly0303521

添加网页前端后端启动脚本,以及增加日志管理功能

  1 +# Shared Configuration
  2 +PUBLIC_IP="106.120.52.146"
  3 +
  4 +# Public Ports (External Access)
  5 +PUBLIC_BACKEND_PORT="37000"
  6 +PUBLIC_FRONTEND_PORT="37001"
  7 +PUBLIC_TURBO_PORT="37002"
  8 +PUBLIC_OSS_PORT="34000"
  9 +PUBLIC_ZIMAGE_PORT="39009"
  10 +
  11 +# Local Ports (Internal Bind)
  12 +LOCAL_BACKEND_PORT="7000"
  13 +LOCAL_FRONTEND_PORT="7001"
@@ -26,3 +26,5 @@ dist-ssr @@ -26,3 +26,5 @@ dist-ssr
26 backend/whitelist.txt 26 backend/whitelist.txt
27 backend/gallery_images.json 27 backend/gallery_images.json
28 backend/gallery_videos.json 28 backend/gallery_videos.json
  29 +z-image-generator/public
  30 +
  1 +#!/bin/bash
  2 +
  3 +BASE_DIR=$(cd "$(dirname "$0")"; pwd)
  4 +
  5 +# Load shared configuration
  6 +if [ -f "$BASE_DIR/.env.sh" ]; then
  7 + source "$BASE_DIR/.env.sh"
  8 +else
  9 + echo "Error: .env.sh not found in $BASE_DIR"
  10 + exit 1
  11 +fi
  12 +FRONTEND_DIR="$BASE_DIR/z-image-generator"
  13 +BACKEND_DIR="$BASE_DIR"
  14 +CONSTANTS_FILE="$FRONTEND_DIR/constants.ts"
  15 +CONFIG_JS_FILE="$FRONTEND_DIR/public/config.js"
  16 +LOGS_DIR="$BASE_DIR/logs"
  17 +
  18 +# Ensure logs directory exists
  19 +mkdir -p "$LOGS_DIR"
  20 +
  21 +# Function to rotate logs
  22 +rotate_log() {
  23 + local log_file="$1"
  24 + if [ -f "$log_file" ]; then
  25 + local timestamp=$(date +"%Y%m%d_%H%M%S")
  26 + local filename=$(basename "$log_file")
  27 + local archived_log="$LOGS_DIR/${filename%.*}_$timestamp.log"
  28 + echo "Rotating log: $log_file -> $archived_log"
  29 + mv "$log_file" "$archived_log"
  30 + fi
  31 +}
  32 +
  33 +echo "=================================================="
  34 +echo "Initializing Front-Backend Z-Image Services"
  35 +echo "=================================================="
  36 +
  37 +# 1. Generate Runtime Config (config.js)
  38 +echo "Generating runtime configuration in $CONFIG_JS_FILE..."
  39 +
  40 +# Ensure directory exists
  41 +mkdir -p "$(dirname "$CONFIG_JS_FILE")"
  42 +
  43 +cat > "$CONFIG_JS_FILE" <<EOF
  44 +window.APP_CONFIG = {
  45 + Z_IMAGE_DIRECT_BASE_URL: "http://$PUBLIC_IP:$PUBLIC_ZIMAGE_PORT",
  46 + TURBO_DIFFUSION_API_URL: "http://$PUBLIC_IP:$PUBLIC_TURBO_PORT",
  47 + VIDEO_OSS_BASE_URL: "http://$PUBLIC_IP:$PUBLIC_OSS_PORT",
  48 + API_BASE_URL: "http://$PUBLIC_IP:$PUBLIC_BACKEND_PORT"
  49 +};
  50 +EOF
  51 +
  52 +echo "Configuration generated."
  53 +
  54 +# 2. Start Backend Service
  55 +echo "--------------------------------------------------"
  56 +echo "Starting Backend Service..."
  57 +echo "Host: 0.0.0.0, Port: $LOCAL_BACKEND_PORT"
  58 +echo "Public URL: http://$PUBLIC_IP:$PUBLIC_BACKEND_PORT"
  59 +
  60 +cd "$BACKEND_DIR" || exit 1
  61 +PID_BACKEND=$(lsof -t -i:$LOCAL_BACKEND_PORT)
  62 +if [ -n "$PID_BACKEND" ]; then
  63 + echo "Killing existing backend process on port $LOCAL_BACKEND_PORT (PID: $PID_BACKEND)..."
  64 + kill -9 "$PID_BACKEND"
  65 +fi
  66 +
  67 +rotate_log "$BASE_DIR/backend.log"
  68 +
  69 +# Use venv uvicorn
  70 +UVICORN_BIN="$BASE_DIR/venv/bin/uvicorn"
  71 +if [ ! -f "$UVICORN_BIN" ]; then
  72 + echo "Warning: venv uvicorn not found at $UVICORN_BIN, trying system uvicorn..."
  73 + UVICORN_BIN="uvicorn"
  74 +fi
  75 +
  76 +nohup "$UVICORN_BIN" backend.main:app --host 0.0.0.0 --port "$LOCAL_BACKEND_PORT" > backend.log 2>&1 &
  77 +echo "Backend started with PID: $!"
  78 +
  79 +# 3. Start Frontend Service
  80 +echo "--------------------------------------------------"
  81 +echo "Starting Frontend Service..."
  82 +echo "Host: 0.0.0.0, Port: $LOCAL_FRONTEND_PORT"
  83 +echo "Public URL: http://$PUBLIC_IP:$PUBLIC_FRONTEND_PORT"
  84 +
  85 +cd "$FRONTEND_DIR" || exit 1
  86 +PID_FRONTEND=$(lsof -t -i:$LOCAL_FRONTEND_PORT)
  87 +if [ -n "$PID_FRONTEND" ]; then
  88 + echo "Killing existing frontend process on port $LOCAL_FRONTEND_PORT (PID: $PID_FRONTEND)..."
  89 + kill -9 "$PID_FRONTEND"
  90 +fi
  91 +
  92 +rotate_log "$BASE_DIR/frontend.log"
  93 +
  94 +export VITE_API_BASE_URL="http://$PUBLIC_IP:$PUBLIC_BACKEND_PORT"
  95 +
  96 +nohup npm run dev -- --port "$LOCAL_FRONTEND_PORT" --host 0.0.0.0 > ../frontend.log 2>&1 &
  97 +echo "Frontend started with PID: $!"
  98 +
  99 +echo "=================================================="
  100 +echo "All services initiated."
  1 +#!/bin/bash
  2 +
  3 +BASE_DIR=$(cd "$(dirname "$0")"; pwd)
  4 +
  5 +# Load shared configuration
  6 +if [ -f "$BASE_DIR/.env.sh" ]; then
  7 + source "$BASE_DIR/.env.sh"
  8 +else
  9 + echo "Error: .env.sh not found in $BASE_DIR"
  10 + exit 1
  11 +fi
  12 +
  13 +echo "=================================================="
  14 +echo "Stopping Front-Backend Z-Image Services"
  15 +echo "=================================================="
  16 +
  17 +# 1. Stop Backend Service
  18 +echo "Checking Backend Service on port $LOCAL_BACKEND_PORT..."
  19 +PID_BACKEND=$(lsof -t -i:$LOCAL_BACKEND_PORT)
  20 +if [ -n "$PID_BACKEND" ]; then
  21 + echo "Found backend process (PID: $PID_BACKEND). Stopping..."
  22 + kill -9 "$PID_BACKEND"
  23 + echo "Backend service stopped."
  24 +else
  25 + echo "No backend service found running on port $LOCAL_BACKEND_PORT."
  26 +fi
  27 +
  28 +echo "--------------------------------------------------"
  29 +
  30 +# 2. Stop Frontend Service
  31 +echo "Checking Frontend Service on port $LOCAL_FRONTEND_PORT..."
  32 +PID_FRONTEND=$(lsof -t -i:$LOCAL_FRONTEND_PORT)
  33 +if [ -n "$PID_FRONTEND" ]; then
  34 + echo "Found frontend process (PID: $PID_FRONTEND). Stopping..."
  35 + kill -9 "$PID_FRONTEND"
  36 + echo "Frontend service stopped."
  37 +else
  38 + echo "No frontend service found running on port $LOCAL_FRONTEND_PORT."
  39 +fi
  40 +
  41 +echo "=================================================="
  42 +echo "All Z-Image services stopped."
1 import { ImageItem } from './types'; 1 import { ImageItem } from './types';
2 2
3 -export const Z_IMAGE_DIRECT_BASE_URL = "http://106.120.52.146:39009"; 3 +// Declare global window object with APP_CONFIG
  4 +declare global {
  5 + interface Window {
  6 + APP_CONFIG?: {
  7 + Z_IMAGE_DIRECT_BASE_URL?: string;
  8 + TURBO_DIFFUSION_API_URL?: string;
  9 + VIDEO_OSS_BASE_URL?: string;
  10 + API_BASE_URL?: string;
  11 + }
  12 + }
  13 +}
  14 +
  15 +const config = typeof window !== 'undefined' ? window.APP_CONFIG || {} : {};
  16 +
  17 +export const Z_IMAGE_DIRECT_BASE_URL = config.Z_IMAGE_DIRECT_BASE_URL || "http://106.120.52.146:39009";
4 18
5 // Base URL for the TurboDiffusion AI service (submit job, poll status) 19 // Base URL for the TurboDiffusion AI service (submit job, poll status)
6 -export const TURBO_DIFFUSION_API_URL = "http://106.120.52.146:38000"; 20 +export const TURBO_DIFFUSION_API_URL = config.TURBO_DIFFUSION_API_URL || "http://106.120.52.146:38000";
7 21
8 // Base URL for the OSS service that serves the final video files 22 // Base URL for the OSS service that serves the final video files
9 -export const VIDEO_OSS_BASE_URL = "http://106.120.52.146:39997"; 23 +export const VIDEO_OSS_BASE_URL = config.VIDEO_OSS_BASE_URL || "http://106.120.52.146:39997";
10 24
11 const ENV_PROXY_URL = import.meta.env?.VITE_API_BASE_URL?.trim(); 25 const ENV_PROXY_URL = import.meta.env?.VITE_API_BASE_URL?.trim();
12 -const DEFAULT_PROXY_URL = ENV_PROXY_URL && ENV_PROXY_URL.length > 0 26 +const DEFAULT_PROXY_URL = config.API_BASE_URL || (ENV_PROXY_URL && ENV_PROXY_URL.length > 0
13 ? ENV_PROXY_URL 27 ? ENV_PROXY_URL
14 - : "http://106.120.52.146:37000"; 28 + : "http://106.120.52.146:37000");
15 29
16 export const API_BASE_URL = DEFAULT_PROXY_URL; 30 export const API_BASE_URL = DEFAULT_PROXY_URL;
17 31
@@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
4 <meta charset="UTF-8" /> 4 <meta charset="UTF-8" />
5 <meta name="viewport" content="width=device-width, initial-scale=1.0" /> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6 <title>Z-Image Generator</title> 6 <title>Z-Image Generator</title>
  7 + <script src="/config.js"></script>
7 <script src="https://cdn.tailwindcss.com"></script> 8 <script src="https://cdn.tailwindcss.com"></script>
8 <script> 9 <script>
9 tailwind.config = { 10 tailwind.config = {