-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
125 lines (105 loc) Β· 3.67 KB
/
Copy pathserver.js
File metadata and controls
125 lines (105 loc) Β· 3.67 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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const fs = require('fs-extra');
const path = require('path');
const yaml = require('js-yaml');
const { exec } = require('child_process');
const app = express();
const PORT = 3000;
// Configuration
const PROJECT_ROOT = process.env.AZTOMIQ_PROJECT_ROOT || process.cwd();
const SRC_DIR = path.join(PROJECT_ROOT, 'src');
const FEATURES_DIR = path.join(SRC_DIR, 'features');
const DIST_DIR = path.join(PROJECT_ROOT, 'dist-dev');
app.use(cors());
app.use(bodyParser.json());
// 1. Static Files (Serve the site)
app.use(express.static(DIST_DIR));
// 2. Admin API
// GET /api/features - List all features and their configs
app.get('/api/features', (req, res) => {
try {
const features = fs.readdirSync(FEATURES_DIR).filter(f => {
return fs.statSync(path.join(FEATURES_DIR, f)).isDirectory();
});
const data = features.map(feature => {
const configPath = path.join(FEATURES_DIR, feature, 'tool.yaml');
if (fs.existsSync(configPath)) {
const config = yaml.load(fs.readFileSync(configPath, 'utf8'));
return { id: feature, config };
}
return null;
}).filter(item => item !== null);
res.json(data);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// POST /api/features/:id - Update feature config
app.post('/api/features/:id', (req, res) => {
const { id } = req.params;
const newConfig = req.body;
const configPath = path.join(FEATURES_DIR, id, 'tool.yaml');
if (!fs.existsSync(configPath)) {
return res.status(404).json({ error: 'Feature not found' });
}
try {
fs.writeFileSync(configPath, yaml.dump(newConfig), 'utf8');
console.log(`π Updated config for ${id}`);
console.log('π Triggering rebuild...');
exec('npx aztomiq build', { cwd: PROJECT_ROOT }, (error, stdout, stderr) => {
if (error) console.error(`Build error: ${error}`);
else console.log(`β
Build complete`);
});
res.json({ success: true, message: 'Config updated and rebuild triggered' });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// GET /api/global
app.get('/api/global', (req, res) => {
try {
const globalPath = path.join(SRC_DIR, 'data', 'global.yaml');
if (fs.existsSync(globalPath)) {
const config = yaml.load(fs.readFileSync(globalPath, 'utf8'));
res.json(config);
} else {
res.json({});
}
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// POST /api/global
app.post('/api/global', (req, res) => {
try {
const globalPath = path.join(SRC_DIR, 'data', 'global.yaml');
const newConfig = req.body;
fs.writeFileSync(globalPath, yaml.dump(newConfig), 'utf8');
console.log(`π Updated Global Config`);
console.log('π Triggering rebuild...');
exec('npx aztomiq build', { cwd: PROJECT_ROOT }, (error, stdout, stderr) => {
if (error) console.error(`Build error: ${error}`);
else console.log(`β
Build complete`);
});
res.json({ success: true, message: 'Global config updated' });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// Fallback to index.html for SPA routing (only for page requests, not assets)
app.use((req, res) => {
// If request has an extension, it's likely a missing asset
if (path.extname(req.path)) {
return res.status(404).send('Not found');
}
res.sendFile(path.join(DIST_DIR, 'index.html'));
});
app.listen(PORT, () => {
console.log(`
π Admin Server running at http://localhost:${PORT}/admin
π Admin API: http://localhost:${PORT}/api/features
π Website: http://localhost:${PORT}/
`);
});