-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
217 lines (188 loc) · 7.55 KB
/
server.js
File metadata and controls
217 lines (188 loc) · 7.55 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const express = require('express');
const cookieParser = require('cookie-parser');
const jwt = require('jsonwebtoken');
const fs = require('fs');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
const DATA_FILE = path.join(__dirname, 'data', 'docs.json');
const CREDENTIALS = { username: 'admin', password: 'Sms@2025' };
const JWT_SECRET = process.env.JWT_SECRET || 'sms-local-dev-secret-change-in-prod';
const IS_VERCEL = process.env.VERCEL === '1';
const HAS_KV = !!(process.env.KV_REST_API_URL);
// Local dev: ensure data file exists
if (!IS_VERCEL) {
const dataDir = path.join(__dirname, 'data');
if (!fs.existsSync(dataDir)) fs.mkdirSync(dataDir, { recursive: true });
if (!fs.existsSync(DATA_FILE)) {
fs.writeFileSync(DATA_FILE, JSON.stringify({ pages: [] }, null, 2));
}
}
// ── STORAGE ───────────────────────────────────────────────────────────────────
async function readData() {
if (HAS_KV) {
const { kv } = require('@vercel/kv');
const pages = await kv.get('sms_pages') ?? [];
return { pages };
}
try {
return JSON.parse(fs.readFileSync(DATA_FILE, 'utf8'));
} catch {
return { pages: [] };
}
}
async function writeData(data) {
if (HAS_KV) {
const { kv } = require('@vercel/kv');
await kv.set('sms_pages', data.pages);
return;
}
if (!IS_VERCEL) {
fs.writeFileSync(DATA_FILE, JSON.stringify(data, null, 2));
}
}
function slugify(str) {
return str.toLowerCase().trim()
.replace(/[^a-z0-9\s-]/g, '')
.replace(/[\s_]+/g, '-')
.replace(/^-+|-+$/g, '');
}
// ── MIDDLEWARE ────────────────────────────────────────────────────────────────
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
// Brand assets and static files are now inside the repo
app.use(express.static(__dirname));
function requireAuth(req, res, next) {
try {
jwt.verify(req.cookies?.sms_token, JWT_SECRET);
next();
} catch {
res.status(401).json({ error: 'Unauthorized' });
}
}
// ── AUTH ──────────────────────────────────────────────────────────────────────
app.post('/api/login', (req, res) => {
const { username, password } = req.body;
if (username === CREDENTIALS.username && password === CREDENTIALS.password) {
const token = jwt.sign({ admin: true }, JWT_SECRET, { expiresIn: '8h' });
res.cookie('sms_token', token, {
httpOnly: true,
secure: IS_VERCEL,
sameSite: 'lax',
maxAge: 8 * 60 * 60 * 1000
});
res.json({ ok: true });
} else {
res.status(401).json({ error: 'Invalid username or password' });
}
});
app.post('/api/logout', (req, res) => {
res.clearCookie('sms_token');
res.json({ ok: true });
});
app.get('/api/me', (req, res) => {
try {
jwt.verify(req.cookies?.sms_token, JWT_SECRET);
res.json({ authenticated: true });
} catch {
res.json({ authenticated: false });
}
});
// ── DOCS API ──────────────────────────────────────────────────────────────────
app.get('/api/docs', async (req, res) => {
try {
const { pages } = await readData();
const list = pages
.sort((a, b) => (a.order ?? 999) - (b.order ?? 999) || a.title.localeCompare(b.title))
.map(({ id, title, subtitle, category, slug, order, updatedAt }) =>
({ id, title, subtitle, category, slug, order, updatedAt }));
res.json(list);
} catch {
res.status(500).json({ error: 'Failed to read data' });
}
});
app.get('/api/docs/:slug', async (req, res) => {
try {
const { pages } = await readData();
const page = pages.find(p => p.slug === req.params.slug || p.id === req.params.slug);
if (!page) return res.status(404).json({ error: 'Page not found' });
res.json(page);
} catch {
res.status(500).json({ error: 'Failed to read data' });
}
});
app.post('/api/docs', requireAuth, async (req, res) => {
try {
const { title, subtitle = '', category = 'Allmänt', content = '', order } = req.body;
if (!title?.trim()) return res.status(400).json({ error: 'Titel krävs' });
const data = await readData();
let slug = slugify(title) || 'sida';
let counter = 2;
while (data.pages.find(p => p.slug === slug)) slug = `${slugify(title)}-${counter++}`;
const page = {
id: Date.now().toString(36) + Math.random().toString(36).slice(2, 6),
title: title.trim(),
subtitle: subtitle.trim(),
category: category.trim() || 'Allmänt',
content,
slug,
order: order != null ? Number(order) : data.pages.length,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString()
};
data.pages.push(page);
await writeData(data);
res.status(201).json(page);
} catch {
res.status(500).json({ error: 'Failed to save data' });
}
});
app.put('/api/docs/:id', requireAuth, async (req, res) => {
try {
const data = await readData();
const i = data.pages.findIndex(p => p.id === req.params.id);
if (i === -1) return res.status(404).json({ error: 'Page not found' });
const { title, subtitle, category, content, order } = req.body;
const p = data.pages[i];
data.pages[i] = {
...p,
...(title != null && { title: title.trim() }),
...(subtitle != null && { subtitle: subtitle.trim() }),
...(category != null && { category: category.trim() || 'Allmänt' }),
...(content != null && { content }),
...(order != null && { order: Number(order) }),
updatedAt: new Date().toISOString()
};
await writeData(data);
res.json(data.pages[i]);
} catch {
res.status(500).json({ error: 'Failed to save data' });
}
});
app.delete('/api/docs/:id', requireAuth, async (req, res) => {
try {
const data = await readData();
const i = data.pages.findIndex(p => p.id === req.params.id);
if (i === -1) return res.status(404).json({ error: 'Page not found' });
data.pages.splice(i, 1);
await writeData(data);
res.json({ ok: true });
} catch {
res.status(500).json({ error: 'Failed to delete' });
}
});
// ── START ─────────────────────────────────────────────────────────────────────
if (!IS_VERCEL) {
app.listen(PORT, () => {
console.log('\n ┌─────────────────────────────────────────┐');
console.log(' │ Security Management System – Docs │');
console.log(' ├─────────────────────────────────────────┤');
console.log(` │ Site: http://localhost:${PORT} │`);
console.log(` │ Docs: http://localhost:${PORT}/docs/ │`);
console.log(` │ Admin: http://localhost:${PORT}/admin/ │`);
console.log(' └─────────────────────────────────────────┘\n');
console.log(` Inloggning: admin / Sms@2025\n`);
});
}
module.exports = app;