66 lines
2.5 KiB
JavaScript
66 lines
2.5 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const express_1 = __importDefault(require("express"));
|
|
const path_1 = __importDefault(require("path"));
|
|
const db_1 = require("./services/db");
|
|
const providers_1 = require("./services/providers");
|
|
require("./cron"); // Start the cron jobs
|
|
const app = (0, express_1.default)();
|
|
const PORT = process.env.PORT || 3000;
|
|
app.use(express_1.default.static(path_1.default.join(__dirname, '../public')));
|
|
app.use(express_1.default.json());
|
|
app.get('/api/quotas', async (req, res) => {
|
|
try {
|
|
const quotas = await (0, providers_1.fetchAllQuotas)();
|
|
// Add DB-based stats
|
|
const { getTodayStartQuota } = require('./services/db');
|
|
for (const quota of quotas) {
|
|
if (quota.provider.toLowerCase() === 'poe' && quota.meta) {
|
|
const todayStart = await getTodayStartQuota('POE');
|
|
if (todayStart) {
|
|
const startAvailable = todayStart.meta?.available ?? (todayStart.total - todayStart.used);
|
|
const currentAvailable = quota.meta.available;
|
|
quota.meta.todayUsed = Math.max(0, startAvailable - currentAvailable);
|
|
}
|
|
else {
|
|
quota.meta.todayUsed = 0;
|
|
}
|
|
}
|
|
}
|
|
res.json(quotas);
|
|
}
|
|
catch (error) {
|
|
console.error('Error fetching quotas:', error);
|
|
res.status(500).json({ error: 'Internal Server Error' });
|
|
}
|
|
});
|
|
app.get('/api/history', async (req, res) => {
|
|
try {
|
|
const history = await (0, db_1.getQuotaHistory)();
|
|
res.json(history);
|
|
}
|
|
catch (error) {
|
|
console.error('Error fetching history:', error);
|
|
res.status(500).json({ error: 'Internal Server Error' });
|
|
}
|
|
});
|
|
app.post('/api/webhook/antigravity', (req, res) => {
|
|
try {
|
|
const { updateAntigravityQuota } = require('./services/providers');
|
|
updateAntigravityQuota(req.body);
|
|
res.json({ success: true, message: 'Antigravity quota updated' });
|
|
}
|
|
catch (error) {
|
|
console.error('Error updating Antigravity via webhook:', error);
|
|
res.status(500).json({ error: 'Failed to update' });
|
|
}
|
|
});
|
|
app.listen(PORT, '0.0.0.0', async () => {
|
|
await (0, db_1.initDb)();
|
|
await (0, providers_1.initAntigravityFromDb)();
|
|
console.log(`TokenWatch server running on port ${PORT}`);
|
|
});
|
|
//# sourceMappingURL=server.js.map
|