Skip to content

Commit 38d01ce

Browse files
committed
Initial commit
Copied simple logic of server from previous cpps project
0 parents  commit 38d01ce

16 files changed

+3541
-0
lines changed

.eslintrc.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"extends": ["google"],
3+
"parserOptions": {
4+
"ecmaVersion": 8,
5+
"sourceType": "module",
6+
"ecmaFeatures": {
7+
"jsx": true,
8+
"experimentalObjectRestSpread": true
9+
}
10+
},
11+
"env": {
12+
"node": true,
13+
"es6": true
14+
},
15+
"rules": {
16+
"new-cap": ["error", {
17+
"capIsNewExceptions": ["Router", "ObjectId"]
18+
}],
19+
"require-jsdoc": "off",
20+
"max-len": "off",
21+
"no-undef": "error"
22+
}
23+
}

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules/*
2+
.eslintrc
3+
server/secret.js
4+
logs/
5+
.env
6+
backup/

package-lock.json

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "cpps",
3+
"version": "1.0.0",
4+
"description": "A portal all about competitive programming and problem solving",
5+
"main": "server/index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "Mohammad Samiul Islam <[email protected]>",
10+
"license": "MIT",
11+
"dependencies": {
12+
"body-parser": "^1.18.3",
13+
"connect-mongo": "^2.0.1",
14+
"cookie-parser": "^1.4.3",
15+
"express": "^4.16.3",
16+
"express-session": "^1.15.6",
17+
"mongoose": "^5.1.4",
18+
"nodemailer": "^4.6.5",
19+
"nodemailer-mailgun-transport": "^1.4.0",
20+
"winston": "^3.0.0-rc6"
21+
},
22+
"devDependencies": {
23+
"eslint": "^4.19.1",
24+
"eslint-config-google": "^0.9.1"
25+
}
26+
}

server/config/database.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const mongoose = require('mongoose');
2+
const dburl = require('./index.js').secretModule.dburl;
3+
4+
mongoose.Promise = global.Promise;
5+
mongoose.connect(dburl, function(err) {
6+
if (err) console.log(err);
7+
else console.log('Successfully connected to database');
8+
});

server/config/index.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
secretModule: require('../secret.js'),
3+
port: process.env.PORT || 8002,
4+
database: {
5+
init() {
6+
require('./database.js');
7+
}
8+
},
9+
session: {
10+
init(app) {
11+
require('./session.js').addSession(app);
12+
}
13+
}
14+
};

server/config/session.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const session = require('express-session');
2+
const MongoStore = require('connect-mongo')(session);
3+
const secret = require('./index.js').secretModule.secret;
4+
const mongoose = require('mongoose');
5+
const cookieParser = require('cookie-parser');
6+
7+
module.exports = {
8+
addSession(app) {
9+
app.use(cookieParser(secret));
10+
11+
app.use(session({
12+
secret,
13+
resave: false,
14+
saveUninitialized: false,
15+
store: new MongoStore({
16+
mongooseConnection: mongoose.connection,
17+
ttl: 2 * 60 * 60,
18+
touchAfter: 2 * 3600
19+
})
20+
}));
21+
}
22+
};

server/index.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const express = require('express');
2+
const bodyParser = require('body-parser');
3+
const config = require('config');
4+
const logger = require('logger');
5+
6+
const app = express();
7+
const server = require('http').createServer(app);
8+
9+
app.set('port', config.port);
10+
11+
app.use(bodyParser.json()); // support json encoded bodies
12+
app.use(bodyParser.urlencoded({
13+
extended: true,
14+
})); // support encoded bodies
15+
16+
config.database.init();
17+
config.session.init(app);
18+
19+
process.on('unhandledRejection', (error) => {
20+
logger.error({
21+
severe: true,
22+
error: error.stack,
23+
});
24+
process.exit(1);
25+
});
26+
27+
process.on('uncaughtException', function(error) {
28+
logger.error({
29+
severe: true,
30+
error: error.stack,
31+
});
32+
process.exit(1);
33+
});
34+
35+
if (require.main === module) {
36+
server.listen(app.get('port'), function() {
37+
logger.info(`Server running at port ${app.get('port')}`);
38+
});
39+
} else {
40+
module.exports = {
41+
server,
42+
app,
43+
};
44+
}

server/node_modules/config.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/node_modules/escapeLatex/index.js

+57
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/node_modules/logger/index.js

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/node_modules/mailer/index.js

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/scripts/dbInit.js

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* Intiation Script
3+
*
4+
* 1. Creates a root folder if it does not exist
5+
* 2. Creates a notebook note if it does not exist
6+
* 3. Create admin
7+
*/
8+
9+
const mongoose = require('mongoose');
10+
const dburl = require('../secret.js').dburl;
11+
const readline = require('readline');
12+
const _ = require('lodash');
13+
const rl = readline.createInterface({
14+
input: process.stdin,
15+
output: process.stdout
16+
});
17+
18+
mongoose.Promise = global.Promise;
19+
20+
function warning (){
21+
_.times(5, function(){
22+
console.log("***Warning*** Creating root account");
23+
})
24+
}
25+
26+
function handleError(err){
27+
console.log(err);
28+
process.exit();
29+
}
30+
31+
async main() {
32+
try {
33+
const promise = await mongoose.connect(dburl, {
34+
useMongoClient: true
35+
});
36+
37+
console.log('Successfully connected to database');
38+
39+
require('../models/userModel');
40+
const User = mongoose.model('User');
41+
42+
require('../models/gateModel')
43+
const Gate = mongoose.model('Gate');
44+
45+
const root = await Gate.findOne({_id: '000000000000000000000000'}).exec();
46+
if ( !root ) {
47+
const newRoot = new Gate({
48+
_id: '000000000000000000000000',
49+
type: 'folder',
50+
ancestor: [],
51+
ind: 0,
52+
title: 'Root'
53+
});
54+
await newRoot.save();
55+
}
56+
57+
warning();
58+
const email = await new Promise(function(resolve, reject) {
59+
rl.question('Enter email for admin: ', function(email){
60+
return resolve(email);
61+
});
62+
});
63+
64+
const password = await new Promise(function(resolve, reject) {
65+
rl.question('Enter password for admin: ', function(password) {
66+
return resolve(password);
67+
})
68+
});
69+
70+
const pass = User.createHash(password);
71+
const user = new User({
72+
email,
73+
password: pass,
74+
status: 'root',
75+
verified: 'true'
76+
});
77+
await user.save();
78+
79+
} catch (err) {
80+
handleError(err);
81+
}
82+
}

server/secret.example.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = {
2+
secret: "some-secret-string",
3+
dburl: "mongodb://cpps_db_1:27017/cpps",
4+
mailApi: "secret-mail-api",
5+
siteName: "cpps",
6+
domain: "localhost:8000",
7+
subdomain: "www",
8+
recaptcha: {
9+
site: "some-site",
10+
secret: "some-secret"
11+
},
12+
ojscraper: {
13+
loj: {
14+
credential: {
15+
userId: 'some-id',
16+
password: 'some-password',
17+
}
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)