diff --git a/README.md b/README.md index 64e37738..f04b7051 100644 --- a/README.md +++ b/README.md @@ -13,13 +13,88 @@ A modern full-stack starter application with Rails backend and React frontend us ## Setup -1. Clone this repository -2. Set up environment variables (see [Environment Variables](#environment-variables)) -3. Setup dependencies & run the server: +### Quick Start + +1. **Clone this repository** + ```bash + git clone https://github.com/danielpaul/RapidRailsInertiaJS.git + cd RapidRailsInertiaJS + ``` + +2. **Run the setup script** ```bash bin/setup ``` -4. Open http://localhost:3000 + + The setup script will: + - Install Ruby and Node.js dependencies + - Create `.env` file from `.env.example` (if not present) + - Guide you through Rails credentials setup + - Prepare the database + - Start the development server + +3. **Open http://localhost:3000** + +### Setup Details + +The `bin/setup` script handles most configuration automatically, but you'll need to configure: + +#### 1. Environment Variables (.env file) +The setup script will create a `.env` file from `.env.example`. You need to: +- Add your Clerk publishable key: `VITE_CLERK_PUBLISHABLE_KEY=pk_test_your_key_here` +- Configure any other environment-specific settings + +#### 2. Rails Credentials +The setup script will help you configure Rails encrypted credentials. You need to add: +- **Clerk secret key** for backend authentication +- **Postmark API token** for production email delivery + +Run `bin/rails credentials:edit` to add: +```yaml +clerk: + secret_key: "sk_test_your_secret_key_here" + +# For production email delivery +postmark: + api_token: "your_postmark_server_api_token_here" +``` + +**Credentials Troubleshooting:** +- If you have an existing encrypted credentials file but missing `master.key`, the setup script will guide you through recovery options +- If you need to start fresh, choose option 2 in the setup script to recreate credentials +- Use `config/credentials.yml.example` as a reference for the expected structure + +### Manual Setup (Alternative) + +If you prefer manual setup or encounter issues: + +1. **Install dependencies:** + ```bash + bundle install + npm install + ``` + +2. **Create environment file:** + ```bash + cp .env.example .env + # Edit .env to add your environment variables + ``` + +3. **Setup Rails credentials:** + ```bash + rails credentials:edit + # Add your secrets following config/credentials.yml.example + ``` + +4. **Prepare database:** + ```bash + bin/rails db:prepare + ``` + +5. **Start the server:** + ```bash + bin/dev + ``` ## Environment Variables diff --git a/bin/setup b/bin/setup index 0a227024..1da86b79 100755 --- a/bin/setup +++ b/bin/setup @@ -1,4 +1,6 @@ #!/usr/bin/env ruby +# frozen_string_literal: true + require "fileutils" APP_ROOT = File.expand_path("..", __dir__) @@ -7,6 +9,121 @@ def system!(*args) system(*args, exception: true) end +def prompt_user(message) + print message + STDIN.gets.chomp +end + +def setup_environment_file + env_example_path = ".env.example" + env_path = ".env" + + if File.exist?(env_path) + puts "✓ .env file already exists" + elsif File.exist?(env_example_path) + puts "\n== Setting up environment file ==" + FileUtils.cp env_example_path, env_path + puts "✓ Created .env file from .env.example" + puts "ℹ Please edit .env file to add your actual environment variables" + else + puts "⚠ Warning: .env.example file not found" + end +end + +def setup_database_names + database_config_path = "config/database.yml" + + puts "\n== Database Configuration ==" + puts "Current database prefix: rapid_rails_inertia_js" + + project_name = prompt_user("Enter your project name (or press Enter to keep 'rapid_rails_inertia_js'): ").strip + + if project_name.empty? + puts "✓ Keeping default database prefix: rapid_rails_inertia_js" + return + end + + # Validate project name for database usage + unless project_name.match?(/\A[a-z][a-z0-9_]*\z/) + puts "⚠ Invalid project name. Must start with a letter and contain only lowercase letters, numbers, and underscores." + puts " Keeping default database prefix: rapid_rails_inertia_js" + return + end + + puts "Updating database configuration with project name: #{project_name}" + + # Read current database.yml + database_config = File.read(database_config_path) + + # Replace the database prefix + updated_config = database_config.gsub(/rapid_rails_inertia_js/, project_name) + + # Write updated configuration + File.write(database_config_path, updated_config) + + puts "✓ Database configuration updated with prefix: #{project_name}" +end + +def setup_credentials + credentials_path = "config/credentials.yml.enc" + master_key_path = "config/master.key" + credentials_example_path = "config/credentials.yml.example" + + if File.exist?(credentials_path) && !File.exist?(master_key_path) + puts "\n== Rails Credentials Setup Required ==" + puts "Found encrypted credentials file but missing master.key" + puts "" + puts "Options:" + puts "1. Provide existing master.key" + puts "2. Create new credentials file (will delete existing encrypted file)" + puts "3. Skip for now (you'll need to set this up manually later)" + + choice = prompt_user("Choose option (1/2/3): ") + + case choice + when "1" + master_key = prompt_user("Please enter your master.key: ").strip + if master_key.length == 32 && master_key.match?(/\A[a-f0-9]+\z/) + File.write(master_key_path, master_key) + puts "✓ Master key saved successfully" + else + puts "⚠ Invalid master.key format. Expected 32 character hex string." + puts " You'll need to set this up manually later." + end + when "2" + puts "Creating new credentials file..." + FileUtils.rm_f(credentials_path) + FileUtils.rm_f(master_key_path) + + # Create new credentials file + system("EDITOR='echo' bin/rails credentials:edit") + + if File.exist?(credentials_example_path) + puts "ℹ Please edit your credentials using: bin/rails credentials:edit" + puts " Use config/credentials.yml.example as a reference" + end + puts "✓ New credentials file created" + when "3" + puts "⚠ Skipping credentials setup" + puts " Run 'bin/rails credentials:edit' manually when ready" + else + puts "⚠ Invalid choice. Skipping credentials setup" + end + elsif File.exist?(credentials_path) && File.exist?(master_key_path) + puts "✓ Rails credentials are properly configured" + elsif !File.exist?(credentials_path) + puts "\n== Creating Rails credentials ==" + if File.exist?(credentials_example_path) + puts "ℹ Creating new credentials file" + puts " Use config/credentials.yml.example as a reference" + end + # Create new credentials file + system("EDITOR='echo' bin/rails credentials:edit") + puts "✓ New credentials file created" + puts "ℹ Please edit your credentials using: bin/rails credentials:edit" + end +end + FileUtils.chdir APP_ROOT do # This script is a way to set up or update your development environment automatically. # This script is idempotent, so that you can run it at any time and get an expectable outcome. @@ -18,10 +135,14 @@ FileUtils.chdir APP_ROOT do # Install JavaScript dependencies system("npm install --check-files") - # puts "\n== Copying sample files ==" - # unless File.exist?("config/database.yml") - # FileUtils.cp "config/database.yml.sample", "config/database.yml" - # end + # Setup environment file + setup_environment_file + + # Setup database names + setup_database_names + + # Setup Rails credentials + setup_credentials puts "\n== Preparing database ==" system! "bin/rails db:prepare" @@ -29,8 +150,18 @@ FileUtils.chdir APP_ROOT do puts "\n== Removing old logs and tempfiles ==" system! "bin/rails log:clear tmp:clear" + puts "\n== Setup Complete! ==" + puts "" + puts "Next steps:" + puts "1. Edit your .env file to configure environment variables" + puts "2. Edit Rails credentials with: bin/rails credentials:edit" + puts " - Add your Clerk API keys" + puts " - Add Postmark API token for production" + puts " - See config/credentials.yml.example for reference" + puts "" + unless ARGV.include?("--skip-server") - puts "\n== Starting development server ==" + puts "== Starting development server ==" STDOUT.flush # flush the output before exec(2) so that it displays exec "bin/dev" end diff --git a/config/credentials.yml.enc b/config/credentials.yml.enc index b0e86bda..46cc6eb6 100644 --- a/config/credentials.yml.enc +++ b/config/credentials.yml.enc @@ -1 +1 @@ -gex42RPdT+jZDItMipbhid3J9lzkgbzVbGqsGxAOCFxJdUopXO8ZxnXs/FAAtjk1SPrPp90QYVp9nlWxgPb7UtknU6p5NqoKSCdYnLo7ojRbhK/BsIYjCTh2NFzxin6RyBL1F1uvkcBnDIv6WM1U1H0dnRrU0xQd6LZ7rlrqOyXKYCq7ydyr+KPNfZRTgd7WfN5nkkuYlB+5eaq+q/BN/+WkllA7CVzlRDrrvvuHFsSRFuUsrvFbaytYRKUhqHulNy0Qr1aJwTso4qPkKbMSjWMCVu/523AGD0vvOitRJOqljX47uaN3b3ByjA/5rW/tjuRgaIP6SxHasaZoUv9sSr3pvABeaVWjfk+bVXETGu8Mq7RB5pEmGorHRpJdZpnLUarwEdP37gmzOTpITwunfDbY2QBGovHDDGSdKFiXw4vt389WUybCcipRRLhsxOLFn3vOrJZAwP25PxGaHUe0aZjW/EB32M+898wzi+D7iv9I06qpdtY8MzcHx4Tadl7mupEsmpHIrtvFtxQ6GqlKbgQdXb27kBwQNaQCGmtLgX6htbDrGec1lXSTii3Jo2g9V2q4EUz2x7vQJ/FiwXAk3OhY/nIf12+72xEeCFk4Q+F5qf6wgAM700g69H699BNRAH0p4JnulHWoY5ozgBA95GdpcUYtZ+IIiWr/LGeUMMSiMeYnPwMxpcMbFmD5IxSHdxEwRBjtfcmu47iSGTEOXpMn/8FQGvHr9fWl9GFyua1oHBNSYvZ9e2VwSGVHH8mnAaPJUGMGmpPVMuQq--fc6WOfN2zS6H8Wyk--rK4CdjEfN3nOxCKvJWibjw== \ No newline at end of file +mi9WZDXWFRs40qheGLbrxxKu94K8kNolg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