diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/server/database.ts | 26 | ||||
-rw-r--r-- | src/server/index.ts | 124 |
2 files changed, 58 insertions, 92 deletions
diff --git a/src/server/database.ts b/src/server/database.ts index 6cc9945b9..e08385d98 100644 --- a/src/server/database.ts +++ b/src/server/database.ts @@ -1,20 +1,20 @@ import * as mongodb from 'mongodb'; export class Database { + public static DocumentsCollection = 'documents'; public static Instance = new Database(); private MongoClient = mongodb.MongoClient; private url = 'mongodb://localhost:27017/Dash'; + private currentWrites: { [_id: string]: Promise<void> } = {}; private db?: mongodb.Db; constructor() { this.MongoClient.connect(this.url, (err, client) => this.db = client.db()); } - private currentWrites: { [_id: string]: Promise<void> } = {}; - - public update(id: string, value: any, callback: () => void) { + public update(id: string, value: any, callback: () => void, collectionName = Database.DocumentsCollection) { if (this.db) { - let collection = this.db.collection('documents'); + let collection = this.db.collection(collectionName); const prom = this.currentWrites[id]; const run = (): Promise<void> => { let newProm = new Promise<void>(resolve => { @@ -40,28 +40,28 @@ export class Database { } } - public delete(id: string) { - this.db && this.db.collection('documents').remove({ _id: id }); + public delete(id: string, collectionName = Database.DocumentsCollection) { + this.db && this.db.collection(collectionName).remove({ _id: id }); } - public deleteAll(collectionName: string = 'documents'): Promise<any> { + public deleteAll(collectionName = Database.DocumentsCollection): Promise<any> { return new Promise(res => this.db && this.db.collection(collectionName).deleteMany({}, res)); } - public insert(kvpairs: any) { - this.db && this.db.collection('documents').insertOne(kvpairs, (err: any, res: any) => + public insert(kvpairs: any, collectionName = Database.DocumentsCollection) { + this.db && this.db.collection(collectionName).insertOne(kvpairs, (err, res) => err // && console.log(err) ); } - public getDocument(id: string, fn: (res: any) => void) { - this.db && this.db.collection('documents').findOne({ _id: id }, (err: any, result: any) => + public getDocument(id: string, fn: (res: any) => void, collectionName = Database.DocumentsCollection) { + this.db && this.db.collection(collectionName).findOne({ _id: id }, (err, result) => fn(result ? result : undefined)); } - public getDocuments(ids: string[], fn: (res: any) => void) { - this.db && this.db.collection('documents').find({ _id: { "$in": ids } }).toArray((err, docs) => { + public getDocuments(ids: string[], fn: (res: any) => void, collectionName = Database.DocumentsCollection) { + this.db && this.db.collection(collectionName).find({ _id: { "$in": ids } }).toArray((err, docs) => { if (err) { console.log(err.message); console.log(err.errmsg); diff --git a/src/server/index.ts b/src/server/index.ts index b9c7448b4..a6fe6fa2c 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -1,49 +1,45 @@ +import * as bodyParser from 'body-parser'; +import { exec } from 'child_process'; +import * as cookieParser from 'cookie-parser'; import * as express from 'express'; -const app = express(); -import * as webpack from 'webpack'; -import * as wdm from 'webpack-dev-middleware'; -import * as whm from 'webpack-hot-middleware'; -import * as path from 'path'; +import * as session from 'express-session'; +import * as expressValidator from 'express-validator'; import * as formidable from 'formidable'; +import * as fs from 'fs'; +import * as mobileDetect from 'mobile-detect'; +import { ObservableMap } from 'mobx'; import * as passport from 'passport'; -import { MessageStore, Transferable } from "./Message"; -import { Client } from './Client'; +import * as path from 'path'; +import * as request from 'request'; +import * as io from 'socket.io'; import { Socket } from 'socket.io'; +import * as webpack from 'webpack'; +import * as wdm from 'webpack-dev-middleware'; +import * as whm from 'webpack-hot-middleware'; +import { Field, FieldId } from '../fields/Field'; import { Utils } from '../Utils'; -import { ObservableMap } from 'mobx'; -import { FieldId, Field } from '../fields/Field'; +import { getForgot, getLogin, getLogout, getReset, getSignup, postForgot, postLogin, postReset, postSignup } from './authentication/controllers/user_controller'; +import { DashUserModel } from './authentication/models/user_model'; +import { Client } from './Client'; import { Database } from './database'; -import * as io from 'socket.io'; -import { getLogin, postLogin, getSignup, postSignup, getLogout, postReset, getForgot, postForgot, getReset } from './authentication/controllers/user_controller'; +import { MessageStore, Transferable } from "./Message"; +import { RouteStore } from './RouteStore'; +const app = express(); const config = require('../../webpack.config'); const compiler = webpack(config); const port = 1050; // default port to listen const serverPort = 4321; -import * as expressValidator from 'express-validator'; import expressFlash = require('express-flash'); import flash = require('connect-flash'); -import * as bodyParser from 'body-parser'; -import * as session from 'express-session'; -import * as cookieParser from 'cookie-parser'; -import * as mobileDetect from 'mobile-detect'; import c = require("crypto"); const MongoStore = require('connect-mongo')(session); const mongoose = require('mongoose'); -import { DashUserModel } from './authentication/models/user_model'; -import * as fs from 'fs'; -import * as request from 'request'; -import { RouteStore } from './RouteStore'; -import { exec } from 'child_process'; -const download = (url: string, dest: fs.PathLike) => { - request.get(url).pipe(fs.createWriteStream(dest)); -}; +const download = (url: string, dest: fs.PathLike) => request.get(url).pipe(fs.createWriteStream(dest));; const mongoUrl = 'mongodb://localhost:27017/Dash'; mongoose.connect(mongoUrl); -mongoose.connection.on('connected', function () { - console.log("connected"); -}); +mongoose.connection.on('connected', () => console.log("connected")); // SESSION MANAGEMENT AND AUTHENTICATION MIDDLEWARE // ORDER OF IMPORTS MATTERS @@ -54,9 +50,7 @@ app.use(session({ resave: true, cookie: { maxAge: 7 * 24 * 60 * 60 * 1000 }, saveUninitialized: true, - store: new MongoStore({ - url: 'mongodb://localhost:27017/Dash' - }) + store: new MongoStore({ url: 'mongodb://localhost:27017/Dash' }) })); app.use(flash()); @@ -71,9 +65,7 @@ app.use((req, res, next) => { next(); }); -app.get("/hello", (req, res) => { - res.send("<p>Hello</p>"); -}); +app.get("/hello", (req, res) => res.send("<p>Hello</p>")); enum Method { GET, @@ -112,21 +104,17 @@ function addSecureRoute(method: Method, } // STATIC FILE SERVING - -let FieldStore: ObservableMap<FieldId, Field> = new ObservableMap(); - app.use(express.static(__dirname + RouteStore.public)); app.use(RouteStore.images, express.static(__dirname + RouteStore.public)); -app.get("/pull", (req, res) => { +app.get("/pull", (req, res) => exec('"C:\\Program Files\\Git\\git-bash.exe" -c "git pull"', (err, stdout, stderr) => { if (err) { res.send(err.message); return; } res.redirect("/"); - }); -}); + })); // GETTERS @@ -143,11 +131,8 @@ addSecureRoute( Method.GET, (user, res, req) => { let detector = new mobileDetect(req.headers['user-agent'] || ""); - if (detector.mobile() !== null) { - res.sendFile(path.join(__dirname, '../../deploy/mobile/image.html')); - } else { - res.sendFile(path.join(__dirname, '../../deploy/index.html')); - } + let filename = detector.mobile() !== null ? 'mobile/image.html' : 'index.html'; + res.sendFile(path.join(__dirname, '../../deploy/' + filename)); }, undefined, RouteStore.home, @@ -163,12 +148,7 @@ addSecureRoute( addSecureRoute( Method.GET, - (user, res) => { - res.send(JSON.stringify({ - id: user.id, - email: user.email - })); - }, + (user, res) => res.send(JSON.stringify({ id: user.id, email: user.email })), undefined, RouteStore.getCurrUser ); @@ -185,10 +165,9 @@ addSecureRoute( console.log("upload"); form.parse(req, (err, fields, files) => { console.log("parsing"); - let names: any[] = []; + let names: string[] = []; for (const name in files) { - let file = files[name]; - names.push(`/files/` + path.basename(file.path)); + names.push(`/files/` + path.basename(files[name].path)); } res.send(names); }); @@ -218,28 +197,22 @@ app.post(RouteStore.forgot, postForgot); app.get(RouteStore.reset, getReset); app.post(RouteStore.reset, postReset); -app.use(RouteStore.corsProxy, (req, res) => { - req.pipe(request(req.url.substring(1))).pipe(res); -}); +app.use(RouteStore.corsProxy, (req, res) => + req.pipe(request(req.url.substring(1))).pipe(res)); -app.get(RouteStore.delete, (req, res) => { - deleteFields().then(() => res.redirect(RouteStore.home)); -}); +app.get(RouteStore.delete, (req, res) => + deleteFields().then(() => res.redirect(RouteStore.home))); -app.get(RouteStore.deleteAll, (req, res) => { - deleteAll().then(() => res.redirect(RouteStore.home)); -}); +app.get(RouteStore.deleteAll, (req, res) => + deleteAll().then(() => res.redirect(RouteStore.home))); -app.use(wdm(compiler, { - publicPath: config.output.publicPath -})); +app.use(wdm(compiler, { publicPath: config.output.publicPath })); app.use(whm(compiler)); // start the Express server -app.listen(port, () => { - console.log(`server started at http://localhost:${port}`); -}); +app.listen(port, () => + console.log(`server started at http://localhost:${port}`)); const server = io(); interface Map { @@ -274,14 +247,8 @@ function barReceived(guid: String) { } function getField([id, callback]: [string, (result: any) => void]) { - Database.Instance.getDocument(id, (result: any) => { - if (result) { - callback(result); - } - else { - callback(undefined); - } - }); + Database.Instance.getDocument(id, (result: any) => + callback(result ? result : undefined)); } function getFields([ids, callback]: [string[], (result: any) => void]) { @@ -289,9 +256,8 @@ function getFields([ids, callback]: [string[], (result: any) => void]) { } function setField(socket: Socket, newValue: Transferable) { - Database.Instance.update(newValue._id, newValue, () => { - socket.broadcast.emit(MessageStore.SetField.Message, newValue); - }); + Database.Instance.update(newValue._id, newValue, () => + socket.broadcast.emit(MessageStore.SetField.Message, newValue)); } server.listen(serverPort); |