Skip to content

Commit

Permalink
Basic status page
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Gebheim committed Dec 28, 2017
1 parent ed22bba commit 2c9bec8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 14 deletions.
13 changes: 6 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import * as _ from "lodash";
import { EthereumNodeEndpoints, FormattedEventLog } from "./types";
import { checkAugurDbSetup } from "./setup/check-augur-db-setup";
import { syncAugurNodeWithBlockchain } from "./blockchain/sync-augur-node-with-blockchain";
import { runWebsocketServer } from "./server/run-websocket-server";
import { runServer } from "./server/run-server";
import { ErrorCallback } from "./types";

// tslint:disable-next-line:no-var-requires
const {augurDbPath, ethereumNodeEndpoints, uploadBlockNumbers, websocketConfigs } = require("../config");
const {augurDbPath, ethereumNodeEndpoints, uploadBlockNumbers } = require("../config");

let db: Knex;
if (process.env.DATABASE_URL) {
Expand All @@ -38,23 +38,22 @@ const envEndpoints: EthereumNodeEndpoints = _.omitBy({

const configuredEndpoints: EthereumNodeEndpoints = _.isEmpty(envEndpoints) ? ethereumNodeEndpoints : envEndpoints;

const app = express();
const websocketServers: Array<WebSocket.Server> = runWebsocketServer(db, app, websocketConfigs);

const augur: Augur = new Augur();

augur.rpc.setDebugOptions({ broadcast: false });

const { app, servers } = runServer(db, augur);

checkAugurDbSetup(db, (err?: Error|null): void => {
if (err) {
console.error("checkAugurDbSetup:", err);
websocketServers.forEach((websocketServer) => websocketServer.close());
servers.forEach((websocketServer) => websocketServer.close());
process.exit(1);
}
syncAugurNodeWithBlockchain(db, augur, configuredEndpoints, uploadBlockNumbers, (err?: Error|null): void => {
if (err) {
console.error("syncAugurNodeWithBlockchain:", err);
websocketServers.forEach((websocketServer) => websocketServer.close());
servers.forEach((websocketServer) => websocketServer.close());
process.exit(1);
}
console.log("Sync with blockchain complete.");
Expand Down
42 changes: 42 additions & 0 deletions src/server/run-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as express from "express";
import * as WebSocket from "ws";
import * as Knex from "knex";
import Augur from "augur.js";
import { runWebsocketServer } from "./run-websocket-server";
import { getMarkets } from "./getters/get-markets";
import { Address, MarketsRow } from "../types";

// tslint:disable-next-line:no-var-requires
const { websocketConfigs } = require("../../config");

export interface RunServerResult {
app: express.Application;
servers: Array<WebSocket.Server>
}

export function runServer(db: Knex, augur: Augur): RunServerResult {
const app: express.Application = express();

const servers: Array<WebSocket.Server> = runWebsocketServer(db, app, websocketConfigs);

app.get("/", (req, res) => {
res.send("Hello World");
});

app.get("/status", (req, res) => {
try {
const networkId: string = augur.rpc.getNetworkID();
const universe: Address = augur.contracts.addresses[networkId].Universe;

getMarkets(db, universe, undefined, undefined, undefined, undefined, (err: Error|null, result?: any): void => {
if (result.length === 0) throw new Error("No markets exist");

res.send( { status: "up", universe } );
});
} catch(e) {
res.send({status: "down", reason: e});
}
});

return { app, servers };
}
19 changes: 12 additions & 7 deletions src/server/run-websocket-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import { makeJsonRpcError, JsonRpcErrorCode } from "./make-json-rpc-error";
import { Subscriptions } from "./subscriptions";
import * as fs from "fs";
import * as https from "https";
import * as http from "http";

export function runWebsocketServer(db: Knex, app: express.Application, webSocketConfigs: WebSocketConfigs): Array<WebSocket.Server> {

const websocketServers: Array<WebSocket.Server> = [];
const servers: Array<WebSocket.Server> = [];

if ( webSocketConfigs.wss != null ) {
console.log("Starting websocket secure server on port", webSocketConfigs.wss.port);
Expand All @@ -24,15 +25,18 @@ export function runWebsocketServer(db: Knex, app: express.Application, webSocket
};
const server = https.createServer(httpsOptions, app);
server.listen(webSocketConfigs.wss.port);
websocketServers.push( new WebSocket.Server({ server }) );
servers.push( new WebSocket.Server({ server }) );
}

if ( webSocketConfigs.ws != null ) {
console.log("Starting websocket server on port", webSocketConfigs.ws.port);
websocketServers.push( new WebSocket.Server({ port: webSocketConfigs.ws.port }) );
const server = http.createServer(app);
server.listen(webSocketConfigs.ws.port);
servers.push( new WebSocket.Server({ server }) );
}

websocketServers.forEach((websocketServer) => {
websocketServer.on("connection", (websocket: WebSocket): void => {
servers.forEach((server) => {
server.on("connection", (websocket: WebSocket): void => {
const subscriptions = new Subscriptions(augurEmitter);

websocket.on("message", (data: WebSocket.Data): void => {
Expand Down Expand Up @@ -80,10 +84,11 @@ export function runWebsocketServer(db: Knex, app: express.Application, webSocket
});
});

websocketServer.on("error", (err: Error): void => {
server.on("error", (err: Error): void => {
console.log("websocket error:", err);
// TODO reconnect
});
});
return websocketServers;

return servers;
}

0 comments on commit 2c9bec8

Please sign in to comment.