Skip to content

Commit 4d20a9a

Browse files
committed
Simplify Hono server and remove dynamic index generation
1 parent 3d8f598 commit 4d20a9a

File tree

1 file changed

+2
-120
lines changed

1 file changed

+2
-120
lines changed

app/src/index.ts

Lines changed: 2 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,140 +1,22 @@
11
import { serve } from '@hono/node-server'
22
import { Hono } from 'hono'
33
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)
104

115
const app = new Hono()
126

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
1037
app.use('*', async (c, next) => {
104-
console.log(`Request path: ${c.req.path}`)
1058
await next()
1069
})
10710

108-
// Servir archivos estáticos desde la carpeta templates
10911
app.use('/templates/*', serveStatic({
11012
root: '../templates',
11113
rewriteRequestPath: (path) => {
112-
console.log('Original path:', path)
11314
return path.replace('/templates/', '')
11415
}
11516
}))
11617

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!')
13820
})
13921

14022
serve({

0 commit comments

Comments
 (0)