diff --git a/package.json b/package.json index 778ff00..d4e0112 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "1.0.0", "private": true, "description": "A frontend for mqtt based IoT sprinklers systems", - "main": "dist/index.js", + "main": "dist/bin/server.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "clean": "rm -rf ./dist ./build ./public", @@ -14,8 +14,8 @@ "watch:server": "yarn build:server --watch", "start:dev-server": "NODE_ENV=development webpack-dev-server --config ./client/webpack.config.js --env dev", "start": "NODE_ENV=development node .", - "start:pretty": "yarn start | node dist/logging/prettyPrint.js", - "start:nodemon": "nodemon --delay 0.5 --exec \"env NODE_ENV=development node . | node dist/logging/prettyPrint.js\"", + "start:pretty": "yarn start | node dist/bin/prettyPrint.js", + "start:nodemon": "nodemon --delay 0.5 --exec \"env NODE_ENV=development node . | node dist/bin/prettyPrint.js\"", "start:watch": "run-p watch:server start:nodemon", "start:dev": "run-p start:dev-server start:watch", "lint:client": "tslint --project client --force --format verbose", diff --git a/server/logging/prettyPrint.ts b/server/bin/prettyPrint.ts similarity index 100% rename from server/logging/prettyPrint.ts rename to server/bin/prettyPrint.ts diff --git a/server/bin/server.ts b/server/bin/server.ts new file mode 100644 index 0000000..982b2c3 --- /dev/null +++ b/server/bin/server.ts @@ -0,0 +1,27 @@ +import * as http from "http"; +import * as WebSocket from "ws"; + +import log from "@common/logger"; +import { createApp, ServerState, WebSocketApi } from "../"; + +const state = new ServerState(); +const app = createApp(state); +const webSocketApi = new WebSocketApi(state); + +const port = +(process.env.PORT || 8080); +const host = process.env.HOST || "0.0.0.0"; + +const server = new http.Server(app); +const webSocketServer = new WebSocket.Server({ server }); +webSocketApi.listen(webSocketServer); + +state + .start() + .then(() => { + server.listen(port, host, () => { + log.info(`listening at ${host}:${port}`); + }); + }) + .catch(err => { + log.error({ err }, "error starting server"); + }); diff --git a/server/index.ts b/server/index.ts index cc667f7..4c73b3d 100644 --- a/server/index.ts +++ b/server/index.ts @@ -4,32 +4,6 @@ import "./configureAlias"; import "./env"; import "./configureLogger"; -import log from "@common/logger"; -import { Server } from "http"; -import * as WebSocket from "ws"; - -import { ServerState } from "./state"; -import { createApp } from "./express"; -import { WebSocketApi } from "./sprinklersRpc/WebSocketApi"; - -const state = new ServerState(); -const app = createApp(state); -const webSocketApi = new WebSocketApi(state); - -const port = +(process.env.PORT || 8080); -const host = process.env.HOST || "0.0.0.0"; - -const server = new Server(app); -const webSocketServer = new WebSocket.Server({ server }); -webSocketApi.listen(webSocketServer); - -state - .start() - .then(() => { - server.listen(port, host, () => { - log.info(`listening at ${host}:${port}`); - }); - }) - .catch(err => { - log.error({ err }, "error starting server"); - }); +export { ServerState } from "./state"; +export { createApp } from "./express"; +export { WebSocketApi } from "./sprinklersRpc/"; diff --git a/server/sprinklersRpc/index.ts b/server/sprinklersRpc/index.ts new file mode 100644 index 0000000..0402ce7 --- /dev/null +++ b/server/sprinklersRpc/index.ts @@ -0,0 +1,2 @@ +export * from "./WebSocketApi"; +export * from "./WebSocketConnection";