tina4ruby 0.5.2 → 3.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1 -1
- data/README.md +434 -544
- data/exe/{tina4 → tina4ruby} +1 -0
- data/lib/tina4/ai.rb +312 -0
- data/lib/tina4/auth.rb +44 -3
- data/lib/tina4/auto_crud.rb +163 -0
- data/lib/tina4/cli.rb +389 -97
- data/lib/tina4/constants.rb +46 -0
- data/lib/tina4/cors.rb +74 -0
- data/lib/tina4/database/sqlite3_adapter.rb +139 -0
- data/lib/tina4/database.rb +144 -7
- data/lib/tina4/debug.rb +4 -79
- data/lib/tina4/dev_admin.rb +1162 -0
- data/lib/tina4/dev_mailbox.rb +191 -0
- data/lib/tina4/dev_reload.rb +9 -9
- data/lib/tina4/drivers/firebird_driver.rb +19 -3
- data/lib/tina4/drivers/mssql_driver.rb +3 -3
- data/lib/tina4/drivers/mysql_driver.rb +4 -4
- data/lib/tina4/drivers/postgres_driver.rb +9 -2
- data/lib/tina4/drivers/sqlite_driver.rb +1 -1
- data/lib/tina4/env.rb +42 -2
- data/lib/tina4/error_overlay.rb +252 -0
- data/lib/tina4/events.rb +90 -0
- data/lib/tina4/field_types.rb +4 -0
- data/lib/tina4/frond.rb +1497 -0
- data/lib/tina4/gallery/auth/meta.json +1 -0
- data/lib/tina4/gallery/auth/src/routes/api/gallery_auth.rb +114 -0
- data/lib/tina4/gallery/database/meta.json +1 -0
- data/lib/tina4/gallery/database/src/routes/api/gallery_db.rb +43 -0
- data/lib/tina4/gallery/error-overlay/meta.json +1 -0
- data/lib/tina4/gallery/error-overlay/src/routes/api/gallery_crash.rb +17 -0
- data/lib/tina4/gallery/orm/meta.json +1 -0
- data/lib/tina4/gallery/orm/src/routes/api/gallery_products.rb +16 -0
- data/lib/tina4/gallery/queue/meta.json +1 -0
- data/lib/tina4/gallery/queue/src/routes/api/gallery_queue.rb +325 -0
- data/lib/tina4/gallery/rest-api/meta.json +1 -0
- data/lib/tina4/gallery/rest-api/src/routes/api/gallery_hello.rb +14 -0
- data/lib/tina4/gallery/templates/meta.json +1 -0
- data/lib/tina4/gallery/templates/src/routes/gallery_page.rb +12 -0
- data/lib/tina4/gallery/templates/src/templates/gallery_page.twig +257 -0
- data/lib/tina4/health.rb +39 -0
- data/lib/tina4/html_element.rb +148 -0
- data/lib/tina4/localization.rb +2 -2
- data/lib/tina4/log.rb +203 -0
- data/lib/tina4/messenger.rb +562 -0
- data/lib/tina4/migration.rb +132 -29
- data/lib/tina4/orm.rb +463 -35
- data/lib/tina4/public/css/tina4.css +178 -1
- data/lib/tina4/public/css/tina4.min.css +1 -2
- data/lib/tina4/public/favicon.ico +0 -0
- data/lib/tina4/public/images/logo.svg +5 -0
- data/lib/tina4/public/images/tina4-logo-icon.webp +0 -0
- data/lib/tina4/public/js/frond.min.js +420 -0
- data/lib/tina4/public/js/tina4-dev-admin.min.js +367 -0
- data/lib/tina4/public/js/tina4.min.js +93 -0
- data/lib/tina4/public/swagger/index.html +90 -0
- data/lib/tina4/public/swagger/oauth2-redirect.html +63 -0
- data/lib/tina4/queue.rb +162 -6
- data/lib/tina4/queue_backends/lite_backend.rb +88 -0
- data/lib/tina4/rack_app.rb +331 -27
- data/lib/tina4/rate_limiter.rb +123 -0
- data/lib/tina4/request.rb +61 -15
- data/lib/tina4/response.rb +54 -24
- data/lib/tina4/response_cache.rb +551 -0
- data/lib/tina4/router.rb +90 -15
- data/lib/tina4/scss_compiler.rb +2 -2
- data/lib/tina4/seeder.rb +56 -61
- data/lib/tina4/service_runner.rb +303 -0
- data/lib/tina4/session.rb +85 -0
- data/lib/tina4/session_handlers/mongo_handler.rb +1 -1
- data/lib/tina4/session_handlers/valkey_handler.rb +43 -0
- data/lib/tina4/shutdown.rb +84 -0
- data/lib/tina4/sql_translation.rb +295 -0
- data/lib/tina4/template.rb +36 -6
- data/lib/tina4/templates/base.twig +2 -2
- data/lib/tina4/templates/errors/302.twig +14 -0
- data/lib/tina4/templates/errors/401.twig +9 -0
- data/lib/tina4/templates/errors/403.twig +22 -15
- data/lib/tina4/templates/errors/404.twig +22 -15
- data/lib/tina4/templates/errors/500.twig +31 -15
- data/lib/tina4/templates/errors/502.twig +9 -0
- data/lib/tina4/templates/errors/503.twig +12 -0
- data/lib/tina4/templates/errors/base.twig +37 -0
- data/lib/tina4/version.rb +1 -1
- data/lib/tina4/webserver.rb +28 -18
- data/lib/tina4.rb +118 -21
- metadata +68 -8
- data/lib/tina4/public/js/tina4.js +0 -134
- data/lib/tina4/public/js/tina4helper.js +0 -387
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name": "Auth", "description": "JWT login form with token display", "try_url": "/gallery/auth"}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# Gallery: Auth — JWT login with a visual demo page.
|
|
2
|
+
|
|
3
|
+
Tina4::Router.get("/gallery/auth") do |request, response|
|
|
4
|
+
html = <<~HTML
|
|
5
|
+
<!DOCTYPE html>
|
|
6
|
+
<html lang="en">
|
|
7
|
+
<head>
|
|
8
|
+
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
|
9
|
+
<title>Auth Demo</title><link rel="stylesheet" href="/css/tina4.min.css">
|
|
10
|
+
</head>
|
|
11
|
+
<body class="bg-dark text-light">
|
|
12
|
+
<div class="container mt-5" style="max-width:600px;">
|
|
13
|
+
<h2 class="mb-4">JWT Authentication Demo</h2>
|
|
14
|
+
<div class="card mb-4">
|
|
15
|
+
<div class="card-header bg-primary text-white">Login</div>
|
|
16
|
+
<div class="card-body">
|
|
17
|
+
<div class="form-group mb-3">
|
|
18
|
+
<label class="form-label">Username</label>
|
|
19
|
+
<input type="text" id="username" class="form-control" placeholder="admin" value="admin">
|
|
20
|
+
</div>
|
|
21
|
+
<div class="form-group mb-3">
|
|
22
|
+
<label class="form-label">Password</label>
|
|
23
|
+
<input type="password" id="password" class="form-control" placeholder="secret" value="secret">
|
|
24
|
+
</div>
|
|
25
|
+
<button class="btn btn-primary" onclick="doLogin()">Login</button>
|
|
26
|
+
</div>
|
|
27
|
+
</div>
|
|
28
|
+
<div id="result" style="display:none;">
|
|
29
|
+
<div class="card mb-3">
|
|
30
|
+
<div class="card-header bg-success text-white">Token Received</div>
|
|
31
|
+
<div class="card-body">
|
|
32
|
+
<pre id="token" style="word-break:break-all;white-space:pre-wrap;color:#4ade80;background:#1e293b;padding:1rem;border-radius:0.5rem;"></pre>
|
|
33
|
+
</div>
|
|
34
|
+
</div>
|
|
35
|
+
<div class="card mb-3">
|
|
36
|
+
<div class="card-header">Token Payload (decoded)</div>
|
|
37
|
+
<div class="card-body">
|
|
38
|
+
<pre id="payload" style="color:#38bdf8;background:#1e293b;padding:1rem;border-radius:0.5rem;"></pre>
|
|
39
|
+
</div>
|
|
40
|
+
</div>
|
|
41
|
+
<button class="btn btn-outline-info" onclick="verifyToken()">Verify Token</button>
|
|
42
|
+
<span id="verify-result" class="ms-2"></span>
|
|
43
|
+
</div>
|
|
44
|
+
<div class="card bg-dark mt-4" style="border:1px solid #334155;">
|
|
45
|
+
<div class="card-body">
|
|
46
|
+
<h6 style="color:#e2e8f0;">How it works</h6>
|
|
47
|
+
<pre style="background:#0f172a;color:#4ade80;padding:1rem;border-radius:0.5rem;font-size:0.8rem;"><code>token = Tina4::Auth.create_token({ username: "admin" })
|
|
48
|
+
payload = Tina4::Auth.get_payload(token)
|
|
49
|
+
result = Tina4::Auth.validate_token(token)</code></pre>
|
|
50
|
+
</div>
|
|
51
|
+
</div>
|
|
52
|
+
</div>
|
|
53
|
+
<script>
|
|
54
|
+
var currentToken = '';
|
|
55
|
+
function doLogin() {
|
|
56
|
+
fetch('/api/gallery/auth/login', {
|
|
57
|
+
method: 'POST',
|
|
58
|
+
headers: {'Content-Type': 'application/json'},
|
|
59
|
+
body: JSON.stringify({
|
|
60
|
+
username: document.getElementById('username').value,
|
|
61
|
+
password: document.getElementById('password').value
|
|
62
|
+
})
|
|
63
|
+
}).then(r => r.json()).then(d => {
|
|
64
|
+
if (d.token) {
|
|
65
|
+
currentToken = d.token;
|
|
66
|
+
document.getElementById('token').textContent = d.token;
|
|
67
|
+
try {
|
|
68
|
+
var parts = d.token.split('.');
|
|
69
|
+
var payload = JSON.parse(atob(parts[1]));
|
|
70
|
+
document.getElementById('payload').textContent = JSON.stringify(payload, null, 2);
|
|
71
|
+
} catch(e) {
|
|
72
|
+
document.getElementById('payload').textContent = 'Could not decode';
|
|
73
|
+
}
|
|
74
|
+
document.getElementById('result').style.display = 'block';
|
|
75
|
+
document.getElementById('verify-result').textContent = '';
|
|
76
|
+
} else {
|
|
77
|
+
alert(d.error || 'Login failed');
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
function verifyToken() {
|
|
82
|
+
fetch('/api/gallery/auth/verify?token=' + encodeURIComponent(currentToken))
|
|
83
|
+
.then(r => r.json()).then(d => {
|
|
84
|
+
var el = document.getElementById('verify-result');
|
|
85
|
+
if (d.valid) {
|
|
86
|
+
el.innerHTML = '<span class="badge bg-success">Valid</span>';
|
|
87
|
+
} else {
|
|
88
|
+
el.innerHTML = '<span class="badge bg-danger">Invalid</span>';
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
</script>
|
|
93
|
+
</body></html>
|
|
94
|
+
HTML
|
|
95
|
+
response.html(html)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
Tina4::Router.post("/api/gallery/auth/login") do |request, response|
|
|
99
|
+
body = request.body || {}
|
|
100
|
+
username = body["username"].to_s
|
|
101
|
+
password = body["password"].to_s
|
|
102
|
+
if !username.empty? && !password.empty?
|
|
103
|
+
token = Tina4::Auth.create_token({ username: username, role: "user" })
|
|
104
|
+
response.json({ token: token, message: "Welcome #{username}!" })
|
|
105
|
+
else
|
|
106
|
+
response.json({ error: "Username and password required" }, 401)
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
Tina4::Router.get("/api/gallery/auth/verify") do |request, response|
|
|
111
|
+
token = request.params["token"].to_s
|
|
112
|
+
result = Tina4::Auth.validate_token(token)
|
|
113
|
+
response.json({ valid: result[:valid] })
|
|
114
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name": "Database", "description": "Raw SQL queries with the Database class", "try_url": "/api/gallery/db/tables"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Gallery: Database — raw SQL query demo.
|
|
2
|
+
|
|
3
|
+
Tina4::Router.get("/api/gallery/db/tables") do |request, response|
|
|
4
|
+
begin
|
|
5
|
+
db = Tina4::Database.new("sqlite://data/gallery.db")
|
|
6
|
+
db.execute(<<~SQL)
|
|
7
|
+
CREATE TABLE IF NOT EXISTS gallery_notes (
|
|
8
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
9
|
+
title TEXT NOT NULL,
|
|
10
|
+
body TEXT,
|
|
11
|
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
12
|
+
)
|
|
13
|
+
SQL
|
|
14
|
+
tables = db.tables
|
|
15
|
+
response.json({ tables: tables, engine: "sqlite" })
|
|
16
|
+
rescue => e
|
|
17
|
+
response.json({ error: e.message }, 500)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
Tina4::Router.post("/api/gallery/db/notes") do |request, response|
|
|
22
|
+
begin
|
|
23
|
+
db = Tina4::Database.new("sqlite://data/gallery.db")
|
|
24
|
+
body = request.body || {}
|
|
25
|
+
db.insert("gallery_notes", {
|
|
26
|
+
title: body["title"] || "Untitled",
|
|
27
|
+
body: body["body"] || ""
|
|
28
|
+
})
|
|
29
|
+
response.json({ created: true }, 201)
|
|
30
|
+
rescue => e
|
|
31
|
+
response.json({ error: e.message }, 500)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
Tina4::Router.get("/api/gallery/db/notes") do |request, response|
|
|
36
|
+
begin
|
|
37
|
+
db = Tina4::Database.new("sqlite://data/gallery.db")
|
|
38
|
+
result = db.fetch("SELECT * FROM gallery_notes ORDER BY id DESC", [], limit: 50)
|
|
39
|
+
response.json(result.to_a)
|
|
40
|
+
rescue => e
|
|
41
|
+
response.json({ error: e.message }, 500)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name": "Error Overlay", "description": "See the rich debug error page with stack trace and source code", "try_url": "/api/gallery/crash"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Gallery: Error Overlay — deliberately crash to demo the debug overlay.
|
|
2
|
+
#
|
|
3
|
+
# This route deliberately raises an error to showcase the error overlay.
|
|
4
|
+
#
|
|
5
|
+
# In debug mode (TINA4_DEBUG=true), you will see:
|
|
6
|
+
# - Exception type and message
|
|
7
|
+
# - Stack trace with syntax-highlighted source code
|
|
8
|
+
# - The exact line that caused the error (highlighted)
|
|
9
|
+
# - Request details (method, path, headers)
|
|
10
|
+
# - Environment info (framework version, Ruby version)
|
|
11
|
+
|
|
12
|
+
Tina4::Router.get("/api/gallery/crash") do |request, response|
|
|
13
|
+
# Simulate a realistic error — accessing a missing key
|
|
14
|
+
user = { name: "Alice", email: "alice@example.com" }
|
|
15
|
+
role = user.fetch(:role) # KeyError: key not found: :role — this line will be highlighted in the overlay
|
|
16
|
+
response.json({ role: role })
|
|
17
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name": "ORM", "description": "Product model with CRUD endpoints", "try_url": "/api/gallery/products"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Gallery: ORM — Product CRUD endpoints.
|
|
2
|
+
|
|
3
|
+
Tina4::Router.get("/api/gallery/products") do |request, response|
|
|
4
|
+
response.json({
|
|
5
|
+
products: [
|
|
6
|
+
{ id: 1, name: "Widget", price: 9.99 },
|
|
7
|
+
{ id: 2, name: "Gadget", price: 24.99 }
|
|
8
|
+
],
|
|
9
|
+
note: "Connect a database and deploy the ORM model for live data"
|
|
10
|
+
})
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
Tina4::Router.post("/api/gallery/products") do |request, response|
|
|
14
|
+
data = request.body || {}
|
|
15
|
+
response.json({ created: data, id: 3 }, 201)
|
|
16
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name": "Queue", "description": "Background job producer and consumer", "try_url": "/gallery/queue"}
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
# Gallery: Queue — interactive queue demo with visual web UI.
|
|
2
|
+
#
|
|
3
|
+
# Uses a SQLite database directly for the demo queue table,
|
|
4
|
+
# matching the Python gallery demo's database-backed approach.
|
|
5
|
+
|
|
6
|
+
require "json"
|
|
7
|
+
|
|
8
|
+
def _gallery_queue_db
|
|
9
|
+
@_gallery_queue_db ||= begin
|
|
10
|
+
db = Tina4::Database.new("sqlite://data/gallery_queue.db")
|
|
11
|
+
unless db.table_exists?("tina4_queue")
|
|
12
|
+
db.execute("CREATE TABLE tina4_queue (
|
|
13
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
14
|
+
topic TEXT NOT NULL,
|
|
15
|
+
data TEXT NOT NULL,
|
|
16
|
+
status TEXT NOT NULL DEFAULT 'pending',
|
|
17
|
+
priority INTEGER NOT NULL DEFAULT 0,
|
|
18
|
+
attempts INTEGER NOT NULL DEFAULT 0,
|
|
19
|
+
error TEXT,
|
|
20
|
+
available_at TEXT NOT NULL,
|
|
21
|
+
created_at TEXT NOT NULL,
|
|
22
|
+
completed_at TEXT,
|
|
23
|
+
reserved_at TEXT
|
|
24
|
+
)")
|
|
25
|
+
end
|
|
26
|
+
db
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def _gallery_queue_now
|
|
31
|
+
Time.now.utc.iso8601
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
GALLERY_QUEUE_MAX_RETRIES = 3
|
|
35
|
+
|
|
36
|
+
GALLERY_QUEUE_HTML = <<~'HTML'
|
|
37
|
+
<!DOCTYPE html>
|
|
38
|
+
<html lang="en">
|
|
39
|
+
<head>
|
|
40
|
+
<meta charset="UTF-8">
|
|
41
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
42
|
+
<title>Queue Gallery — Tina4 Ruby</title>
|
|
43
|
+
<link rel="stylesheet" href="/css/tina4.min.css">
|
|
44
|
+
</head>
|
|
45
|
+
<body>
|
|
46
|
+
<div class="container mt-4 mb-4">
|
|
47
|
+
<h1>Queue Gallery</h1>
|
|
48
|
+
<p class="text-muted">Interactive demo of Tina4's database-backed job queue. Produce messages, consume them, simulate failures, and inspect dead letters.</p>
|
|
49
|
+
|
|
50
|
+
<div class="row mt-3">
|
|
51
|
+
<div class="col-md-6">
|
|
52
|
+
<div class="card">
|
|
53
|
+
<div class="card-header">Produce a Message</div>
|
|
54
|
+
<div class="card-body">
|
|
55
|
+
<div class="d-flex gap-2">
|
|
56
|
+
<input type="text" id="msgInput" class="form-control" placeholder="Enter a task message, e.g. send-email">
|
|
57
|
+
<button class="btn btn-primary" onclick="produce()">Produce</button>
|
|
58
|
+
</div>
|
|
59
|
+
</div>
|
|
60
|
+
</div>
|
|
61
|
+
</div>
|
|
62
|
+
<div class="col-md-6">
|
|
63
|
+
<div class="card">
|
|
64
|
+
<div class="card-header">Actions</div>
|
|
65
|
+
<div class="card-body d-flex gap-2 flex-wrap">
|
|
66
|
+
<button class="btn btn-success" onclick="consume()">Consume Next</button>
|
|
67
|
+
<button class="btn btn-danger" onclick="failNext()">Fail Next</button>
|
|
68
|
+
<button class="btn btn-warning" onclick="retryFailed()">Retry Failed</button>
|
|
69
|
+
<button class="btn btn-secondary" onclick="refresh()">Refresh</button>
|
|
70
|
+
</div>
|
|
71
|
+
</div>
|
|
72
|
+
</div>
|
|
73
|
+
</div>
|
|
74
|
+
|
|
75
|
+
<div id="alertArea" class="mt-3"></div>
|
|
76
|
+
|
|
77
|
+
<div class="card mt-3">
|
|
78
|
+
<div class="card-header d-flex justify-content-between align-items-center">
|
|
79
|
+
<span>Queue Messages</span>
|
|
80
|
+
<small class="text-muted" id="lastRefresh"></small>
|
|
81
|
+
</div>
|
|
82
|
+
<div class="card-body p-0">
|
|
83
|
+
<table class="table table-striped mb-0">
|
|
84
|
+
<thead>
|
|
85
|
+
<tr>
|
|
86
|
+
<th>ID</th>
|
|
87
|
+
<th>Data</th>
|
|
88
|
+
<th>Status</th>
|
|
89
|
+
<th>Attempts</th>
|
|
90
|
+
<th>Error</th>
|
|
91
|
+
<th>Created</th>
|
|
92
|
+
</tr>
|
|
93
|
+
</thead>
|
|
94
|
+
<tbody id="queueBody">
|
|
95
|
+
<tr><td colspan="6" class="text-center text-muted">Loading...</td></tr>
|
|
96
|
+
</tbody>
|
|
97
|
+
</table>
|
|
98
|
+
</div>
|
|
99
|
+
</div>
|
|
100
|
+
</div>
|
|
101
|
+
|
|
102
|
+
<script>
|
|
103
|
+
function statusBadge(status) {
|
|
104
|
+
var colors = {pending:"primary", reserved:"warning", completed:"success", failed:"danger", dead:"secondary"};
|
|
105
|
+
var color = colors[status] || "secondary";
|
|
106
|
+
return '<span class="badge bg-' + color + '">' + status + '</span>';
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function showAlert(msg, type) {
|
|
110
|
+
var area = document.getElementById("alertArea");
|
|
111
|
+
area.innerHTML = '<div class="alert alert-' + type + ' alert-dismissible">' + msg +
|
|
112
|
+
'<button type="button" class="btn-close" onclick="this.parentElement.remove()"></button></div>';
|
|
113
|
+
setTimeout(function(){ area.innerHTML = ""; }, 3000);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function truncate(s, n) {
|
|
117
|
+
if (!s) return "";
|
|
118
|
+
return s.length > n ? s.substring(0, n) + "..." : s;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function refresh() {
|
|
122
|
+
try {
|
|
123
|
+
var r = await fetch("/api/gallery/queue/status");
|
|
124
|
+
var data = await r.json();
|
|
125
|
+
var tbody = document.getElementById("queueBody");
|
|
126
|
+
if (!data.messages || data.messages.length === 0) {
|
|
127
|
+
tbody.innerHTML = '<tr><td colspan="6" class="text-center text-muted">No messages in queue. Produce one above.</td></tr>';
|
|
128
|
+
} else {
|
|
129
|
+
var html = "";
|
|
130
|
+
for (var i = 0; i < data.messages.length; i++) {
|
|
131
|
+
var m = data.messages[i];
|
|
132
|
+
html += "<tr><td>" + m.id + "</td><td><code>" + truncate(m.data, 60) + "</code></td><td>" +
|
|
133
|
+
statusBadge(m.status) + "</td><td>" + m.attempts + "</td><td>" +
|
|
134
|
+
truncate(m.error || "", 40) + "</td><td><small>" + (m.created_at || "") + "</small></td></tr>";
|
|
135
|
+
}
|
|
136
|
+
tbody.innerHTML = html;
|
|
137
|
+
}
|
|
138
|
+
document.getElementById("lastRefresh").textContent = "Updated " + new Date().toLocaleTimeString();
|
|
139
|
+
} catch (e) {
|
|
140
|
+
console.error(e);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async function produce() {
|
|
145
|
+
var input = document.getElementById("msgInput");
|
|
146
|
+
var task = input.value.trim() || "demo-task";
|
|
147
|
+
var r = await fetch("/api/gallery/queue/produce", {
|
|
148
|
+
method: "POST", headers: {"Content-Type":"application/json"},
|
|
149
|
+
body: JSON.stringify({task: task, data: {message: task}})
|
|
150
|
+
});
|
|
151
|
+
var d = await r.json();
|
|
152
|
+
showAlert("Produced message: " + task, "success");
|
|
153
|
+
input.value = "";
|
|
154
|
+
refresh();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async function consume() {
|
|
158
|
+
var r = await fetch("/api/gallery/queue/consume", {method:"POST"});
|
|
159
|
+
var d = await r.json();
|
|
160
|
+
if (d.consumed) {
|
|
161
|
+
showAlert("Consumed job #" + d.job_id + " successfully", "success");
|
|
162
|
+
} else {
|
|
163
|
+
showAlert(d.message || "Nothing to consume", "info");
|
|
164
|
+
}
|
|
165
|
+
refresh();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
async function failNext() {
|
|
169
|
+
var r = await fetch("/api/gallery/queue/fail", {method:"POST"});
|
|
170
|
+
var d = await r.json();
|
|
171
|
+
if (d.failed) {
|
|
172
|
+
showAlert("Deliberately failed job #" + d.job_id, "danger");
|
|
173
|
+
} else {
|
|
174
|
+
showAlert(d.message || "Nothing to fail", "info");
|
|
175
|
+
}
|
|
176
|
+
refresh();
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async function retryFailed() {
|
|
180
|
+
var r = await fetch("/api/gallery/queue/retry", {method:"POST"});
|
|
181
|
+
var d = await r.json();
|
|
182
|
+
showAlert("Retried " + (d.retried || 0) + " failed message(s)", "warning");
|
|
183
|
+
refresh();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
refresh();
|
|
187
|
+
setInterval(refresh, 2000);
|
|
188
|
+
</script>
|
|
189
|
+
</body>
|
|
190
|
+
</html>
|
|
191
|
+
HTML
|
|
192
|
+
|
|
193
|
+
# ── Render the interactive HTML page ──────────────────────────
|
|
194
|
+
|
|
195
|
+
Tina4::Router.get("/gallery/queue") do |request, response|
|
|
196
|
+
response.html(GALLERY_QUEUE_HTML)
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
# ── Produce — add a message to the queue ──────────────────────
|
|
200
|
+
|
|
201
|
+
Tina4::Router.post("/api/gallery/queue/produce") do |request, response|
|
|
202
|
+
body = request.body || {}
|
|
203
|
+
task = body["task"] || "default-task"
|
|
204
|
+
data = body["data"] || {}
|
|
205
|
+
|
|
206
|
+
db = _gallery_queue_db
|
|
207
|
+
now = _gallery_queue_now
|
|
208
|
+
payload = JSON.generate({ task: task, data: data })
|
|
209
|
+
|
|
210
|
+
db.execute(
|
|
211
|
+
"INSERT INTO tina4_queue (topic, data, status, priority, attempts, available_at, created_at) VALUES (?, ?, 'pending', 0, 0, ?, ?)",
|
|
212
|
+
["gallery-tasks", payload, now, now]
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
row = db.fetch_one("SELECT last_insert_rowid() as last_id")
|
|
216
|
+
job_id = row ? row["last_id"] : 0
|
|
217
|
+
|
|
218
|
+
response.json({ queued: true, task: task, job_id: job_id }, 201)
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
# ── Status — list all messages with statuses ──────────────────
|
|
222
|
+
|
|
223
|
+
Tina4::Router.get("/api/gallery/queue/status") do |request, response|
|
|
224
|
+
db = _gallery_queue_db
|
|
225
|
+
|
|
226
|
+
result = db.fetch(
|
|
227
|
+
"SELECT * FROM tina4_queue WHERE topic = ? ORDER BY id DESC",
|
|
228
|
+
["gallery-tasks"],
|
|
229
|
+
limit: 100
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
messages = []
|
|
233
|
+
counts = { pending: 0, reserved: 0, completed: 0, failed: 0 }
|
|
234
|
+
|
|
235
|
+
(result.respond_to?(:records) ? result.records : (result || [])).each do |row|
|
|
236
|
+
status = row["status"] || "pending"
|
|
237
|
+
attempts = (row["attempts"] || 0).to_i
|
|
238
|
+
|
|
239
|
+
display_status = status
|
|
240
|
+
if status == "failed" && attempts >= GALLERY_QUEUE_MAX_RETRIES
|
|
241
|
+
display_status = "dead"
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
counts[status.to_sym] = (counts[status.to_sym] || 0) + 1 if counts.key?(status.to_sym)
|
|
245
|
+
|
|
246
|
+
messages << {
|
|
247
|
+
id: row["id"],
|
|
248
|
+
data: row["data"] || "",
|
|
249
|
+
status: display_status,
|
|
250
|
+
attempts: attempts,
|
|
251
|
+
error: row["error"] || "",
|
|
252
|
+
created_at: row["created_at"] || ""
|
|
253
|
+
}
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
response.json({
|
|
257
|
+
topic: "gallery-tasks",
|
|
258
|
+
messages: messages,
|
|
259
|
+
counts: counts
|
|
260
|
+
})
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
# ── Consume — process the next pending message ────────────────
|
|
264
|
+
|
|
265
|
+
Tina4::Router.post("/api/gallery/queue/consume") do |request, response|
|
|
266
|
+
db = _gallery_queue_db
|
|
267
|
+
now = _gallery_queue_now
|
|
268
|
+
|
|
269
|
+
row = db.fetch_one(
|
|
270
|
+
"SELECT * FROM tina4_queue WHERE topic = ? AND status = 'pending' AND available_at <= ? ORDER BY priority DESC, id ASC",
|
|
271
|
+
["gallery-tasks", now]
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
if row.nil?
|
|
275
|
+
response.json({ consumed: false, message: "No pending messages to consume" })
|
|
276
|
+
else
|
|
277
|
+
db.execute(
|
|
278
|
+
"UPDATE tina4_queue SET status = 'completed', completed_at = ? WHERE id = ? AND status = 'pending'",
|
|
279
|
+
[now, row["id"]]
|
|
280
|
+
)
|
|
281
|
+
response.json({ consumed: true, job_id: row["id"], data: row["data"] })
|
|
282
|
+
end
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
# ── Fail — deliberately fail the next pending message ─────────
|
|
286
|
+
|
|
287
|
+
Tina4::Router.post("/api/gallery/queue/fail") do |request, response|
|
|
288
|
+
db = _gallery_queue_db
|
|
289
|
+
now = _gallery_queue_now
|
|
290
|
+
|
|
291
|
+
row = db.fetch_one(
|
|
292
|
+
"SELECT * FROM tina4_queue WHERE topic = ? AND status = 'pending' AND available_at <= ? ORDER BY priority DESC, id ASC",
|
|
293
|
+
["gallery-tasks", now]
|
|
294
|
+
)
|
|
295
|
+
|
|
296
|
+
if row.nil?
|
|
297
|
+
response.json({ failed: false, message: "No pending messages to fail" })
|
|
298
|
+
else
|
|
299
|
+
db.execute(
|
|
300
|
+
"UPDATE tina4_queue SET status = 'failed', error = ?, attempts = attempts + 1 WHERE id = ?",
|
|
301
|
+
["Deliberately failed via gallery demo", row["id"]]
|
|
302
|
+
)
|
|
303
|
+
response.json({ failed: true, job_id: row["id"], data: row["data"] })
|
|
304
|
+
end
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
# ── Retry — re-queue failed messages ──────────────────────────
|
|
308
|
+
|
|
309
|
+
Tina4::Router.post("/api/gallery/queue/retry") do |request, response|
|
|
310
|
+
db = _gallery_queue_db
|
|
311
|
+
now = _gallery_queue_now
|
|
312
|
+
|
|
313
|
+
db.execute(
|
|
314
|
+
"UPDATE tina4_queue SET status = 'pending', available_at = ? WHERE topic = ? AND status = 'failed' AND attempts < ?",
|
|
315
|
+
[now, "gallery-tasks", GALLERY_QUEUE_MAX_RETRIES]
|
|
316
|
+
)
|
|
317
|
+
|
|
318
|
+
row = db.fetch_one(
|
|
319
|
+
"SELECT COUNT(*) as cnt FROM tina4_queue WHERE topic = ? AND status = 'pending'",
|
|
320
|
+
["gallery-tasks"]
|
|
321
|
+
)
|
|
322
|
+
retried = row ? row["cnt"].to_i : 0
|
|
323
|
+
|
|
324
|
+
response.json({ retried: retried })
|
|
325
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name": "REST API", "description": "A simple JSON API with GET and POST endpoints", "try_url": "/api/gallery/hello"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Gallery: REST API — simple JSON endpoints.
|
|
2
|
+
|
|
3
|
+
Tina4::Router.get("/api/gallery/hello") do |request, response|
|
|
4
|
+
response.json({ message: "Hello from Tina4!", method: "GET" })
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
Tina4::Router.get("/api/gallery/hello/{name}") do |request, response|
|
|
8
|
+
response.json({ message: "Hello #{request.params["name"]}!", method: "GET" })
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
Tina4::Router.post("/api/gallery/hello") do |request, response|
|
|
12
|
+
data = request.body || {}
|
|
13
|
+
response.json({ echo: data, method: "POST" }, 201)
|
|
14
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name": "Templates", "description": "Twig template with dynamic data", "try_url": "/gallery/page"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Gallery: Templates — render an HTML page with dynamic data via template.
|
|
2
|
+
|
|
3
|
+
Tina4::Router.get "/gallery/page", template: "gallery_page.twig" do |request, response|
|
|
4
|
+
response.call({
|
|
5
|
+
title: "Gallery Demo Page",
|
|
6
|
+
items: [
|
|
7
|
+
{ name: "Tina4 Ruby", description: "Zero-dep web framework", badge: "v3.0.0" },
|
|
8
|
+
{ name: "Twig Engine", description: "Built-in template rendering", badge: "included" },
|
|
9
|
+
{ name: "Auto-Reload", description: "Templates refresh on save", badge: "dev mode" }
|
|
10
|
+
]
|
|
11
|
+
}, Tina4::HTTP_OK)
|
|
12
|
+
end
|