scelebi's picture
Create public/index.php
58e98e4 verified
<?php
$dbPath = getenv("DB_PATH");
if (!$dbPath) {
$dbPath = __DIR__ . "/../db/app.sqlite";
}
@mkdir(dirname($dbPath), 0777, true);
$pdo = new PDO("sqlite:" . $dbPath);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec("CREATE TABLE IF NOT EXISTS notes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
msg TEXT NOT NULL,
created_at TEXT NOT NULL
)");
$count = (int)$pdo->query("SELECT COUNT(*) FROM notes")->fetchColumn();
if ($count === 0) {
$stmt = $pdo->prepare("INSERT INTO notes (msg, created_at) VALUES (?, datetime('now'))");
$stmt->execute(["Hello from Hugging Face Space"]);
}
$rows = $pdo->query("SELECT id, msg, created_at FROM notes ORDER BY id DESC LIMIT 10")
->fetchAll(PDO::FETCH_ASSOC);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>PHP + SQLite on Hugging Face</title>
<style>
body{font-family:system-ui,Arial;margin:24px;max-width:900px}
code{background:#f3f3f3;padding:2px 6px;border-radius:6px}
table{border-collapse:collapse;width:100%;margin-top:12px}
th,td{border:1px solid #ddd;padding:8px;text-align:left}
</style>
</head>
<body>
<h1>PHP + SQLite (Hugging Face Docker Space)</h1>
<p>DB Path: <code><?= htmlspecialchars($dbPath) ?></code></p>
<form method="post">
<input name="msg" placeholder="Yeni not..." style="padding:8px;width:70%" />
<button style="padding:8px 12px">Ekle</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["msg"])) {
$m = trim($_POST["msg"]);
if ($m !== "") {
$stmt = $pdo->prepare("INSERT INTO notes (msg, created_at) VALUES (?, datetime('now'))");
$stmt->execute([$m]);
header("Location: " . $_SERVER["REQUEST_URI"]);
exit;
}
}
?>
<h2>Son 10 kayıt</h2>
<table>
<thead><tr><th>ID</th><th>Mesaj</th><th>Tarih</th></tr></thead>
<tbody>
<?php foreach ($rows as $r): ?>
<tr>
<td><?= (int)$r["id"] ?></td>
<td><?= htmlspecialchars($r["msg"]) ?></td>
<td><?= htmlspecialchars($r["created_at"]) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<p><small>Not: Free tier’da disk ephemeraldir; restart/stop sonrasi DB sıfırlanabilir.</small></p>
</body>
</html>