aboutsummaryrefslogtreecommitdiff
path: root/src/server
diff options
context:
space:
mode:
authormadelinegr <laura_wilson@brown.edu>2019-02-18 19:38:08 -0500
committermadelinegr <laura_wilson@brown.edu>2019-02-18 19:38:08 -0500
commit6fd2cec91efd6672a70e15a786954f92c1d23416 (patch)
tree0cbf4c1dd399bd041e05eb4c911a642547f673f9 /src/server
parent41ba832136aef2b7e6a5034486757aa4b3047cf9 (diff)
parent70a8b4ab8075af4d06efb04c083b3bf11636373e (diff)
Merge remote-tracking branch 'origin/server_database_merge' into authentication
Diffstat (limited to 'src/server')
-rw-r--r--src/server/Message.ts25
-rw-r--r--src/server/ServerUtil.ts34
-rw-r--r--src/server/database.ts87
-rw-r--r--src/server/index.js15
-rw-r--r--src/server/index.ts53
5 files changed, 114 insertions, 100 deletions
diff --git a/src/server/Message.ts b/src/server/Message.ts
index 658c5612b..e78c36ef1 100644
--- a/src/server/Message.ts
+++ b/src/server/Message.ts
@@ -51,9 +51,9 @@ export enum Types {
export class DocumentTransfer implements Transferable {
readonly type = Types.Document
- _id: String;
+ _id: string
- constructor(readonly obj: { type: Types, data: [string, string][], _id: String }) {
+ constructor(readonly obj: { type: Types, data: [string, string][], _id: string }) {
this._id = obj._id
}
}
@@ -61,12 +61,12 @@ export class DocumentTransfer implements Transferable {
export class ImageTransfer implements Transferable {
readonly type = Types.Image
- constructor(readonly _id: String) { }
+ constructor(readonly _id: string) { }
}
export class KeyTransfer implements Transferable {
name: string
- readonly _id: String
+ readonly _id: string
readonly type = Types.Key
constructor(i: string, n: string) {
@@ -78,18 +78,18 @@ export class KeyTransfer implements Transferable {
export class ListTransfer implements Transferable {
type = Types.List;
- constructor(readonly _id: String) { }
+ constructor(readonly _id: string) { }
}
export class NumberTransfer implements Transferable {
readonly type = Types.Number
- constructor(readonly value: number, readonly _id: String) { }
+ constructor(readonly value: number, readonly _id: string) { }
}
export class TextTransfer implements Transferable {
value: string
- readonly _id: String
+ readonly _id: string
readonly type = Types.Text
constructor(t: string, i: string) {
@@ -100,7 +100,7 @@ export class TextTransfer implements Transferable {
export class RichTextTransfer implements Transferable {
value: string
- readonly _id: String
+ readonly _id: string
readonly type = Types.Text
constructor(t: string, i: string) {
@@ -110,7 +110,7 @@ export class RichTextTransfer implements Transferable {
}
export interface Transferable {
- readonly _id: String
+ readonly _id: string
readonly type: Types
}
@@ -118,6 +118,9 @@ export namespace MessageStore {
export const Foo = new Message<string>("Foo");
export const Bar = new Message<string>("Bar");
export const AddDocument = new Message<DocumentTransfer>("Add Document");
- export const SetField = new Message<{ _id: String, data: any, type: Types }>("Set Field")
- export const GetField = new Message<GetFieldArgs>("Get Field")
+ export const SetField = new Message<{ _id: string, data: any, type: Types }>("Set Field")
+ export const GetField = new Message<string>("Get Field")
+ export const GetFields = new Message<string[]>("Get Fields")
+ export const GetDocument = new Message<string>("Get Document");
+ export const DeleteAll = new Message<any>("Delete All");
} \ No newline at end of file
diff --git a/src/server/ServerUtil.ts b/src/server/ServerUtil.ts
index 6757615fb..46c409ec4 100644
--- a/src/server/ServerUtil.ts
+++ b/src/server/ServerUtil.ts
@@ -11,47 +11,35 @@ import { Types } from './Message';
import { Utils } from '../Utils';
export class ServerUtils {
- public static FromJson(json: string): Field {
- let obj = JSON.parse(json)
+ public static FromJson(json: any): Field {
+ let obj = json
let data: any = obj.data
- let id: string = obj.id
+ let id: string = obj._id
let type: Types = obj.type
- if (!(data && id && type != undefined)) {
+ if (!(data !== undefined && id && type !== undefined)) {
console.log("how did you manage to get an object that doesn't have a data or an id?")
return new TextField("Something to fill the space", Utils.GenerateGuid());
}
switch (type) {
case Types.Number:
- return new NumberField(data, id)
+ return new NumberField(data, id, false)
case Types.Text:
- return new TextField(data, id)
+ return new TextField(data, id, false)
case Types.RichText:
- return new RichTextField(data, id)
+ return new RichTextField(data, id, false)
case Types.Key:
- return new Key(data, id)
+ return new Key(data, id, false)
case Types.Image:
- return new ImageField(data, id)
+ return new ImageField(new URL(data), id, false)
case Types.List:
- return new ListField(data, id)
+ return ListField.FromJson(id, data)
case Types.Document:
- let doc: Document = new Document(id)
+ let doc: Document = new Document(id, false)
let fields: [string, string][] = data as [string, string][]
fields.forEach(element => {
doc._proxies.set(element[0], element[1]);
- let keyId: string = element[0]
- let valueId: string = element[1]
- Server.GetField(keyId, (key: Field) => {
- if (key instanceof Key) {
- Server.GetField(valueId, (field: Field) => {
- doc.Set(key as Key, field)
- })
- }
- else {
- console.log("how did you get a key that isnt a key wtf")
- }
- })
});
return doc
}
diff --git a/src/server/database.ts b/src/server/database.ts
index 16211a2f6..07c5819ab 100644
--- a/src/server/database.ts
+++ b/src/server/database.ts
@@ -8,50 +8,71 @@ export class Database {
public static Instance = new Database()
private MongoClient = mongodb.MongoClient;
private url = 'mongodb://localhost:27017/Dash';
+ private db?: mongodb.Db;
- public update(id: String, value: any) {
- this.MongoClient.connect(this.url, (err, db) => {
- let collection = db.db().collection('documents');
- collection.update({ _id: id }, { $set: value });
- db.close();
- });
+ constructor() {
+ this.MongoClient.connect(this.url, (err, client) => {
+ this.db = client.db()
+ })
+ }
+
+ public update(id: string, value: any) {
+ if (this.db) {
+ let collection = this.db.collection('documents');
+ collection.update({ _id: id }, { $set: value }, {
+ upsert: true
+ });
+ }
}
public delete(id: string) {
- this.MongoClient.connect(this.url, (err, db) => {
- let collection = db.db().collection('documents');
+ if (this.db) {
+ let collection = this.db.collection('documents');
collection.remove({ _id: id });
- db.close();
- });
+ }
+ }
+
+ public deleteAll() {
+ if (this.db) {
+ let collection = this.db.collection('documents');
+ collection.deleteMany({});
+ }
}
public insert(kvpairs: any) {
- this.MongoClient.connect(this.url, (err, db) => {
- let collection = db.db().collection('documents');
- collection.insertOne(kvpairs, () => { });
- db.close();
- });
+ if (this.db) {
+ let collection = this.db.collection('documents');
+ collection.insertOne(kvpairs, (err: any, res: any) => {
+ if (err) {
+ // console.log(err)
+ return
+ }
+ });
+ }
}
- public getDocument(id: String): string | undefined {
+ public getDocument(id: string, fn: (res: any) => void) {
var result: JSON;
- this.MongoClient.connect(this.url, (err, db) => {
- if (err) {
- console.log(err)
- return undefined
- }
- let collection = db.db().collection('documents');
- collection.findOne({ _id: id }, (err: any, res: any) => result = res)
- console.log(result)
- db.close();
- if (!result) {
- console.log("not found")
- return undefined
- }
- console.log("found")
- return result;
- });
- return undefined
+ if (this.db) {
+ let collection = this.db.collection('documents');
+ collection.findOne({ _id: id }, (err: any, res: any) => {
+ result = res
+ if (!result) {
+ fn(undefined)
+ }
+ fn(result)
+ })
+ };
+ }
+
+ public getDocuments(ids: string[], fn: (res: any) => void) {
+ if (this.db) {
+ let collection = this.db.collection('documents');
+ let cursor = collection.find({ _id: { "$in": ids } })
+ cursor.toArray((err, docs) => {
+ fn(docs);
+ })
+ };
}
public print() {
diff --git a/src/server/index.js b/src/server/index.js
deleted file mode 100644
index 1ee6fbeef..000000000
--- a/src/server/index.js
+++ /dev/null
@@ -1,15 +0,0 @@
-"use strict";
-exports.__esModule = true;
-var express = require("express");
-var app = express();
-var port = 8080; // default port to listen
-import { Database } from './database';
-
-// define a route handler for the default home page
-app.get("/", function (req, res) {
- res.send("Hello world!");
-});
-// start the Express server
-app.listen(port, function () {
- console.log("server started at http://localhost:" + port);
-});
diff --git a/src/server/index.ts b/src/server/index.ts
index b81cdbcca..d05e1fca6 100644
--- a/src/server/index.ts
+++ b/src/server/index.ts
@@ -11,8 +11,11 @@ import { Socket } from 'socket.io';
import { Utils } from '../Utils';
import { ObservableMap } from 'mobx';
import { FIELD_ID, Field } from '../fields/Field';
-// import { Database } from './database';
+import { Database } from './database';
import { ServerUtils } from './ServerUtil';
+import { ObjectID } from 'mongodb';
+import { Document } from '../fields/Document';
+import * as io from 'socket.io'
import * as passportConfig from './authentication/config/passport';
import { getLogin, postLogin, getSignup, postSignup } from './authentication/controllers/user';
const config = require('../../webpack.config');
@@ -27,6 +30,7 @@ import c = require("crypto");
const MongoStore = require('connect-mongo')(session);
const mongoose = require('mongoose');
const bluebird = require('bluebird');
+import { performance } from 'perf_hooks'
const mongoUrl = 'mongodb://localhost:27017/Dash';
// mongoose.Promise = bluebird;
@@ -75,6 +79,11 @@ app.get("/hello", (req, res) => {
res.send("<p>Hello</p>");
})
+app.get("/delete", (req, res) => {
+ deleteAll();
+ res.redirect("/");
+});
+
app.use(wdm(compiler, {
publicPath: config.output.publicPath
}))
@@ -86,7 +95,7 @@ app.listen(port, () => {
console.log(`server started at http://localhost:${port}`);
})
-const server = require("socket.io")();
+const server = io();
interface Map {
[key: string]: Client;
}
@@ -98,10 +107,16 @@ server.on("connection", function (socket: Socket) {
Utils.Emit(socket, MessageStore.Foo, "handshooken")
Utils.AddServerHandler(socket, MessageStore.Bar, barReceived)
- // Utils.AddServerHandler(socket, MessageStore.SetField, setField)
- // Utils.AddServerHandlerCallback(socket, MessageStore.GetField, getField)
+ Utils.AddServerHandler(socket, MessageStore.SetField, (args) => setField(socket, args))
+ Utils.AddServerHandlerCallback(socket, MessageStore.GetField, getField)
+ Utils.AddServerHandlerCallback(socket, MessageStore.GetFields, getFields)
+ Utils.AddServerHandler(socket, MessageStore.DeleteAll, deleteAll)
})
+function deleteAll() {
+ Database.Instance.deleteAll();
+}
+
function barReceived(guid: String) {
clients[guid.toString()] = new Client(guid.toString());
// Database.Instance.print()
@@ -111,22 +126,24 @@ function addDocument(document: Document) {
}
-function setField(newValue: Transferable) {
- console.log(newValue._id)
- // if (Database.Instance.getDocument(newValue._id)) {
- // Database.Instance.update(newValue._id, newValue)
- // }
- // else {
- // Database.Instance.insert(newValue)
- // }
+function getField([id, callback]: [string, (result: any) => void]) {
+ Database.Instance.getDocument(id, (result: any) => {
+ if (result) {
+ callback(result)
+ }
+ else {
+ callback(undefined)
+ }
+ })
+}
+
+function getFields([ids, callback]: [string[], (result: any) => void]) {
+ Database.Instance.getDocuments(ids, callback);
}
-function getField([fieldRequest, callback]: [GetFieldArgs, (field: Field) => void]) {
- let fieldId: string = fieldRequest.field
- // let result: string | undefined = Database.Instance.getDocument(fieldId)
- // if (result) {
- // let fromJson: Field = ServerUtils.FromJson(result)
- // }
+function setField(socket: Socket, newValue: Transferable) {
+ Database.Instance.update(newValue._id, newValue)
+ socket.broadcast.emit(MessageStore.SetField.Message, newValue)
}
server.listen(serverPort);