- Add start_app.sh script for one-click application startup - Add STARTUP_GUIDE.md with detailed startup instructions - Add init_and_start.sh alternative startup script - Fix JpaAuditingConfig to remove duplicate @EnableJpaRepositories - Update application-dev.yml to use environment variables for database config - Fix TenantContextFilterTest to use proper MockFilterChain implementation - Fix OrderAuditIntegrationTest to use correct TenantScope constructor - Fix OrderCacheIntegrationTest to use correct method names and return types - Add tmpl-admin frontend project - Update OrderControllerTest with correct method mappings and return codes Co-authored-by: Qwen-Coder <[email protected]>
198 lines
6.5 KiB
Bash
Executable File
198 lines
6.5 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Java Service Template - Initialization and Startup Script
|
|
# This script initializes the database, establishes Flyway baseline, and starts the application
|
|
|
|
set -e # Exit immediately if a command exits with a non-zero status
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m' # No Color
|
|
|
|
echo_color() {
|
|
local color=$1
|
|
local message=$2
|
|
echo -e "${color}${message}${NC}"
|
|
}
|
|
|
|
# Configuration
|
|
DB_NAME="java_template"
|
|
DB_USER="${DB_USER:-postgres}"
|
|
DB_PASS="${DB_PASS:-postgres}"
|
|
DB_HOST="${DB_HOST:-localhost}"
|
|
DB_PORT="${DB_PORT:-5432}"
|
|
|
|
APP_JAR="tmpl-api/target/tmpl-api-0.1.0-SNAPSHOT.jar"
|
|
|
|
echo_color $YELLOW "Starting Java Service Template initialization..."
|
|
|
|
# Function to check if PostgreSQL is running
|
|
check_postgres() {
|
|
if ! command -v psql &> /dev/null; then
|
|
echo_color $RED "psql command not found. Please install PostgreSQL client."
|
|
exit 1
|
|
fi
|
|
|
|
if ! pg_isready -h "$DB_HOST" -p "$DB_PORT" 2>/dev/null; then
|
|
echo_color $RED "PostgreSQL server is not running at $DB_HOST:$DB_PORT"
|
|
echo_color $YELLOW "Please start PostgreSQL server before running this script."
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Function to create database
|
|
create_database() {
|
|
echo_color $YELLOW "Creating database: $DB_NAME"
|
|
|
|
# Check if database already exists
|
|
if PGPASSWORD="$DB_PASS" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -lqt | cut -d \| -f 1 | grep -qw "$DB_NAME"; then
|
|
echo_color $GREEN "Database $DB_NAME already exists, skipping creation."
|
|
return 0
|
|
fi
|
|
|
|
# Create database
|
|
PGPASSWORD="$DB_PASS" createdb -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -O "$DB_USER" "$DB_NAME"
|
|
echo_color $GREEN "Database $DB_NAME created successfully."
|
|
}
|
|
|
|
# Function to establish Flyway baseline
|
|
establish_flyway_baseline() {
|
|
echo_color $YELLOW "Establishing Flyway baseline..."
|
|
|
|
# Check if flyway schema history table exists
|
|
if PGPASSWORD="$DB_PASS" psql -h "$DB_HOST" -p "$DB_PORT" -d "$DB_NAME" -U "$DB_USER" -tAc "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'flyway_schema_history');" | grep -q 't'; then
|
|
echo_color $GREEN "Flyway schema history table already exists, skipping baseline."
|
|
return 0
|
|
fi
|
|
|
|
# Run the application with Flyway baseline enabled
|
|
echo_color $YELLOW "Running application to establish Flyway baseline..."
|
|
|
|
# Temporarily set baseline on migrate to true
|
|
JAVA_OPTS="-Dspring.flyway.baseline-on-migrate=true -Dspring.flyway.baseline-version=0 -Dspring.profiles.active=dev"
|
|
JAVA_OPTS="$JAVA_OPTS -DDB_HOST=$DB_HOST -DDB_PORT=$DB_PORT -DDB_NAME=$DB_NAME -DDB_USER=$DB_USER -DDB_PASS=$DB_PASS"
|
|
|
|
# Run the application briefly to establish baseline
|
|
timeout 30s java $JAVA_OPTS -jar $APP_JAR || true
|
|
|
|
echo_color $GREEN "Flyway baseline established."
|
|
}
|
|
|
|
# Function to run migrations
|
|
run_migrations() {
|
|
echo_color $YELLOW "Running database migrations..."
|
|
|
|
# Run the application to apply migrations
|
|
JAVA_OPTS="-Dspring.profiles.active=dev"
|
|
JAVA_OPTS="$JAVA_OPTS -DDB_HOST=$DB_HOST -DDB_PORT=$DB_PORT -DDB_NAME=$DB_NAME -DDB_USER=$DB_USER -DDB_PASS=$DB_PASS"
|
|
|
|
# Run the application briefly to apply migrations
|
|
timeout 30s java $JAVA_OPTS -jar $APP_JAR || true
|
|
|
|
echo_color $GREEN "Database migrations applied."
|
|
}
|
|
|
|
# Function to build the application
|
|
build_application() {
|
|
echo_color $YELLOW "Building the application..."
|
|
|
|
if [ ! -f "pom.xml" ]; then
|
|
echo_color $RED "pom.xml not found in current directory!"
|
|
exit 1
|
|
fi
|
|
|
|
mvn clean package -DskipTests
|
|
|
|
if [ ! -f "$APP_JAR" ]; then
|
|
echo_color $RED "Application JAR not found at $APP_JAR after build!"
|
|
exit 1
|
|
fi
|
|
|
|
echo_color $GREEN "Application built successfully."
|
|
}
|
|
|
|
# Function to start the application
|
|
start_application() {
|
|
echo_color $YELLOW "Starting the application..."
|
|
|
|
# Set environment variables for the application
|
|
export DB_HOST=$DB_HOST
|
|
export DB_PORT=$DB_PORT
|
|
export DB_NAME=$DB_NAME
|
|
export DB_USER=$DB_USER
|
|
export DB_PASS=$DB_PASS
|
|
|
|
# Start the application in the background
|
|
JAVA_OPTS="-Dspring.profiles.active=dev"
|
|
JAVA_OPTS="$JAVA_OPTS -DDB_HOST=$DB_HOST -DDB_PORT=$DB_PORT -DDB_NAME=$DB_NAME -DDB_USER=$DB_USER -DDB_PASS=$DB_PASS"
|
|
|
|
echo_color $GREEN "Starting application with command: java $JAVA_OPTS -jar $APP_JAR"
|
|
java $JAVA_OPTS -jar $APP_JAR &
|
|
|
|
APP_PID=$!
|
|
echo $APP_PID > app.pid
|
|
echo_color $GREEN "Application started with PID: $APP_PID"
|
|
echo_color $GREEN "Application is available at: http://localhost:8080"
|
|
echo_color $GREEN "Actuator endpoints available at: http://localhost:8080/actuator"
|
|
|
|
# Wait for the application to finish
|
|
wait $APP_PID
|
|
}
|
|
|
|
# Main execution
|
|
main() {
|
|
echo_color $YELLOW "Step 1: Checking prerequisites..."
|
|
check_postgres
|
|
|
|
echo_color $YELLOW "Step 2: Building application..."
|
|
build_application
|
|
|
|
echo_color $YELLOW "Step 3: Creating database..."
|
|
create_database
|
|
|
|
echo_color $YELLOW "Step 4: Establishing Flyway baseline..."
|
|
establish_flyway_baseline
|
|
|
|
echo_color $YELLOW "Step 5: Running migrations..."
|
|
run_migrations
|
|
|
|
echo_color $YELLOW "Step 6: Starting application..."
|
|
start_application
|
|
}
|
|
|
|
# Parse command line arguments
|
|
case "${1:-full}" in
|
|
"init-only")
|
|
echo_color $YELLOW "Initializing database and establishing Flyway baseline only..."
|
|
check_postgres
|
|
create_database
|
|
establish_flyway_baseline
|
|
run_migrations
|
|
echo_color $GREEN "Database initialization completed!"
|
|
;;
|
|
"start-only")
|
|
echo_color $YELLOW "Starting application only..."
|
|
if [ ! -f "$APP_JAR" ]; then
|
|
echo_color $RED "Application JAR not found. Please build first or run 'full'."
|
|
exit 1
|
|
fi
|
|
start_application
|
|
;;
|
|
"build-only")
|
|
echo_color $YELLOW "Building application only..."
|
|
build_application
|
|
;;
|
|
"full"|"")
|
|
main
|
|
;;
|
|
*)
|
|
echo_color $RED "Usage: $0 [full|init-only|start-only|build-only]"
|
|
echo_color $YELLOW " full (default): Initialize database and start application"
|
|
echo_color $YELLOW " init-only: Initialize database and establish Flyway baseline only"
|
|
echo_color $YELLOW " start-only: Start application only (requires built JAR)"
|
|
echo_color $YELLOW " build-only: Build application only"
|
|
exit 1
|
|
;;
|
|
esac |