-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
112 lines (97 loc) · 3.54 KB
/
server.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const express = require('express');
const path = require('path');
const axios = require('axios');
const dotenv = require('dotenv');
const winston = require('winston');
dotenv.config();
// Configure logger
const logger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json()
),
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' })
]
});
const app = express();
const port = process.env.PORT || 3001;
app.use(express.static('public'));
app.use(express.json());
// Encryptus API configuration
const ENCRYPTUS_API_URL = process.env.ENCRYPTUS_API_URL;
const PARTNER_EMAIL = process.env.PARTNER_EMAIL;
const PARTNER_PASSWORD = process.env.PARTNER_PASSWORD;
const CLIENT_ID = process.env.CLIENT_ID;
const CLIENT_SECRET = process.env.CLIENT_SECRET;
// Get access token
app.get('/api/access-token', async (req, res) => {
try {
console.log("I am here");
logger.info('Requesting Encryptus access token');
console.log(ENCRYPTUS_API_URL,PARTNER_EMAIL,PARTNER_PASSWORD,CLIENT_ID,CLIENT_SECRET);
const response = await axios.post(`${ENCRYPTUS_API_URL}/generate/token`, {
partnerEmail: PARTNER_EMAIL,
partnerPassword: PARTNER_PASSWORD,
grant_services: [
"FORENSICS"
],
clientID: CLIENT_ID,
clientSecret: CLIENT_SECRET
});
console.log("11111",response.data.access_token);
if (!response.data.access_token) {
throw new Error('Invalid access token response');
}
logger.info('Successfully obtained access token');
res.json({ access_token: response.data.access_token });
} catch (error) {
logger.error('Error getting access token:', error);
res.status(500).json({ error: 'Failed to get access token' });
}
});
// Get payout link
app.get('/api/payout-link', async (req, res) => {
try {
logger.info('Requesting Encryptus payout link');
const authHeader = req.headers.authorization;
if (!authHeader?.startsWith('Bearer ')) {
throw new Error('No access token provided');
}
const accessToken = authHeader.split(' ')[1];
console.log("33333",accessToken);
const response = await axios.get(`${ENCRYPTUS_API_URL}/generate/payoutlink`,
{
headers: {
'accept': '*/*',
'Authorization': `Bearer ${accessToken}`
}
}
);
console.log("22222",response.data);
if (!response.data?.data?.link) {
throw new Error('Invalid payout link response');
}
logger.info('Successfully obtained payout link');
res.json({ data: { link: response.data.data.link } });
} catch (error) {
logger.error('Error getting payout link:', error);
const errorMessage = error.response?.data?.message || 'Failed to get payout link';
const statusCode = error.response?.status || 500;
res.status(statusCode).json({
error: errorMessage,
details: error.response?.data
});
}
});
// Error handling middleware
app.use((err, req, res, next) => {
logger.error('Unhandled error:', err);
res.status(500).json({ error: 'Internal server error' });
});
app.listen(port, () => {
logger.info(`Server running on port ${port}`);
});