-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
68 lines (58 loc) · 1.93 KB
/
Copy pathserver.js
File metadata and controls
68 lines (58 loc) · 1.93 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
import 'dotenv/config';
import express from 'express';
import cors from 'cors';
import axios from 'axios';
import path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const app = express();
app.use(cors());
const LM_API_KEY = process.env.LM_API_KEY;
const LM_HOSTNAME = process.env.LM_HOSTNAME;
if (!LM_API_KEY || !LM_HOSTNAME) {
console.error('Missing LM_API_KEY or LM_HOSTNAME in .env');
process.exit(1);
}
async function proxyLM(res, path, params = {}) {
try {
const response = await axios.get(`${LM_HOSTNAME}${path}`, {
headers: { Authorization: `Bearer ${LM_API_KEY}` },
params
});
res.json(response.data);
} catch (err) {
const status = err.response?.status || 500;
const msg = err.response?.data?.error || err.message;
res.status(status).json({ error: msg });
}
}
app.get('/api/transactions', async (req, res) => {
const { start_date, end_date } = req.query;
if (!start_date || !end_date) {
return res.status(400).json({ error: 'start_date and end_date required' });
}
await proxyLM(res, '/v1/transactions', { start_date, end_date });
});
app.get('/api/assets', async (req, res) => {
await proxyLM(res, '/v1/assets');
});
app.get('/api/plaid_accounts', async (req, res) => {
await proxyLM(res, '/v1/plaid_accounts');
});
app.get('/api/budgets', async (req, res) => {
const { start_date, end_date } = req.query;
if (!start_date || !end_date) {
return res.status(400).json({ error: 'start_date and end_date required' });
}
await proxyLM(res, '/v1/budgets', { start_date, end_date });
});
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, 'dist')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});
}
const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});