-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdev-server.js
More file actions
159 lines (144 loc) · 4.48 KB
/
dev-server.js
File metadata and controls
159 lines (144 loc) · 4.48 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const { createServer } = require('http');
const { parse } = require('url');
const next = require('next');
const fs = require('fs');
const path = require('path');
// Create a Next.js app in production mode with custom server
const app = next({ dev: false });
const handle = app.getRequestHandler();
// Ensure upload directories exist
function ensureUploadDirectories() {
const uploadDirs = [
path.join(process.cwd(), 'public', 'uploads'),
path.join(process.cwd(), 'public', 'uploads', 'avatars'),
path.join(process.cwd(), 'public', 'uploads', 'models'),
path.join(process.cwd(), 'public', 'uploads', 'temp')
];
console.log('Ensuring upload directories exist...');
for (const dir of uploadDirs) {
if (!fs.existsSync(dir)) {
console.log(`Creating directory: ${dir}`);
fs.mkdirSync(dir, { recursive: true });
} else {
console.log(`Directory already exists: ${dir}`);
}
// Make sure directory is writable
try {
fs.accessSync(dir, fs.constants.W_OK);
console.log(`Directory is writable: ${dir}`);
} catch (error) {
console.error(`Directory is not writable: ${dir}`, error);
}
}
console.log('All upload directories verified');
}
// Paths that require static file handling
const staticPaths = {
'/signin': '/public/signin-static.html',
'/signup': '/public/signup-static.html',
'/auth/callback': '/public/auth-callback-static.html'
};
// Simple fallback content in case files are missing
const defaultFallback = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Loading...</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, sans-serif;
margin: 0;
padding: 0;
min-height: 100vh;
background: linear-gradient(to bottom, #1a1a2e, #000000);
color: #ffffff;
display: flex;
align-items: center;
justify-content: center;
}
.container {
max-width: 500px;
padding: 2rem;
background-color: rgba(255, 255, 255, 0.05);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
border-radius: 1rem;
text-align: center;
}
.loader {
display: inline-block;
width: 40px;
height: 40px;
border: 3px solid rgba(255, 255, 255, 0.3);
border-radius: 50%;
border-top-color: #9f7aea;
animation: spin 1s ease-in-out infinite;
margin-bottom: 1.5rem;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
</style>
</head>
<body>
<div class="container">
<div class="loader"></div>
<h1>Loading...</h1>
<p>Please wait while we redirect you...</p>
<script>
setTimeout(() => {
window.location = window.location.href;
}, 1500);
</script>
</div>
</body>
</html>
`;
// Start the app
app.prepare().then(() => {
// Create upload directories before starting server
ensureUploadDirectories();
createServer((req, res) => {
// Set headers for middleware to identify special handling
req.headers['x-is-build'] = 'true';
const parsedUrl = parse(req.url, true);
const { pathname } = parsedUrl;
// Handle static paths for auth pages
if (staticPaths[pathname]) {
try {
const filePath = path.join(process.cwd(), staticPaths[pathname]);
if (fs.existsSync(filePath)) {
const fileContent = fs.readFileSync(filePath, 'utf8');
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(fileContent);
return;
} else {
console.warn(`Static file not found: ${filePath}, using default fallback`);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(defaultFallback);
return;
}
} catch (err) {
console.error(`Error serving static file for ${pathname}:`, err);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(defaultFallback);
return;
}
}
// Let Next.js handle everything else
handle(req, res, parsedUrl);
}).listen(3000, (err) => {
if (err) {
console.error('Failed to start server:', err);
process.exit(1);
}
console.log('\n🚀 Server running at http://localhost:3000');
console.log('✅ Auth pages using static HTML fallbacks');
console.log('✅ Upload directories verified and ready');
});
}).catch(err => {
console.error('Error preparing Next.js app:', err);
process.exit(1);
});