1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { blob } from "https://esm.town/v/std/blob";
import { Hono } from "npm:hono";
import { html } from "https://esm.town/v/stevekrouse/html";
// Initialize sample stories and store them in blob storage
const SAMPLE_STORIES_KEY = "hn_sample_stories";
async function initializeSampleStories() {
const existingStories = await blob.getJSON(SAMPLE_STORIES_KEY);
if (!existingStories) {
const sampleStories = Array.from({ length: 30 }).map((_, idx) => ({
id: idx + 1,
title: `Sample Story ${idx + 1}`,
url: `https://example.com/story${idx + 1}`,
votes: Math.floor(Math.random() * 100),
}));
await blob.setJSON(SAMPLE_STORIES_KEY, sampleStories);
}
}
await initializeSampleStories();
// Hono app setup
const app = new Hono();
// Serve main page with stories
app.get("/", async (c) => {
const stories = await blob.getJSON(SAMPLE_STORIES_KEY) || [];
const storiesHtml = stories
.sort((a, b) => b.votes - a.votes)
.map(story => `<tr>
<td style="padding-right:7px; text-align:right" class="title">${story.votes}</td>
<td class="votelinks"><form method="POST" action="/vote" style="display:inline;">
<input type="hidden" name="id" value="${story.id}" />
<button type="submit" style="border:none; background:none; padding:0; margin:0; font-size:inherit; cursor:pointer;">▲</button>
</form></td>
<td class="title"><a href="${story.url}" target="_blank" class="storylink">${story.title}</a></td>
</tr>`).join("");
return html(`
<html>
<head>
<title>Hacker News</title>
<style>
body { font-family: Verdana, Geneva, sans-serif; background: #f6f6ef; margin: 0; }
.container { width: 85%; margin: auto; }
table { width: 100%; border-collapse: collapse; }
.title { font-weight: bold; }
.storylink { color: #000000; font-size: 1rem; text-decoration: none; }
.votelinks { vertical-align: middle; }
.votelinks button { color: #ff6600; }
.header { background: #ff6600; padding: 10px; color: #ffffff; }
.header a { color: #ffffff; text-decoration: none; }
.forms { margin-top: 20px; }
input[type="text"], input[type="url"] { padding: 5px; margin: 5px 0; width: 80%; }
</style>
</head>
<body>
<div class="container">
<div class="header">
<a href="/"><b>Hacker News</b></a>
</div>
<table>
${storiesHtml}
</table>
<div class="forms">
<h2>Submit a New Story</h2>
<form method="POST" action="/submit">
<input type="text" name="title" placeholder="Title" required /><br />
<input type="url" name="url" placeholder="URL" required /><br />
<button type="submit">Submit</button>
</form>
</div>
</div>
</body>
</html>
`);
});
// Handle story submission
app.post("/submit", async (c) => {
const formData = await c.req.parseBody();
const title = formData.title;
const url = formData.url;
const stories = await blob.getJSON(SAMPLE_STORIES_KEY) || [];
const newStory = {
id: stories.length + 1,
title,
url,
votes: 0,
};
stories.push(newStory);
await blob.setJSON(SAMPLE_STORIES_KEY, stories);
return c.redirect("/");
});
// Handle story upvote
app.post("/vote", async (c) => {
const formData = await c.req.parseBody();
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
July 17, 2024