-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
77 lines (67 loc) · 2.58 KB
/
app.js
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
const express = require("express");
const mongoose = require("mongoose");
const cors = require("cors");
const axios = require("axios");
const authRoutes = require("./routes/authRoutes");
const walletRoutes = require("./routes/walletRoutes");
const lightningRoutes = require("./routes/lightningRoutes");
const litecoinRoutes = require("./routes/litecoinRoutes");
const stakeRoutes = require("./routes/stakeRoutes");
const contactRoutes = require('./routes/contactRoutes');
const { errorHandler } = require("./middleware/errorMiddleware");
const { initLightning } = require("./services/lightningService");
const { authMiddleware } = require("./middleware/authMiddleware");
require("dotenv").config();
const app = express();
// app.use(cors());
app.use(
cors({
origin: ["http://localhost:8081", "http://192.168.1.199:8081"],
})
);
app.use(express.json());
mongoose
.connect(process.env.MONGODB_URI, {
// useNewUrlParser: true,
// useUnifiedTopology: true,
// useCreateIndex: true,
// useFindAndModify: false,
})
.then(() => console.log("Connected to MongoDB"))
.catch((err) => console.error("MongoDB connection error:", err));
app.use("/api/auth", authRoutes);
app.use("/api/wallet", walletRoutes);
app.use("/api/wallet", walletRoutes);
app.use('/api/lightning', lightningRoutes);
app.use("/api/litecoin", litecoinRoutes);
app.use('/api/contacts', contactRoutes);
app.use("/api/stake", stakeRoutes);
app.get("/convert", async (req, res) => {
try {
const { amount, from, to } = req.query;
const response = await axios.get(
`https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,litecoin&vs_currencies=usd`
);
const btcPrice = response.data.bitcoin.usd;
const ltcPrice = response.data.litecoin.usd;
let convertedAmount;
if (from.toUpperCase() === "BTC" && to.toUpperCase() === "USD") {
convertedAmount = amount * btcPrice;
} else if (from.toUpperCase() === "USD" && to.toUpperCase() === "BTC") {
convertedAmount = amount / btcPrice;
} else if (from.toUpperCase() === "LTC" && to.toUpperCase() === "USD") {
convertedAmount = amount * ltcPrice;
} else if (from.toUpperCase() === "USD" && to.toUpperCase() === "LTC") {
convertedAmount = amount / ltcPrice;
} else {
throw new Error("Invalid conversion parameters");
}
res.json({ amount: convertedAmount.toFixed(8) });
} catch (error) {
res.status(400).json({ error: error.message });
}
});
app.use(errorHandler);
initLightning().catch(console.error);
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));