Skip to content

Commit c3a1869

Browse files
authored
Merge pull request #108 from Lemoncode/#96-Create-storage-contract-and-mocked-storage
#96 create storage contract and mocked storage
2 parents 30bb940 + bcf8f02 commit c3a1869

24 files changed

Lines changed: 204 additions & 128 deletions

‎back/.babelrc‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,19 @@
1313
}
1414
]
1515
],
16-
"plugins": ["@babel/plugin-proposal-optional-chaining"]
16+
"plugins": ["@babel/plugin-proposal-optional-chaining",
17+
[
18+
"module-resolver",
19+
{
20+
"root": ["."],
21+
"alias": {
22+
"core": "./src/core",
23+
"dals": "./src/dals",
24+
"pods": "./src/pods",
25+
"messages": "./src/messages",
26+
"business": "./src/business"
27+
}
28+
}
29+
]
30+
]
1731
}

‎back/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"@types/jest": "^26.0.15",
4444
"@types/mongoose": "^5.7.37",
4545
"@types/socket.io": "^2.1.10",
46+
"babel-plugin-module-resolver": "^4.0.0",
4647
"cross-env": "^7.0.2",
4748
"jest": "^26.6.1",
4849
"nodemon": "^2.0.4",

‎back/src/app.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ app.listen(envConstants.PORT, () => {
4343

4444
// whenever a user connects on port 3000 via
4545
// a websocket, log that a user has connected
46-
io.on('connection', function (socket: Socket) {
46+
io.on('connection', async function (socket: Socket) {
4747
// WATCH OUT !! Reconnect is not implemented
4848
// In the connection input processing, we should
4949
// check if connectionId matches with userId and RoomId
@@ -63,15 +63,15 @@ io.on('connection', function (socket: Socket) {
6363

6464
// TODO encapuslate this to processInputMessage
6565
if (trainertoken) {
66-
outputMessageCollection = processInputMessage(socketInfo, {
66+
outputMessageCollection = await processInputMessage(socketInfo, {
6767
type: InputMessageTypes.ESTABLISH_CONNECTION_TRAINER,
6868
payload: {
6969
room,
7070
trainertoken,
7171
},
7272
});
7373
} else {
74-
outputMessageCollection = processInputMessage(socketInfo, {
74+
outputMessageCollection = await processInputMessage(socketInfo, {
7575
type: InputMessageTypes.ESTABLISH_CONNECTION_STUDENT,
7676
payload: {
7777
room,
@@ -81,15 +81,15 @@ io.on('connection', function (socket: Socket) {
8181

8282
processOutputMessageCollection(socketInfo, outputMessageCollection);
8383

84-
socket.on('message', function (message: any) {
84+
socket.on('message', async function (message: any) {
8585
console.log(message);
8686
if (message && message.type) {
87-
const outputMessageCollection: Action[] = processInputMessage(
87+
const outputMessageCollection: Action[] = await processInputMessage(
8888
socketInfo,
8989
message
9090
);
9191

92-
processOutputMessageCollection(socketInfo, outputMessageCollection);
92+
await processOutputMessageCollection(socketInfo, outputMessageCollection);
9393
}
9494
});
9595
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const envConstants = {
2+
NODE_ENV: process.env.NODE_ENV,
3+
PORT: process.env.PORT,
4+
SOCKET_PORT: process.env.SOCKET_PORT,
5+
isMockRepository: process.env.MOCK_REPOSITORY === 'true',
6+
MONGODB_URI: process.env.MONGODB_URI,
7+
API_URL: process.env.API_URL,
8+
CORS_ORIGIN: process.env.CORS_ORIGIN,
9+
};
10+

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './env.constants';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { connect } from 'mongoose';
2+
3+
export const connectToDB = async (connectionString: string) => {
4+
const db = await connect(connectionString, {
5+
poolSize: 5,
6+
useNewUrlParser: true,
7+
promiseLibrary: Promise,
8+
useUnifiedTopology: true,
9+
useFindAndModify: false,
10+
});
11+
12+
console.log('Connected to DB');
13+
14+
return db;
15+
};

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { envConstants } from '../constants';
2+
import cors from 'cors';
3+
4+
const options: cors.CorsOptions = {
5+
allowedHeaders: [
6+
'Origin',
7+
'X-Requested-With',
8+
'Content-Type',
9+
'Accept',
10+
'X-Access-Token',
11+
],
12+
credentials: true,
13+
methods: 'GET,HEAD,OPTIONS,PUT,PATCH,POST,DELETE',
14+
// IMPORTANT LIMIT HERE YOUR CLIENT APPS DOMAINS
15+
origin: envConstants.CORS_ORIGIN,
16+
preflightContinue: false,
17+
};
18+
19+
export const corsOptions = cors(options);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import express from 'express';
2+
import bodyParser from 'body-parser';
3+
import { corsOptions } from './cors'
4+
5+
export const createApp = () => {
6+
const app = express();
7+
8+
app.use(bodyParser.urlencoded({ extended: false }));
9+
app.use(bodyParser.json());
10+
app.use(corsOptions);
11+
return app;
12+
};

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from './express.server';
2+
3+
//TODO: refactoring
4+
//export * from './sockets.server';

‎back/src/dals/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './room';
2+
export * from './session';

0 commit comments

Comments
 (0)