Skip to content

Commit 11571bc

Browse files
authored
Merge pull request #115 from Lemoncode/#114--Refactor-to-pods-structure-and-core
Refactored to pods and core structure
2 parents cadb6ad + fa5f37d commit 11571bc

34 files changed

Lines changed: 155 additions & 173 deletions

‎back/.babelrc‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
"core": "./src/core",
2323
"dals": "./src/dals",
2424
"pods": "./src/pods",
25-
"messages": "./src/messages",
26-
"business": "./src/business"
2725
}
2826
}
2927
]

‎back/.env‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
NODE_ENV=development
22
PORT=8081
3-
SOCKET_PORT=3000
43
MOCK_REPOSITORY=false
54
API_URL=/api
65
CORS_ORIGIN=http://localhost:8080
76
MONGODB_URI=mongodb://localhost:27017/code-paster
7+
STATIC_FILES_PATH="../../front/dist"

‎back/.vscode/launch.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"runtimeExecutable": null,
1111
"runtimeArgs": ["--nolazy"],
1212
"env": {
13-
"NODE_ENV": "development"
13+
"NODE_ENV": "development",
14+
"MOCK_REPOSITORY": "true"
1415
},
1516
"console": "internalConsole",
1617
"sourceMaps": true,

‎back/src/app.ts‎

Lines changed: 11 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,24 @@
1-
import cors from 'cors';
2-
import SocketIOClient, { Socket } from 'socket.io';
1+
import express from 'express';
2+
import path from 'path';
33

4-
import { createApp } from './express.server';
5-
import { envConstants, connectToDB } from 'core';
6-
import { api } from './api';
7-
import { processInputMessage, InputMessageTypes, processOutputMessageCollection, Action, SocketInfo } from './messages';
4+
import { createApp, createSocketServer } from 'core/servers';
5+
import { connectToDB } from 'core/db';
6+
import { envConstants } from 'core/constants';
7+
import { api } from 'pods/room';
88

99
const app = createApp();
10-
11-
let http = require('http').Server(app);
12-
// set up socket.io and bind it to our
13-
// http server.
14-
let io: SocketIOClient.Socket = require('socket.io')(http);
15-
16-
//options for cors midddleware
17-
const options: cors.CorsOptions = {
18-
allowedHeaders: [
19-
'Origin',
20-
'X-Requested-With',
21-
'Content-Type',
22-
'Accept',
23-
'X-Access-Token',
24-
],
25-
credentials: true,
26-
methods: 'GET,HEAD,OPTIONS,PUT,PATCH,POST,DELETE',
27-
// IMPORTANT LIMIT HERE YOUR CLIENT APPS DOMAINS
28-
origin: '*',
29-
preflightContinue: false,
30-
};
31-
32-
app.use(cors(options));
3310
app.use(envConstants.API_URL, api);
3411

35-
app.listen(envConstants.PORT, async () => {
12+
const staticFilesPath = path.resolve(__dirname, envConstants.STATIC_FILES_PATH);
13+
app.use('/', express.static(staticFilesPath));
14+
15+
const appServer = app.listen(envConstants.PORT, async () => {
3616
if(!envConstants.isMockRepository && envConstants.MONGODB_URI) {
3717
await connectToDB(envConstants.MONGODB_URI);
3818
}
3919
console.log(`Using ${envConstants.isMockRepository ? 'Mock' : 'DataBase'} for session storage`)
4020
console.log(`Server ready at http://localhost:${envConstants.PORT}${envConstants.API_URL}`);
4121
});
4222

43-
// whenever a user connects on port 3000 via
44-
// a websocket, log that a user has connected
45-
io.on('connection', async function (socket: Socket) {
46-
// WATCH OUT !! Reconnect is not implemented
47-
// In the connection input processing, we should
48-
// check if connectionId matches with userId and RoomId
49-
// if not reject, if it's accepted send connection
50-
// reestablished
51-
const { room, trainertoken } = socket.handshake.query;
52-
console.log(`room request: ${room}`);
53-
console.log(`trainer token: ${trainertoken}`);
54-
console.log('*** Session ID:', socket.conn.id);
55-
56-
let outputMessageCollection: Action[] = [];
57-
const socketInfo: SocketInfo = {
58-
socket: socket,
59-
io,
60-
connectionId: socket.conn.id,
61-
};
62-
63-
// TODO encapuslate this to processInputMessage
64-
if (trainertoken) {
65-
outputMessageCollection = await processInputMessage(socketInfo, {
66-
type: InputMessageTypes.ESTABLISH_CONNECTION_TRAINER,
67-
payload: {
68-
room,
69-
trainertoken,
70-
},
71-
});
72-
} else {
73-
outputMessageCollection = await processInputMessage(socketInfo, {
74-
type: InputMessageTypes.ESTABLISH_CONNECTION_STUDENT,
75-
payload: {
76-
room,
77-
},
78-
});
79-
}
80-
81-
processOutputMessageCollection(socketInfo, outputMessageCollection);
82-
83-
socket.on('message', async function (message: any) {
84-
console.log(message);
85-
if (message && message.type) {
86-
const outputMessageCollection: Action[] = await processInputMessage(
87-
socketInfo,
88-
message
89-
);
23+
createSocketServer(appServer);
9024

91-
await processOutputMessageCollection(socketInfo, outputMessageCollection);
92-
}
93-
});
94-
});
95-
96-
const server = http.listen(3001, function () {
97-
console.log('listening on *:3001');
98-
});

‎back/src/business/index.ts‎

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
export const envConstants = {
22
NODE_ENV: process.env.NODE_ENV,
33
PORT: process.env.PORT,
4-
SOCKET_PORT: process.env.SOCKET_PORT,
54
isMockRepository: process.env.MOCK_REPOSITORY === 'true',
65
MONGODB_URI: process.env.MONGODB_URI,
76
API_URL: process.env.API_URL,
87
CORS_ORIGIN: process.env.CORS_ORIGIN,
8+
isProduction: process.env.NODE_ENV === 'production',
9+
STATIC_FILES_PATH: process.env.STATIC_FILES_PATH
910
};
11+
1012

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const headerConstants = {
2+
protocol: 'x-forwarded-proto',
3+
httpsProtocol: 'https',
4+
host: 'host',
5+
};

‎back/src/core/constants/index.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export * from './env.constants';
1+
export * from './env.constants';
2+
export * from './header.constants';

‎back/src/core/servers/cors.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { envConstants } from 'core';
1+
import { envConstants } from 'core/constants';
22
import cors from 'cors';
33

44
const options: cors.CorsOptions = {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import express from 'express';
22
import bodyParser from 'body-parser';
33
import { corsOptions } from './cors'
4+
import { envConstants } from 'core/constants';
5+
import { redirectHttpsMiddleware } from './middlewares';
46

57
export const createApp = () => {
68
const app = express();
79

810
app.use(bodyParser.urlencoded({ extended: false }));
911
app.use(bodyParser.json());
1012
app.use(corsOptions);
13+
14+
if (envConstants.isProduction) {
15+
app.use(redirectHttpsMiddleware);
16+
}
17+
1118
return app;
1219
};

0 commit comments

Comments
 (0)