Rails 8.1 + SQLite app - 2026-02-04 22:51:42

This commit is contained in:
2026-02-04 22:51:42 +08:00
commit 70f4740359
12 changed files with 699 additions and 0 deletions

123
start.sh Executable file
View File

@@ -0,0 +1,123 @@
#!/bin/bash
# 简化的Rails应用启动脚本
set -e
echo "🚀 启动Rails应用..."
# 如果没有Rails应用创建一个
if [ ! -f config/database.yml ]; then
echo "📝 创建新的Rails应用..."
rails new . --force --database=sqlite3 --skip-git --skip-bundle
fi
# 安装依赖
echo "📦 安装依赖..."
bundle install
# 创建数据库
echo "🗄️ 创建数据库..."
rails db:create db:migrate || true
# 创建示例控制器
if [ ! -f app/controllers/welcome_controller.rb ]; then
echo "📝 创建示例页面..."
cat > app/controllers/welcome_controller.rb <<'RUBY'
class WelcomeController < ApplicationController
def index
@hostname = Socket.gethostname
@rails_version = Rails.version
@ruby_version = RUBY_VERSION
end
end
RUBY
mkdir -p app/views/welcome
cat > app/views/welcome/index.html.erb <<'HTML'
<!DOCTYPE html>
<html>
<head>
<title>Rails 8.1 on K3s</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.container {
background: white;
padding: 40px;
border-radius: 10px;
box-shadow: 0 10px 40px rgba(0,0,0,0.2);
text-align: center;
max-width: 600px;
}
h1 {
color: #667eea;
margin-bottom: 20px;
}
.status {
background: #10b981;
color: white;
padding: 10px 20px;
border-radius: 5px;
display: inline-block;
margin: 20px 0;
}
.info {
text-align: left;
background: #f3f4f6;
padding: 20px;
border-radius: 5px;
margin-top: 20px;
}
.info p {
margin: 10px 0;
}
.emoji {
font-size: 48px;
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="container">
<div class="emoji">💎</div>
<h1>Rails <%= @rails_version %> on K3s</h1>
<div class="status">✅ 运行正常</div>
<div class="info">
<p><strong>Ruby版本:</strong> <%= @ruby_version %></p>
<p><strong>Rails版本:</strong> <%= @rails_version %></p>
<p><strong>数据库:</strong> SQLite3</p>
<p><strong>Pod主机名:</strong> <%= @hostname %></p>
<p><strong>访问域名:</strong> r1.jpd.net3w.com</p>
<p><strong>部署方式:</strong> GitOps (Gitea + ArgoCD)</p>
<p><strong>集群:</strong> JPD K3s Cluster</p>
</div>
<p style="margin-top: 20px; color: #6b7280;">
<%= Time.now.strftime("%Y-%m-%d %H:%M:%S") %>
</p>
</div>
</body>
</html>
HTML
# 配置路由
if ! grep -q "root.*welcome#index" config/routes.rb 2>/dev/null; then
cat > config/routes.rb <<'RUBY'
Rails.application.routes.draw do
root "welcome#index"
get "up" => "rails/health#show", as: :rails_health_check
end
RUBY
fi
fi
# 启动服务器
echo "🌐 启动Rails服务器..."
exec rails server -b 0.0.0.0 -p 3000