-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
146 lines (126 loc) · 4.67 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
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
require('dotenv').config();
const https = require('https');
const fs = require('fs');
const express = require('express');
const axios = require('axios');
const cors = require('cors');
const app = express();
const jwt = require('jsonwebtoken');
const validator = require('validator');
const sqlite3 = require('sqlite3').verbose();
const { readSSLFile } = require('./auth/security');
const setupWebSocketServer = require('./webServices/websocket');
// Require the endpoint modules
const basicEndpoint = require('./endpoints/basic');
const access = require('./endpoints/access');
const jsonConversions = require('./endpoints/jsonConversion');
const fileManager = require('./endpoints/fileManagement');
const prm = require('./endpoints/prm');
const registration = require('./endpoints/registration');
const logs = require('./endpoints/logging');
const userManagement = require('./endpoints/userManagement');
const clarityData = require('./endpoints/clarityData');
const formManagement = require('./endpoints/formManagement');
const { sendSMS } = require('./twilio/sms');
const stripe = require('./endpoints/stripe');
const qbo = require('./endpoints/qbo')
const path = require('path');
// const corsOptions = {
// origin: [
// 'http://localhost:3000',
// 'https://localhost:3000',
// 'http://localhost:3001',
// 'https://localhost:3001',
// 'http://localhost:1234',
// 'http://localhost:4040',
// 'http://localhost:4040',
// 'http://server.claritybusinesssolutions.ca',
// 'https://selecthomecleaning.ca',
// 'https://selecthomecleaning.app',
// 'https://www.selecthomecleaning.app'
// ], // or '*' for allowing any origin
// methods: ['GET','POST'], // Allowed methods
// allowedHeaders: ['Content-Type','Authorization'] // Allowed headers
// };
const corsOptions = {
origin: function (origin, callback) {
// Allowed origins
const allowedOrigins = [
'http://localhost:3000',
'https://localhost:3000',
'http://localhost:3001',
'https://localhost:3001',
'http://localhost:1234',
'http://localhost:4040',
'http://server.claritybusinesssolutions.ca',
'https://devtools.claritybusinesssolutions.ca',
'https://selecthomecleaning.ca',
'https://selecthomecleaning.app',
'https://www.selecthomecleaning.app'
];
// If no origin or origin is in allowedOrigins, allow the request
if (!origin || allowedOrigins.includes(origin)) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
methods: ['GET', 'POST'], // Allowed methods
allowedHeaders: ['Content-Type', 'Authorization'], // Allowed headers
credentials: true, // Enable this if you want cookies or authorization headers to be allowed
};
app.use(cors(corsOptions));
console.log("v.1.0.0")
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors(corsOptions));
app.use('/.well-known/acme-challenge', express.static(path.join(__dirname, '.well-known/acme-challenge')));
try {
// DEFINE VARIABLES
const authPrivateKey = process.env.SECRETKEY;
if (!authPrivateKey) {
throw new Error('Required environmental variable authPrivateKey is undefined');
}
const host = process.env.DEVhost;
if (!host) {
throw new Error('Required environmental variable host is undefined');
}
const httpsOptions = {
key: readSSLFile('/etc/letsencrypt/live/server.claritybusinesssolutions.ca/privkey.pem'),
cert: readSSLFile('/etc/letsencrypt/live/server.claritybusinesssolutions.ca/fullchain.pem')
};
let server;
if (!httpsOptions.key || !httpsOptions.cert) {
// Start server without SSL for local testing
server = app.listen(process.env.PORT || 4040, () => {
console.log(`[${new Date().toISOString()}] Server is running on port ${process.env.PORT || 4040} without SSL`);
});
} else {
// Start HTTPS server
server = https.createServer(httpsOptions, app).listen(4343, () => {
console.log(`[${new Date().toISOString()}] SSL Server is running on port 4343`);
});
// Start server without SSL for local testing
app.listen(process.env.PORT || 4040, () => {
console.log(`[${new Date().toISOString()}] Server is running on port ${process.env.PORT || 4040} without SSL`);
});
}
// Setup WebSocket server
setupWebSocketServer(server);
} catch (error) {
sendSMS(process.env.DEV_NUMBER, `Server start-up error: ${error.message}`);
console.error(`Server start-up error: ${error.message}`);
}
//ENDPOINT FILES
basicEndpoint(app);
access(app, express);
jsonConversions(app);
fileManager(app);
prm(app);
registration(app);
logs(app);
userManagement(app);
clarityData(app);
formManagement(app);
stripe(app);
qbo(app);