Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const cors = require('cors');
|
| 3 |
+
const fs = require('fs');
|
| 4 |
+
const path = require('path');
|
| 5 |
+
const multer = require('multer');
|
| 6 |
+
|
| 7 |
+
const app = express();
|
| 8 |
+
const PORT = process.env.PORT || 7860;
|
| 9 |
+
|
| 10 |
+
// Persistent storage on Hugging Face
|
| 11 |
+
const UPLOAD_DIR = '/data/uploads';
|
| 12 |
+
const DATA_FILE = '/data/data.json';
|
| 13 |
+
|
| 14 |
+
// Create upload dir
|
| 15 |
+
try {
|
| 16 |
+
if (!fs.existsSync('/data')) fs.mkdirSync('/data', { recursive: true });
|
| 17 |
+
if (!fs.existsSync(UPLOAD_DIR)) fs.mkdirSync(UPLOAD_DIR, { recursive: true });
|
| 18 |
+
} catch (e) {
|
| 19 |
+
console.log('Dir note:', e.message);
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
app.use(cors({ origin: '*' }));
|
| 23 |
+
app.use(express.json({ limit: '500mb' }));
|
| 24 |
+
app.use('/uploads', express.static(UPLOAD_DIR));
|
| 25 |
+
|
| 26 |
+
// Serve static HTML files
|
| 27 |
+
app.use(express.static(path.join(__dirname, 'public')));
|
| 28 |
+
|
| 29 |
+
const storage = multer.diskStorage({
|
| 30 |
+
destination: (req, file, cb) => cb(null, UPLOAD_DIR),
|
| 31 |
+
filename: (req, file, cb) => {
|
| 32 |
+
const ext = path.extname(file.originalname);
|
| 33 |
+
const name = `${Date.now()}-${Math.round(Math.random() * 1e9)}${ext}`;
|
| 34 |
+
cb(null, name);
|
| 35 |
+
}
|
| 36 |
+
});
|
| 37 |
+
|
| 38 |
+
const upload = multer({
|
| 39 |
+
storage,
|
| 40 |
+
limits: { fileSize: 500 * 1024 * 1024 }
|
| 41 |
+
});
|
| 42 |
+
|
| 43 |
+
function readData() {
|
| 44 |
+
try {
|
| 45 |
+
if (!fs.existsSync(DATA_FILE)) return { articles: [], videos: [] };
|
| 46 |
+
return JSON.parse(fs.readFileSync(DATA_FILE, 'utf-8'));
|
| 47 |
+
} catch (e) {
|
| 48 |
+
return { articles: [], videos: [] };
|
| 49 |
+
}
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
function writeData(data) {
|
| 53 |
+
try {
|
| 54 |
+
fs.writeFileSync(DATA_FILE, JSON.stringify(data, null, 2));
|
| 55 |
+
} catch (e) {
|
| 56 |
+
console.error('Write error:', e.message);
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
// Health check
|
| 61 |
+
app.get('/', (req, res) => {
|
| 62 |
+
res.json({ status: 'ok', message: 'Tera News Server Running!' });
|
| 63 |
+
});
|
| 64 |
+
|
| 65 |
+
// Get all data
|
| 66 |
+
app.get('/api/data', (req, res) => {
|
| 67 |
+
res.json(readData());
|
| 68 |
+
});
|
| 69 |
+
|
| 70 |
+
// Update all data
|
| 71 |
+
app.put('/api/data', (req, res) => {
|
| 72 |
+
const data = {
|
| 73 |
+
articles: req.body.articles || [],
|
| 74 |
+
videos: req.body.videos || []
|
| 75 |
+
};
|
| 76 |
+
writeData(data);
|
| 77 |
+
res.json({ success: true, data });
|
| 78 |
+
});
|
| 79 |
+
|
| 80 |
+
// Add article
|
| 81 |
+
app.post('/api/articles', (req, res) => {
|
| 82 |
+
const data = readData();
|
| 83 |
+
const article = req.body;
|
| 84 |
+
if (!article || !article.id) return res.status(400).json({ error: 'Invalid article' });
|
| 85 |
+
data.articles.unshift(article);
|
| 86 |
+
writeData(data);
|
| 87 |
+
res.json({ success: true, article });
|
| 88 |
+
});
|
| 89 |
+
|
| 90 |
+
// Add video
|
| 91 |
+
app.post('/api/videos', (req, res) => {
|
| 92 |
+
const data = readData();
|
| 93 |
+
const video = req.body;
|
| 94 |
+
if (!video || !video.id) return res.status(400).json({ error: 'Invalid video' });
|
| 95 |
+
data.videos.unshift(video);
|
| 96 |
+
writeData(data);
|
| 97 |
+
res.json({ success: true, video });
|
| 98 |
+
});
|
| 99 |
+
|
| 100 |
+
// Delete article
|
| 101 |
+
app.delete('/api/articles/:id', (req, res) => {
|
| 102 |
+
const data = readData();
|
| 103 |
+
data.articles = data.articles.filter(a => a.id !== parseInt(req.params.id));
|
| 104 |
+
writeData(data);
|
| 105 |
+
res.json({ success: true });
|
| 106 |
+
});
|
| 107 |
+
|
| 108 |
+
// Delete video
|
| 109 |
+
app.delete('/api/videos/:id', (req, res) => {
|
| 110 |
+
const data = readData();
|
| 111 |
+
data.videos = data.videos.filter(v => v.id !== parseInt(req.params.id));
|
| 112 |
+
writeData(data);
|
| 113 |
+
res.json({ success: true });
|
| 114 |
+
});
|
| 115 |
+
|
| 116 |
+
// Update views
|
| 117 |
+
app.post('/api/articles/:id/views', (req, res) => {
|
| 118 |
+
const data = readData();
|
| 119 |
+
const article = data.articles.find(a => a.id === parseInt(req.params.id));
|
| 120 |
+
if (!article) return res.status(404).json({ error: 'Not found' });
|
| 121 |
+
article.views = (article.views || 0) + 1;
|
| 122 |
+
writeData(data);
|
| 123 |
+
res.json({ success: true, views: article.views });
|
| 124 |
+
});
|
| 125 |
+
|
| 126 |
+
// Upload file (video or image)
|
| 127 |
+
app.post('/api/upload', upload.single('file'), (req, res) => {
|
| 128 |
+
if (!req.file) return res.status(400).json({ error: 'No file uploaded' });
|
| 129 |
+
const url = `${req.protocol}://${req.get('host')}/uploads/${req.file.filename}`;
|
| 130 |
+
res.json({ success: true, url, filename: req.file.filename });
|
| 131 |
+
});
|
| 132 |
+
|
| 133 |
+
// Get uploaded file list
|
| 134 |
+
app.get('/api/files', (req, res) => {
|
| 135 |
+
try {
|
| 136 |
+
const files = fs.readdirSync(UPLOAD_DIR).map(f => ({
|
| 137 |
+
name: f,
|
| 138 |
+
url: `${req.protocol}://${req.get('host')}/uploads/${f}`,
|
| 139 |
+
size: fs.statSync(path.join(UPLOAD_DIR, f)).size
|
| 140 |
+
}));
|
| 141 |
+
res.json({ success: true, files });
|
| 142 |
+
} catch (e) {
|
| 143 |
+
res.json({ success: true, files: [] });
|
| 144 |
+
}
|
| 145 |
+
});
|
| 146 |
+
|
| 147 |
+
app.listen(PORT, '0.0.0.0', () => {
|
| 148 |
+
console.log(`Tera News Server running on port ${PORT}`);
|
| 149 |
+
});
|