-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
164 lines (147 loc) · 4.22 KB
/
Copy pathserver.js
File metadata and controls
164 lines (147 loc) · 4.22 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const morgan = require('morgan');
const rateLimit = require('express-rate-limit');
const soap = require('soap');
const fs = require('fs');
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');
const swaggerUi = require('swagger-ui-express');
const swaggerJsdoc = require('swagger-jsdoc');
const { accounts } = require('./models/accounts');
const app = express();
// Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());
app.use(helmet());
app.use(morgan('combined'));
// Rate limiting
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
message: 'Too many requests from this IP, please try again later.'
});
app.use('/api/', limiter);
// SOAP Service
const wsdl = fs.readFileSync('./wsdl/account.wsdl', 'utf8');
const service = {
AccountService: {
AccountServicePort: {
getAccount: function(args) {
const account = accounts.find(a => a.id == args.id);
if (!account) {
throw new Error('Account not found');
}
return { account };
}
}
}
};
// GraphQL Schema
const schema = buildSchema(`
type Account {
id: Int
name: String
balance: Float
accountNumber: String
}
type Query {
accounts: [Account]
account(id: Int!): Account
}
`);
const root = {
accounts: () => accounts,
account: ({ id }) => accounts.find(a => a.id === id)
};
// Swagger
const swaggerOptions = {
definition: {
openapi: '3.0.0',
info: {
title: 'OpenBank API',
version: '1.0.0',
description: 'API for OpenBank banking services',
},
servers: [
{
url: 'http://localhost:8000',
},
],
components: {
securitySchemes: {
bearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
},
},
},
},
apis: ['./routes/*.js'],
};
const swaggerSpecs = swaggerJsdoc(swaggerOptions);
// Routes
const authRouter = require('./routes/auth');
const accountsRouter = require('./routes/accounts');
const customersRouter = require('./routes/customers');
const paymentsRouter = require('./routes/payments');
const consentsRouter = require('./routes/consents');
const transactionsRouter = require('./routes/transactions');
const { router: webhooksRouter } = require('./routes/webhooks');
app.use('/api/v1/auth', authRouter);
app.use('/api/v1/accounts', accountsRouter);
app.use('/api/v1/customers', customersRouter);
app.use('/api/v1/payments', paymentsRouter);
app.use('/api/v1/consents', consentsRouter);
app.use('/api/v1/transactions', transactionsRouter);
app.use('/api/v1/webhooks', webhooksRouter);
// GraphQL
app.use('/graphql', graphqlHTTP({ schema, root, graphiql: true }));
// Swagger
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpecs));
// Basic route
app.get('/', (req, res) => {
res.json({ message: 'Welcome to OpenBank API' });
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ error: 'Something went wrong!' });
});
const PORT = process.env.PORT || 8000;
const server = app.listen(PORT, () => {
console.log(`OpenBank server running on port ${PORT}`);
});
// Attach SOAP service
soap.listen(server, '/soap/account', service, wsdl);
// WebSocket
const WebSocket = require('ws');
const wss = new WebSocket.Server({ server });
const clients = [];
wss.on('connection', (ws) => {
clients.push(ws);
ws.on('message', (message) => {
console.log('Received: %s', message);
ws.send('Echo: ' + message);
});
ws.on('close', () => {
clients.splice(clients.indexOf(ws), 1);
});
});
// Broadcast function
function broadcast(data) {
clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(data));
}
});
}
// Simulate balance updates
setInterval(() => {
const account = accounts[Math.floor(Math.random() * accounts.length)];
account.balance += Math.random() * 100 - 50; // random change
broadcast({ type: 'balanceUpdate', accountId: account.id, balance: account.balance });
}, 10000);