MCPcopy
hub / github.com/AutoMaker-Org/automaker / startStaticServer

Function startStaticServer

apps/ui/src/electron/server/static-server.ts:39–90  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

37 * Uses centralized electronApp methods for serving static files from app bundle.
38 */
39export async function startStaticServer(): Promise<void> {
40 // __dirname is apps/ui/dist-electron (Vite bundles all into single file)
41 const staticPath = path.join(__dirname, '../dist');
42
43 state.staticServer = http.createServer((request, response) => {
44 let filePath = path.join(staticPath, request.url?.split('?')[0] || '/');
45
46 if (filePath.endsWith('/')) {
47 filePath = path.join(filePath, 'index.html');
48 } else if (!path.extname(filePath)) {
49 // For client-side routing, serve index.html for paths without extensions
50 const possibleFile = filePath + '.html';
51 try {
52 if (!electronAppExists(filePath) && !electronAppExists(possibleFile)) {
53 filePath = path.join(staticPath, 'index.html');
54 } else if (electronAppExists(possibleFile)) {
55 filePath = possibleFile;
56 }
57 } catch {
58 filePath = path.join(staticPath, 'index.html');
59 }
60 }
61
62 electronAppStat(filePath, (err, stats) => {
63 if (err || !stats?.isFile()) {
64 filePath = path.join(staticPath, 'index.html');
65 }
66
67 electronAppReadFile(filePath, (error, content) => {
68 if (error || !content) {
69 response.writeHead(500);
70 response.end('Server Error');
71 return;
72 }
73
74 const ext = path.extname(filePath);
75 response.writeHead(200, {
76 'Content-Type': CONTENT_TYPES[ext] || 'application/octet-stream',
77 });
78 response.end(content);
79 });
80 });
81 });
82
83 return new Promise((resolve, reject) => {
84 state.staticServer!.listen(state.staticPort, () => {
85 logger.info('Static server running at http://localhost:' + state.staticPort);
86 resolve();
87 });
88 state.staticServer!.on('error', reject);
89 });
90}
91
92/**
93 * Stop the static server if running

Callers 1

handleAppReadyFunction · 0.90

Calls 3

electronAppExistsFunction · 0.90
electronAppStatFunction · 0.90
electronAppReadFileFunction · 0.90

Tested by

no test coverage detected