Spaces:
Sleeping
Sleeping
Download server.js from Ethscriptions/cut: direct link, hf CLI and curl.
- Browser
- Download file 2.55 kB
-
https://huggingface.co/spaces/Ethscriptions/cut/resolve/main/server.js
- Command line
-
hf download hf://spaces/Ethscriptions/cut/server.js
-
curl -L -o server.js https://huggingface.co/spaces/Ethscriptions/cut/resolve/main/server.js
2.55 kB
| /** | |
| * Surf Mystery Box 赠链追踪 — single-process server. | |
| * | |
| * 1. restore the SQLite DB (local file → encrypted HF dataset backup → legacy snapshot import) | |
| * 2. start the background loops (X search, Telegram, invite checks, pushes, backups) | |
| * 3. serve /api and the built frontend on one port | |
| */ | |
| const path = require('path') | |
| const express = require('express') | |
| const log = require('./lib/log') | |
| const backup = require('./lib/backup') | |
| const alerts = require('./lib/alerts') | |
| const invites = require('./lib/invites') | |
| const scheduler = require('./lib/scheduler') | |
| const xscan = require('./lib/x/scan') | |
| const tg = require('./lib/tg') | |
| const db = require('./lib/db') | |
| const port = Number(process.env.PORT || 7860) | |
| const publicDir = process.env.PUBLIC_DIR || path.join(__dirname, 'public') | |
| async function main() { | |
| const bootTs = log.now() | |
| const restored = await backup.restore() | |
| log.info('backup', `数据来源:${backup.status().restoredFrom} ${JSON.stringify(restored)}`) | |
| // No push history to go on → don't flood the group with codes that may already have been sent. | |
| const counts = db.counts() | |
| if (String(backup.status().restoredFrom || '').startsWith('empty') && !counts.codes) { | |
| alerts.holdThenBaseline(8 * 60000, bootTs - 2 * 3600) | |
| } | |
| xscan.init() | |
| invites.setUsableHandler(() => alerts.pushPending()) | |
| const app = express() | |
| app.disable('x-powered-by') | |
| app.use(express.json({ limit: '1mb' })) | |
| app.use('/api', require('./routes/api')) | |
| app.use('/api', (req, res) => res.status(404).json({ error: 'not found' })) | |
| app.use(express.static(publicDir, { index: false, maxAge: '1h' })) | |
| app.get('*', (req, res) => { | |
| res.set('Cache-Control', 'no-cache') | |
| res.sendFile(path.join(publicDir, 'index.html'), (err) => { | |
| if (err) res.status(404).send('frontend not built') | |
| }) | |
| }) | |
| app.listen(port, () => log.info('server', `已启动,端口 ${port}`)) | |
| backup.start() | |
| scheduler.start() | |
| } | |
| let stopping = false | |
| async function shutdown(signal) { | |
| if (stopping) return | |
| stopping = true | |
| log.info('server', `收到 ${signal},保存数据后退出`) | |
| await Promise.race([ | |
| (async () => { | |
| await tg.stop().catch(() => {}) | |
| await backup.save('shutdown').catch((err) => log.error('backup', err.message)) | |
| })(), | |
| new Promise((resolve) => setTimeout(resolve, 20000)), | |
| ]) | |
| process.exit(0) | |
| } | |
| process.on('SIGTERM', () => shutdown('SIGTERM')) | |
| process.on('SIGINT', () => shutdown('SIGINT')) | |
| main().catch((err) => { | |
| console.error('启动失败:', err) | |
| process.exit(1) | |
| }) | |