-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
118 lines (102 loc) · 3.15 KB
/
server.js
File metadata and controls
118 lines (102 loc) · 3.15 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
// Custom dual-port server for ShareFile
// Upload server: 8800
// Download server: 8801
import http from 'http'
import httpProxy from 'http-proxy'
import { spawn } from 'child_process'
const UPLOAD_PORT = 8800
const DOWNLOAD_PORT = 8801
const NUXT_PORT = 3333 // Internal Nuxt port
// Create proxy
const proxy = httpProxy.createProxyServer({
target: `http://localhost:${NUXT_PORT}`,
ws: true
})
proxy.on('error', (err, req, res) => {
console.error('Proxy error:', err)
if (res.writeHead) {
res.writeHead(502, { 'Content-Type': 'text/plain' })
res.end('Bad Gateway')
}
})
// Upload server (17710) - only serves /admin/*, /api/upload, and static assets
const uploadServer = http.createServer((req, res) => {
const url = req.url || ''
// Allow admin pages, upload API, admin API, and assets
if (
url === '/' ||
url.startsWith('/admin') ||
url.startsWith('/api/upload') ||
url.startsWith('/api/admin') ||
url.startsWith('/_nuxt') ||
url.startsWith('/__nuxt') ||
url.startsWith('/@') ||
url.startsWith('/node_modules')
) {
proxy.web(req, res)
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' })
res.end('Not Found - This is the upload server (port 8800)')
}
})
uploadServer.on('upgrade', (req, socket, head) => {
proxy.ws(req, socket, head)
})
// Download server (17711) - only serves /download/*, /api/files/*, /api/download/*
const downloadServer = http.createServer((req, res) => {
const url = req.url || ''
// Allow download pages, file info API, download API, and assets
if (
url === '/' ||
url.startsWith('/download') ||
url.startsWith('/api/files') ||
url.startsWith('/api/download') ||
url.startsWith('/_nuxt') ||
url.startsWith('/__nuxt') ||
url.startsWith('/@') ||
url.startsWith('/node_modules')
) {
proxy.web(req, res)
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' })
res.end('Not Found - This is the download server (port 8801)')
}
})
downloadServer.on('upgrade', (req, socket, head) => {
proxy.ws(req, socket, head)
})
// Start Nuxt dev server on internal port
console.log('Starting Nuxt dev server...')
const nuxt = spawn('npx', ['nuxt', 'dev', '--port', NUXT_PORT.toString()], {
stdio: ['ignore', 'pipe', 'pipe'],
env: { ...process.env, NUXT_PORT: NUXT_PORT.toString() }
})
nuxt.stdout.on('data', (data) => {
const output = data.toString()
if (output.includes('Local:')) {
// Nuxt is ready, start our servers
uploadServer.listen(UPLOAD_PORT, () => {
console.log(`\n✓ Upload server ready: http://localhost:${UPLOAD_PORT}/admin/upload`)
})
downloadServer.listen(DOWNLOAD_PORT, () => {
console.log(`✓ Download server ready: http://localhost:${DOWNLOAD_PORT}/download/[id]`)
console.log('\n')
})
}
process.stdout.write(output)
})
nuxt.stderr.on('data', (data) => {
process.stderr.write(data.toString())
})
nuxt.on('close', (code) => {
console.log(`Nuxt process exited with code ${code}`)
process.exit(code || 0)
})
// Handle shutdown
process.on('SIGINT', () => {
console.log('\nShutting down...')
nuxt.kill()
uploadServer.close()
downloadServer.close()
process.exit(0)
})