|
1 | 1 | import { serve } from '@hono/node-server' |
2 | 2 | import { Hono } from 'hono' |
3 | 3 | import { serveStatic } from '@hono/node-server/serve-static' |
4 | | -import fs from 'node:fs' |
5 | | -import path from 'node:path' |
6 | | -import { fileURLToPath } from 'url' |
7 | | - |
8 | | -const __filename = fileURLToPath(import.meta.url) |
9 | | -const __dirname = path.dirname(__filename) |
10 | 4 |
|
11 | 5 | const app = new Hono() |
12 | 6 |
|
13 | | -// Función para generar el HTML del índice |
14 | | -async function generateIndex() { |
15 | | - const templatesDir = path.join(__dirname, '../../templates') |
16 | | - const templates = fs.readdirSync(templatesDir) |
17 | | - .filter(file => fs.statSync(path.join(templatesDir, file)).isDirectory()) |
18 | | - |
19 | | - const html = ` |
20 | | -<!DOCTYPE html> |
21 | | -<html lang="en"> |
22 | | -<head> |
23 | | - <meta charset="UTF-8"> |
24 | | - <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
25 | | - <title>Templates Directory</title> |
26 | | - <style> |
27 | | - body { |
28 | | - font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif; |
29 | | - max-width: 800px; |
30 | | - margin: 0 auto; |
31 | | - padding: 2rem; |
32 | | - background: #f5f5f5; |
33 | | - } |
34 | | - h1 { |
35 | | - color: #2c3e50; |
36 | | - border-bottom: 2px solid #3498db; |
37 | | - padding-bottom: 0.5rem; |
38 | | - } |
39 | | - .templates { |
40 | | - display: grid; |
41 | | - grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); |
42 | | - gap: 1rem; |
43 | | - margin-top: 2rem; |
44 | | - } |
45 | | - .template-card { |
46 | | - background: white; |
47 | | - padding: 1.5rem; |
48 | | - border-radius: 8px; |
49 | | - box-shadow: 0 2px 4px rgba(0,0,0,0.1); |
50 | | - transition: transform 0.2s; |
51 | | - } |
52 | | - .template-card:hover { |
53 | | - transform: translateY(-2px); |
54 | | - box-shadow: 0 4px 8px rgba(0,0,0,0.15); |
55 | | - } |
56 | | - .template-card h2 { |
57 | | - margin: 0 0 1rem 0; |
58 | | - color: #2c3e50; |
59 | | - } |
60 | | - .template-links { |
61 | | - display: flex; |
62 | | - gap: 0.5rem; |
63 | | - flex-wrap: wrap; |
64 | | - } |
65 | | - .template-links a { |
66 | | - text-decoration: none; |
67 | | - color: #3498db; |
68 | | - padding: 0.5rem 1rem; |
69 | | - border-radius: 4px; |
70 | | - background: #f8f9fa; |
71 | | - transition: background 0.2s; |
72 | | - } |
73 | | - .template-links a:hover { |
74 | | - background: #e9ecef; |
75 | | - } |
76 | | - </style> |
77 | | -</head> |
78 | | -<body> |
79 | | - <h1>Available Templates</h1> |
80 | | - <div class="templates"> |
81 | | - ${templates.map(template => { |
82 | | - const templatePath = path.join(templatesDir, template) |
83 | | - const files = fs.readdirSync(templatePath) |
84 | | - return ` |
85 | | - <div class="template-card"> |
86 | | - <h2>${template}</h2> |
87 | | - <div class="template-links"> |
88 | | - ${files.map(file => ` |
89 | | - <a href="templates/${template}/${file}">${file}</a> |
90 | | - `).join('')} |
91 | | - </div> |
92 | | - </div> |
93 | | - ` |
94 | | - }).join('')} |
95 | | - </div> |
96 | | -</body> |
97 | | -</html> |
98 | | - ` |
99 | | - return html |
100 | | -} |
101 | | - |
102 | | -// Middleware para logging |
103 | 7 | app.use('*', async (c, next) => { |
104 | | - console.log(`Request path: ${c.req.path}`) |
105 | 8 | await next() |
106 | 9 | }) |
107 | 10 |
|
108 | | -// Servir archivos estáticos desde la carpeta templates |
109 | 11 | app.use('/templates/*', serveStatic({ |
110 | 12 | root: '../templates', |
111 | 13 | rewriteRequestPath: (path) => { |
112 | | - console.log('Original path:', path) |
113 | 14 | return path.replace('/templates/', '') |
114 | 15 | } |
115 | 16 | })) |
116 | 17 |
|
117 | | -// Ruta principal que muestra el índice |
118 | | -app.get('/', async (c) => { |
119 | | - return c.html(await generateIndex()) |
120 | | -}) |
121 | | - |
122 | | -// Ruta para generar el archivo index.html estático |
123 | | -app.get('/generate-static', async (c) => { |
124 | | - const html = await generateIndex() |
125 | | - const outputPath = path.join(__dirname, '../../docs/index.html') |
126 | | - |
127 | | - // Asegurarse de que el directorio docs existe |
128 | | - fs.mkdirSync(path.join(__dirname, '../../docs'), { recursive: true }) |
129 | | - |
130 | | - // Copiar la carpeta templates a docs |
131 | | - const templatesDir = path.join(__dirname, '../../templates') |
132 | | - const docsTemplatesDir = path.join(__dirname, '../../docs/templates') |
133 | | - fs.cpSync(templatesDir, docsTemplatesDir, { recursive: true }) |
134 | | - |
135 | | - // Guardar el index.html |
136 | | - fs.writeFileSync(outputPath, html) |
137 | | - return c.text('Static files generated in docs folder') |
| 18 | +app.get('/', (c) => { |
| 19 | + return c.text('Hello Hono!') |
138 | 20 | }) |
139 | 21 |
|
140 | 22 | serve({ |
|
0 commit comments