Skip to content
This repository was archived by the owner on Mar 10, 2024. It is now read-only.

Commit 85708b2

Browse files
Add rest of the files
1 parent d857d9f commit 85708b2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+3171
-0
lines changed

src/app.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* App
3+
* @author Alexandre Dewilde
4+
* @date 15/11/2020
5+
* @version 1.0.0
6+
*/
7+
8+
const express = require('express');
9+
const nunjucks = require('nunjucks');
10+
const path = require('path');
11+
const minify = require('express-minify');
12+
const logger = require('morgan');
13+
const compression = require('compression');
14+
const fs = require('fs');
15+
const index = require('./routes/index');
16+
const editor = require('./routes/editor');
17+
const legal = require('./routes/legal');
18+
const config = require('./config/config');
19+
20+
const app = express();
21+
app.disable("x-powered-by");
22+
23+
// Configure views folder
24+
nunjucks.configure(path.join(__dirname, 'views'), {
25+
autoescape: true,
26+
express: app
27+
});
28+
29+
// logger files
30+
const accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'), { flags: 'a' })
31+
32+
// Adding middleware
33+
if (config.PRODUCTION) {
34+
app.use(logger('combined', { stream: accessLogStream }));
35+
app.use(compression());
36+
}
37+
else {
38+
app.use(logger('dev'));
39+
}
40+
app.use(minify({ jsMatch: false }));
41+
42+
43+
//app.use(secure);
44+
app.use(express.json());
45+
app.use(express.urlencoded({extended: false}));
46+
// Set static folder
47+
app.use(express.static(path.join(__dirname, 'publics/')));
48+
49+
// Configure routes
50+
app.use('/', index);
51+
app.use('/editor', editor);
52+
app.use('/legal', legal);
53+
54+
55+
// 404 error
56+
app.all('*', (req, res) => {
57+
res.status(404).render('404.html', {production: config.PRODUCTION, client_versobe: config.CLIENT_VERBOSE});
58+
});
59+
60+
// Handle errors
61+
app.use((error, req, res, next) => {
62+
if (config.DEBUG) {
63+
console.error(error);
64+
}
65+
res.sendStatus(500);
66+
});
67+
68+
module.exports = app;

src/config/config dist.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"DEBUG" : true,
3+
"HOST": "localhost",
4+
"PORT": 5000,
5+
"DB_TYPE": "mysql",
6+
"PRODUCTION": false,
7+
"CLIENT_VERBOSE": 2,
8+
"DISCORD_WEBHOOK": null,
9+
"SSL": false,
10+
"KEY_FILE_SSL": null,
11+
"CERT_FILE_SSL" : null,
12+
"DB_CONFIG": {
13+
"DB_HOST": "db",
14+
"DB_USERNAME": "root",
15+
"DB_PASSWORD": "root",
16+
"DB_DATABASE": "codewe",
17+
"DB_PORT": "3306"
18+
},
19+
"METRICS": false,
20+
"METRICS_PORT": 8000
21+
}

src/config/config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
module.exports = JSON.parse(fs.readFileSync(path.join(__dirname, './config.json'), 'utf8'));

src/db/MongoDB.js

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
const { MongoClient, ObjectID } = require("mongodb");
2+
const configs = require('../config/config');
3+
const utils = require('../utils');
4+
5+
const baseCode = [
6+
{uuid: utils.uuid(Math.random().toString(), 10), content: 'def main(text: str) -> None:'},
7+
{uuid: utils.uuid(Math.random().toString(), 10), content: ' print(text)'},
8+
{uuid: utils.uuid(Math.random().toString(), 10), content: ''},
9+
{uuid: utils.uuid(Math.random().toString(), 10), content: 'if __name__ == \'__main__\':'},
10+
{uuid: utils.uuid(Math.random().toString(), 10), content: ' main(\'Hello World !\')'}
11+
];
12+
13+
14+
class MongoDB {
15+
constructor (username, password, host, database, port) {
16+
let url = `mongodb://${username}:${password}@${host}:${port}/?retryWrites=true&w=majority`;
17+
this.client = new MongoClient(url);
18+
}
19+
20+
async connect () {
21+
try {
22+
this.db = await this.client.connect();
23+
this.codeWe = await this.db.db('codewe');
24+
this.documentsCollection = await this.codeWe.collection('codewe');
25+
} catch (err) {
26+
if (configs.DEBUG) {
27+
console.error('Error with db connection');
28+
}
29+
throw new Error(err);
30+
}
31+
}
32+
33+
async createDocument () {
34+
let doc = {
35+
content: baseCode,
36+
creationDate: Date.now(),
37+
lastViewedDate: Date.now(),
38+
customDocumentName: '',
39+
documentOwner: '',
40+
editors: [],
41+
linkEdit: '',
42+
linkView: '',
43+
language: '',
44+
tab: 4
45+
};
46+
try {
47+
let results = (await this.documentsCollection.insertOne(doc));
48+
const documentLink = utils.uuid(results.insertedId.toString());
49+
this.documentsCollection.updateOne({_id: results.insertedId}, {$set: {documentLink: documentLink}})
50+
return documentLink;
51+
} catch (err) {
52+
if (configs.DEBUG) {
53+
console.error('Error when creating a new document');
54+
}
55+
throw new Error(err);
56+
}
57+
58+
}
59+
60+
async getDocument (documentLink) {
61+
try {
62+
return await this.documentsCollection.findOne({documentLink: documentLink});
63+
} catch (err) {
64+
if (configs.DEBUG) {
65+
console.error('Error when fetching document');
66+
}
67+
throw new Error(err);
68+
}
69+
}
70+
71+
async setLine (documentLink, uuid, content) {
72+
try {
73+
await this.documentsCollection.updateOne({documentLink: documentLink, 'content.uuid': uuid}, {$set: {'content.$.content': content}});
74+
} catch (err) {
75+
if (configs.DEBUG) {
76+
console.error('Error when changing line content');
77+
}
78+
throw new Error(err);
79+
}
80+
}
81+
82+
async newLine (documentLink, previousUuid, uuid, content) {
83+
// Insert a line at the right place
84+
//TODO is it possible in one operation ?
85+
// TODO is it possible to implement with bulk?
86+
try {
87+
let doc = await this.documentsCollection.findOne({documentLink: documentLink});
88+
let index = doc.content.findIndex(line => {
89+
return line.uuid == previousUuid;
90+
});
91+
this.documentsCollection.updateOne({documentLink: documentLink}, {
92+
$push: {
93+
content: {
94+
$each : [{uuid: uuid, content: content}],
95+
$position : index + 1
96+
}
97+
}
98+
});
99+
} catch (err) {
100+
if (configs.DEBUG) {
101+
console.error('Error when adding a new line to document');
102+
}
103+
throw new Error(err);
104+
}
105+
}
106+
107+
async deleteLine (documentLink, uuid) {
108+
try {
109+
// Delete line at the right place
110+
await this.documentsCollection.updateOne({documentLink: documentLink}, {$pull: {content: {uuid: uuid}}});
111+
} catch (err) {
112+
if (configs.DEBUG) {
113+
console.error('Error when deleting a line in document');
114+
}
115+
throw new Error(err);
116+
}
117+
118+
}
119+
120+
async applyRequests (documentLink, requests) {
121+
// TODO look to use bulk write
122+
try {
123+
for (let request of requests) {
124+
let requestType = request.type;
125+
let data = request.data;
126+
switch (requestType) {
127+
case 'set-line':
128+
await this.setLine(documentLink, data.id, data.content);
129+
break;
130+
case 'new-line':
131+
await this.newLine(documentLink, data.previous, data.id, data.content);
132+
break;
133+
case 'delete-line':
134+
await this.deleteLine(documentLink, data.id);
135+
break;
136+
}
137+
}
138+
} catch (err) {
139+
if (configs.DEBUG) {
140+
console.error('Error when applying requests');
141+
}
142+
throw new Error(err);
143+
}
144+
}
145+
}
146+
147+
function getDB () {
148+
db = new MongoDB(
149+
configs.DB_CONFIG.DB_USERNAME,
150+
configs.DB_CONFIG.DB_PASSWORD,
151+
configs.DB_CONFIG.DB_HOST,
152+
configs.DB_CONFIG.DB_DATABASE,
153+
configs.DB_CONFIG.DB_PORT
154+
);
155+
db.connect();
156+
return db;
157+
}
158+
159+
module.exports = getDB();

src/metricsApp.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const express = require("express");
2+
const promBundle = require("express-prom-bundle");
3+
4+
const promMetrics = promBundle({
5+
includeMethod: true,
6+
includePath: true,
7+
autoregister: false
8+
});
9+
10+
const { promClient, metricsMiddleware } = promMetrics;
11+
12+
const metricsApp = express();
13+
metricsApp.disable("x-powered-by");
14+
metricsApp.use(metricsMiddleware);
15+
16+
module.exports = { metricsMiddleware, metricsApp };
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/**
2+
* Tab customization (allow convert current file)
3+
*/
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* This component deals with cards alerts.
3+
* @author Brieuc Dubois
4+
* @date Created on 14/11/2020
5+
* @date Last modification on 14/11/2020
6+
* @version 1.0.0
7+
*/
8+
9+
import Random from "/js/dev/utils/random.js";
10+
11+
/**
12+
* Create temporary card on the top right of screen
13+
* @param {string} title
14+
* @param {string} message
15+
* @param {number} duration
16+
* @param {string} color
17+
*/
18+
export default function temporaryCardAlert(title, message, duration, color='#1e90ff'){
19+
const id = Random.string(11);
20+
const card = document.createElement('div');
21+
card.classList.add('alertCard');
22+
card.style.backgroundColor = color;
23+
card.id = id;
24+
card.innerHTML = "<h3>" + title + "</h3><p>" + message + "</p>";
25+
document.getElementById('body').appendChild(card);
26+
27+
setTimeout(() => {
28+
card.remove();
29+
}, duration);
30+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* This component deals with the welcome message
3+
* @author Brieuc Dubois
4+
* @date Created on 14/11/2020
5+
* @date Last modification on 15/11/2020
6+
* @version 1.0.1
7+
*/
8+
9+
import Cookie from "/js/dev/utils/cookie.js";
10+
import _ from "/js/dev/utils/element.js";
11+
12+
/**
13+
* Class for managing welcome message
14+
*/
15+
export default class Welcome{
16+
/**
17+
* Initialise Welcome functions
18+
* @param {HTMLElement} element
19+
*/
20+
constructor(element) {
21+
if(Cookie.exist('welcome') || document.documentURI.includes('/legal/')) Welcome.hide();
22+
else Welcome.show();
23+
element.addEventListener('click', Welcome.hideWithCookie);
24+
}
25+
/**
26+
* hide the welcome message
27+
*/
28+
static hide(){
29+
_.id('main').classList.remove('blur-background');
30+
_.id("welcome").style.display = "None";
31+
}
32+
33+
/**
34+
* hide the welcome message and set the cookie
35+
*/
36+
static hideWithCookie(){
37+
Welcome.hide();
38+
Cookie.set('welcome', 'true', 90);
39+
}
40+
41+
/**
42+
* Displays the welcome message
43+
*/
44+
static show(){
45+
_.id('main').classList.add('blur-background')
46+
_.id("welcome").style.display = "block";
47+
}
48+
}

src/publics/js/dev/config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* This module manage configuration.
3+
* @author Brieuc Dubois
4+
* @date Created on 14/11/2020
5+
* @date Last modification on 14/11/2020
6+
* @version 1.0.0
7+
*/
8+
9+
export default class Config{
10+
static isDebug(){
11+
return true;
12+
}
13+
}

0 commit comments

Comments
 (0)