|
| 1 | +import "dotenv/config"; |
| 2 | +import { Hono } from "hono"; |
| 3 | +import { PrismaClient } from "./generated/prisma/client"; |
| 4 | +import { serve } from "@hono/node-server"; |
| 5 | + |
| 6 | +const prisma = new PrismaClient(); |
| 7 | +const app = new Hono(); |
| 8 | + |
| 9 | +app.get("/", (c) => { |
| 10 | + const html = ` |
| 11 | + <!DOCTYPE html> |
| 12 | + <html> |
| 13 | + <head> |
| 14 | + <title>Prisma + Railway Example</title> |
| 15 | + <style> |
| 16 | + body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; } |
| 17 | + h1 { color: #333; } |
| 18 | + .endpoint { margin: 10px 0; padding: 10px; background: #f5f5f5; border-radius: 4px; } |
| 19 | + button { margin-left: 10px; padding: 4px 8px; cursor: pointer; } |
| 20 | + pre { margin-top: 20px; padding: 10px; border: 1px solid #ddd; background: #f9f9f9; } |
| 21 | + </style> |
| 22 | + </head> |
| 23 | + <body> |
| 24 | + <h1>API Endpoints</h1> |
| 25 | + |
| 26 | + <div class="endpoint"> |
| 27 | + <code>GET /api</code> |
| 28 | + <button onclick="testEndpoint('/api')">Test</button> |
| 29 | + </div> |
| 30 | + |
| 31 | + <div class="endpoint"> |
| 32 | + <code>GET /api/feed</code> |
| 33 | + <button onclick="testEndpoint('/api/feed')">Test</button> |
| 34 | + </div> |
| 35 | + |
| 36 | + <div class="endpoint"> |
| 37 | + <code>GET /api/seed</code> |
| 38 | + <button onclick="testEndpoint('/api/seed')">Test</button> |
| 39 | + </div> |
| 40 | + |
| 41 | + <pre><code id="result">Click a button to test an endpoint</code></pre> |
| 42 | +
|
| 43 | + <script> |
| 44 | + async function testEndpoint(endpoint) { |
| 45 | + const resultDiv = document.getElementById('result'); |
| 46 | + resultDiv.textContent = 'Loading...'; |
| 47 | + |
| 48 | + try { |
| 49 | + const response = await fetch(endpoint); |
| 50 | + const data = await response.json(); |
| 51 | + resultDiv.textContent = JSON.stringify(data, null, 2); |
| 52 | + } catch (error) { |
| 53 | + resultDiv.textContent = 'Error: ' + error.message; |
| 54 | + } |
| 55 | + } |
| 56 | + </script> |
| 57 | + </body> |
| 58 | + </html> |
| 59 | + `; |
| 60 | + return c.html(html); |
| 61 | +}); |
| 62 | + |
| 63 | +app.get("/api", async (c) => { |
| 64 | + return c.json({ up: true }); |
| 65 | +}); |
| 66 | + |
| 67 | +app.get("/api/seed", async (c) => { |
| 68 | + try { |
| 69 | + await prisma.post.deleteMany({}); |
| 70 | + await prisma.user.deleteMany({}); |
| 71 | + |
| 72 | + const author1 = await prisma.user.create({ |
| 73 | + data: { |
| 74 | + |
| 75 | + name: "Jane Doe", |
| 76 | + posts: { |
| 77 | + create: { |
| 78 | + title: "Comparing Database Types", |
| 79 | + content: |
| 80 | + "https://www.prisma.io/blog/comparison-of-database-models-1iz9u29nwn37/", |
| 81 | + published: true, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + include: { posts: true }, |
| 86 | + }); |
| 87 | + |
| 88 | + const author2 = await prisma.user.create({ |
| 89 | + data: { |
| 90 | + |
| 91 | + name: "John Smith", |
| 92 | + posts: { |
| 93 | + create: { |
| 94 | + title: "Getting Started with Prisma", |
| 95 | + content: "https://www.prisma.io/docs/getting-started", |
| 96 | + published: true, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + include: { posts: true }, |
| 101 | + }); |
| 102 | + |
| 103 | + return c.json({ |
| 104 | + message: "Database seeded successfully", |
| 105 | + authors: [author1, author2], |
| 106 | + }); |
| 107 | + } catch (e) { |
| 108 | + return c.json({ error: "Failed to seed database" }, 500); |
| 109 | + } |
| 110 | +}); |
| 111 | + |
| 112 | +app.get("/api/feed", async (c) => { |
| 113 | + const posts = await prisma.post.findMany({ |
| 114 | + where: { published: true }, |
| 115 | + include: { author: true }, |
| 116 | + }); |
| 117 | + return c.json(posts); |
| 118 | +}); |
| 119 | + |
| 120 | +const port = process.env.PORT ? Number(process.env.PORT) : 3000; |
| 121 | +console.log(`Server is running on port http://localhost:${port}`); |
| 122 | + |
| 123 | +serve({ |
| 124 | + fetch: app.fetch, |
| 125 | + port, |
| 126 | +}); |
0 commit comments