-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
64 lines (54 loc) · 2.25 KB
/
Copy pathserver.ts
File metadata and controls
64 lines (54 loc) · 2.25 KB
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
import express from 'express';
import { createServer as createViteServer } from 'vite';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
async function startServer() {
const app = express();
const port = 3000;
app.use(express.json());
// API Routes - Proxy to Supabase
app.get('/api/gallery', async (req, res) => {
try {
const supabaseUrl = process.env.SUPABASE_URL || 'https://byspouzbijpkvclztxru.supabase.co';
const supabaseKey = process.env.SUPABASE_ANON_KEY || 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImJ5c3BvdXpiaWpwa3ZjbHp0eHJ1Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzIwNzg3NzcsImV4cCI6MjA4NzY1NDc3N30.Wkhmc-tYxiWFao6lShkMYNIAIgvos1pDXDkEXsVXDXk';
const response = await fetch(`${supabaseUrl}/rest/v1/gallery?select=*`, {
method: 'GET',
headers: {
'apikey': supabaseKey,
'Authorization': `Bearer ${supabaseKey}`,
'Content-Type': 'application/json'
}
});
if (!response.ok) {
const errorText = await response.text();
console.error('Supabase error:', errorText);
return res.status(response.status).json({ error: 'Supabase fetch failed' });
}
const data = await response.json();
res.json(data);
} catch (error) {
console.error('Server error:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
// Vite middleware for development
if (process.env.NODE_ENV !== 'production') {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: 'spa',
});
app.use(vite.middlewares);
} else {
const distPath = path.join(process.cwd(), 'dist');
app.use(express.static(distPath));
app.get('*', (req, res) => {
res.sendFile(path.join(distPath, 'index.html'));
});
}
app.listen(port, '0.0.0.0', () => {
console.log(`Server running at http://localhost:${port}`);
});
}
startServer();