-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathserver.js
More file actions
86 lines (70 loc) · 2.51 KB
/
server.js
File metadata and controls
86 lines (70 loc) · 2.51 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
const express = require('express');
const helmet = require('helmet');
const cors = require('cors');
const { verifyApiKey } = require('./utils/auth');
require('dotenv').config();
const app = express();
// Startup validation for required .env variables (#25)
if (!process.env.PORT) {
console.warn('Warning: PORT is not defined in .env file. Falling back to default port 3000.');
}
const port = process.env.PORT || 3000;
// Security and middleware
app.use(helmet());
app.use(cors());
app.use(express.json());
// Mock data: A list of transactions to demonstrate pagination
const transactions = Array.from({ length: 100 }, (_, i) => ({
id: `tx_${i + 1}`,
type: i % 2 === 0 ? 'deposit' : 'withdrawal',
amount: (Math.random() * 10000).toFixed(2),
currency: 'USDC',
timestamp: new Date(Date.now() - i * 3600000).toISOString(), // 1 hour apart
status: 'completed'
}));
// Health check
app.get('/health', (req, res) => {
res.json({ status: 'ok', timestamp: Date.now() });
});
// Contract info (from README_SERVER.md)
app.get('/api/contracts', (req, res) => {
res.json({
invoice_nft: process.env.INVOICE_NFT_ID || 'CCYU3LOQI34VHVN3ZOSEBHHKL4YK36FMTOEGLRYDUDRGS7JOLLRKCEQM',
lending_pool: process.env.LENDING_POOL_ID || 'CDVJMVPLZJKXSJFDY5AWBOUIRN73BKU2SG674MQDH4GRE6BGBPQD33IQ'
});
});
// Transactions endpoint with actual pagination logic (Issue #35)
app.get('/api/transactions', (req, res) => {
// 1. Parse page and limit from query parameters (default to page 1, limit 10)
const page = parseInt(req.query.page) || 1;
const limit = parseInt(req.query.limit) || 10;
// 2. Calculate startIndex and endIndex
const startIndex = (page - 1) * limit;
const endIndex = page * limit;
// 3. Slice the array to get the requested chunk
const paginatedData = transactions.slice(startIndex, endIndex);
res.json({
data: paginatedData,
page,
limit,
totalCount: transactions.length,
totalPages: Math.ceil(transactions.length / limit)
});
});
// Mock routes for CI validation
app.get('/api/v1/version', (req, res) => {
res.json({ version: '1.0.0', api: 'v1' });
});
app.get('/api/v1/prices', (req, res) => {
res.json({ USDC: 1.00, XLM: 0.12 });
});
app.get('/api/v1/test', (req, res) => {
res.json({ status: 'test successful' });
});
// Global 404 Not Found handler
app.use((req, res) => {
res.status(404).json({ "error": "Route not found" });
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});