From a698775388edfd1e07fad907f198a0771323cc3c Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Tue, 3 Oct 2017 13:07:05 -0600 Subject: [PATCH] Added request logger and made stuff better --- app/webpack.config.js | 5 +++ package.json | 7 +--- paths.js | 3 +- server/app/index.ts | 2 + server/app/logger.ts | 8 ++++ server/app/serveApp.ts | 25 ++---------- server/configureAlias.ts | 6 +++ server/configureLogger.ts | 6 +++ server/express-pino-logger.d.ts | 7 ++++ server/index.ts | 12 ++---- yarn.lock | 69 +++++++-------------------------- 11 files changed, 60 insertions(+), 90 deletions(-) create mode 100644 server/app/logger.ts create mode 100644 server/configureAlias.ts create mode 100644 server/configureLogger.ts create mode 100644 server/express-pino-logger.d.ts diff --git a/app/webpack.config.js b/app/webpack.config.js index 89d14e5..80f79f4 100644 --- a/app/webpack.config.js +++ b/app/webpack.config.js @@ -229,6 +229,11 @@ const getConfig = module.exports = (env) => { plugins: plugins, devServer: { hot: true, + port: 8081, + proxy: [{ + context: ["/api"], // TODO: update when there is actually an api + target: paths.publicUrl, + }], }, } }; diff --git a/package.json b/package.json index ce7d6f1..5b7ecd5 100644 --- a/package.json +++ b/package.json @@ -41,12 +41,12 @@ "@types/react-dom": "^15.5.0", "@types/react-fontawesome": "^1.5.0", "@types/react-hot-loader": "^3.0.4", - "@types/webpack-hot-middleware": "^2.16.0", "autoprefixer": "^7.1.4", "case-sensitive-paths-webpack-plugin": "^2.1.1", "classnames": "^2.2.5", "dotenv": "^4.0.0", "express": "^4.15.4", + "express-pino-logger": "^2.0.0", "extract-text-webpack-plugin": "^3.0.0", "font-awesome": "^4.7.0", "mobx": "^3.1.11", @@ -67,11 +67,9 @@ "semantic-ui-react": "^0.73.0", "tslint-loader": "^3.5.3", "uglifyjs-webpack-plugin": "^0.4.6", - "url-loader": "^0.5.9", - "webpack-hot-middleware": "^2.19.1" + "url-loader": "^0.5.9" }, "devDependencies": { - "@types/webpack-dev-middleware": "^1.9.3", "@types/webpack-env": "^1.13.0", "awesome-typescript-loader": "^3.1.3", "css-loader": "^0.28.4", @@ -87,7 +85,6 @@ "tslint-react": "^3.2.0", "typescript": "^2.3.4", "webpack": "^3.0.0", - "webpack-dev-middleware": "^1.12.0", "webpack-dev-server": "^2.4.4" } } diff --git a/paths.js b/paths.js index 18640f4..31b1ba3 100644 --- a/paths.js +++ b/paths.js @@ -20,7 +20,7 @@ function ensureSlash(path, needsSlash) { exports.dotenv = resolveRoot(".env"); exports.nodeModulesDir = resolveRoot("node_modules"); exports.packageJson = resolveRoot("package.json"); -exports.publicUrl = ensureSlash(process.env.PUBLIC_URL || "/", true); +exports.publicUrl = ensureSlash(process.env.PUBLIC_URL || "http://localhost:8080/", true); exports.publicPath = ensureSlash(url.parse(exports.publicUrl).pathname || "/", true); exports.commonDir = resolveRoot("common"); @@ -31,6 +31,7 @@ exports.appHtml = path.resolve(exports.appDir, "index.html"); exports.appWebpackConfig = path.resolve(exports.appDir, "webpack.config.js"); exports.appTsConfig = path.resolve(exports.appDir, "tsconfig.json"); exports.appBuildDir = resolveRoot("public"); +exports.publicDir = exports.appBuildDir; exports.serverDir = resolveRoot("server"); exports.serverBuildDir = resolveRoot("dist"); diff --git a/server/app/index.ts b/server/app/index.ts index 458997c..eb9f994 100644 --- a/server/app/index.ts +++ b/server/app/index.ts @@ -1,9 +1,11 @@ import * as express from "express"; +import logger from "./logger"; import serveApp from "./serveApp"; const app = express(); +app.use(logger); serveApp(app); export default app; diff --git a/server/app/logger.ts b/server/app/logger.ts new file mode 100644 index 0000000..5f69445 --- /dev/null +++ b/server/app/logger.ts @@ -0,0 +1,8 @@ +import log from "@common/logger"; +import expressPinoLogger = require("express-pino-logger"); +import * as pino from "pino"; + +const l = pino(); +pino(l); + +export default expressPinoLogger(log); diff --git a/server/app/serveApp.ts b/server/app/serveApp.ts index f5243b4..e4c3ae0 100644 --- a/server/app/serveApp.ts +++ b/server/app/serveApp.ts @@ -1,28 +1,9 @@ import { Express } from "express"; -import * as paths from "paths"; -import * as webpack from "webpack"; -import * as webpackMiddleware from "webpack-dev-middleware"; -import * as webpackHotMiddleware from "webpack-hot-middleware"; +import * as serveStatic from "serve-static"; import logger from "@common/logger"; -const log = logger.child({ source: "webpack" }); - -/* tslint:disable-next-line:no-var-requires */ -const webpackConfig = require(paths.appWebpackConfig)("dev"); +import * as paths from "paths"; export default function serveApp(app: Express) { - const compiler = webpack(webpackConfig); - app.use(webpackMiddleware(compiler, - { - noInfo: true, - publicPath: webpackConfig.output.publicPath, - log: log.info.bind(log), - warn: log.warn.bind(log), - error: log.error.bind(log), - }, - )); - app.use(webpackHotMiddleware(compiler, - { - log: log.info.bind(log), - })); + app.use(serveStatic(paths.appBuildDir)); } diff --git a/server/configureAlias.ts b/server/configureAlias.ts new file mode 100644 index 0000000..45250a6 --- /dev/null +++ b/server/configureAlias.ts @@ -0,0 +1,6 @@ +import * as moduleAlias from "module-alias"; +import * as path from "path"; +moduleAlias.addAlias("@common", path.resolve(__dirname, "..", "common")); +moduleAlias.addAlias("@server", __dirname); +moduleAlias.addAlias("env", require.resolve("../env")); +moduleAlias.addAlias("paths", require.resolve("../paths")); diff --git a/server/configureLogger.ts b/server/configureLogger.ts new file mode 100644 index 0000000..6de12be --- /dev/null +++ b/server/configureLogger.ts @@ -0,0 +1,6 @@ +import log, { setLogger } from "@common/logger"; +setLogger(log.child({ + name: "sprinklers3/server", + level: "trace", +})); + diff --git a/server/express-pino-logger.d.ts b/server/express-pino-logger.d.ts new file mode 100644 index 0000000..7cc0574 --- /dev/null +++ b/server/express-pino-logger.d.ts @@ -0,0 +1,7 @@ +declare module "express-pino-logger" { + import { Logger } from "pino"; + import { ErrorRequestHandler } from "express"; + + function makeLogger(logger: Logger): ErrorRequestHandler; + export = makeLogger; +} \ No newline at end of file diff --git a/server/index.ts b/server/index.ts index 830a62a..1c56a39 100644 --- a/server/index.ts +++ b/server/index.ts @@ -1,16 +1,12 @@ -import * as moduleAlias from "module-alias"; -import * as path from "path"; -moduleAlias.addAlias("@common", path.resolve(__dirname, "..", "common")); -moduleAlias.addAlias("@server", __dirname); -moduleAlias.addAlias("paths", require.resolve("../paths")); +import "./configureAlias"; -import log, { setLogger } from "@common/logger"; +import "env"; + +import log from "@common/logger"; import * as mqtt from "@common/mqtt"; import { Server } from "http"; import app from "./app"; -setLogger(log.child({ name: "sprinklers3/server", level: "trace" })); - const mqttClient = new mqtt.MqttApiClient("mqtt://localhost:1882"); mqttClient.start(); diff --git a/yarn.lock b/yarn.lock index 82a8f11..b16db2b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10,12 +10,6 @@ version "2.2.3" resolved "https://registry.yarnpkg.com/@types/classnames/-/classnames-2.2.3.tgz#3f0ff6873da793870e20a260cada55982f38a9e5" -"@types/connect@*": - version "3.4.31" - resolved "https://registry.yarnpkg.com/@types/connect/-/connect-3.4.31.tgz#1f92d6b117ecc05076c49ecd024f7976e528bad9" - dependencies: - "@types/node" "*" - "@types/core-js@^0.9.43": version "0.9.43" resolved "https://registry.yarnpkg.com/@types/core-js/-/core-js-0.9.43.tgz#65d646c5e8c0cd1bdee37065799f9d3d48748253" @@ -94,46 +88,10 @@ "@types/express-serve-static-core" "*" "@types/mime" "*" -"@types/source-map@*": - version "0.5.1" - resolved "https://registry.yarnpkg.com/@types/source-map/-/source-map-0.5.1.tgz#7e74db5d06ab373a712356eebfaea2fad0ea2367" - -"@types/tapable@*": - version "0.2.4" - resolved "https://registry.yarnpkg.com/@types/tapable/-/tapable-0.2.4.tgz#8181a228da46185439300e600c5ae3b3b3982585" - -"@types/uglify-js@*": - version "2.6.29" - resolved "https://registry.yarnpkg.com/@types/uglify-js/-/uglify-js-2.6.29.tgz#521347f69e20201d218f5991ae66e10878afcf1a" - dependencies: - "@types/source-map" "*" - -"@types/webpack-dev-middleware@^1.9.3": - version "1.9.3" - resolved "https://registry.yarnpkg.com/@types/webpack-dev-middleware/-/webpack-dev-middleware-1.9.3.tgz#2120f79a4fbb0dc49ddba97e4b0b778f947c7c5b" - dependencies: - "@types/connect" "*" - "@types/webpack" "*" - "@types/webpack-env@^1.13.0": version "1.13.1" resolved "https://registry.yarnpkg.com/@types/webpack-env/-/webpack-env-1.13.1.tgz#b45c222e24301bd006e3edfc762cc6b51bda236a" -"@types/webpack-hot-middleware@^2.16.0": - version "2.16.0" - resolved "https://registry.yarnpkg.com/@types/webpack-hot-middleware/-/webpack-hot-middleware-2.16.0.tgz#4fe6b0bc077b43d4341c0c5b212d9198ab8b79a3" - dependencies: - "@types/connect" "*" - "@types/webpack" "*" - -"@types/webpack@*": - version "3.0.10" - resolved "https://registry.yarnpkg.com/@types/webpack/-/webpack-3.0.10.tgz#1d27db07df32109f8c882535b547aae4252fd53e" - dependencies: - "@types/node" "*" - "@types/tapable" "*" - "@types/uglify-js" "*" - abbrev@1: version "1.1.0" resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.0.tgz#d0554c2256636e2f56e7c2e5ad183f859428d81f" @@ -1693,6 +1651,12 @@ expand-tilde@^2.0.0, expand-tilde@^2.0.2: dependencies: homedir-polyfill "^1.0.1" +express-pino-logger@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/express-pino-logger/-/express-pino-logger-2.0.0.tgz#e2b1046a8e9a9fe65d4e7fcbe57115e28df076a1" + dependencies: + pino-http "^2.0.0" + express@^4.13.3, express@^4.15.4: version "4.15.4" resolved "https://registry.yarnpkg.com/express/-/express-4.15.4.tgz#032e2253489cf8fce02666beca3d11ed7a2daed1" @@ -3574,7 +3538,13 @@ pinkie@^2.0.0: version "2.0.4" resolved "https://registry.yarnpkg.com/pinkie/-/pinkie-2.0.4.tgz#72556b80cfa0d48a974e80e77248e80ed4f7f870" -pino@^4.7.2: +pino-http@^2.0.0: + version "2.6.2" + resolved "https://registry.yarnpkg.com/pino-http/-/pino-http-2.6.2.tgz#aedb71b08d15bc69d959870bafa6b7780103274f" + dependencies: + pino "^4.0.2" + +pino@^4.0.2, pino@^4.7.2: version "4.7.2" resolved "https://registry.yarnpkg.com/pino/-/pino-4.7.2.tgz#56e8e712637669095f0a61b27a15649d62dec8cd" dependencies: @@ -4021,7 +3991,7 @@ querystring-es3@^0.2.0: version "0.2.1" resolved "https://registry.yarnpkg.com/querystring-es3/-/querystring-es3-0.2.1.tgz#9ec61f79049875707d69414596fd907a4d711e73" -querystring@0.2.0, querystring@^0.2.0: +querystring@0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.0.tgz#b209849203bb25df820da756e747005878521620" @@ -5337,7 +5307,7 @@ wbuf@^1.1.0, wbuf@^1.7.2: dependencies: minimalistic-assert "^1.0.0" -webpack-dev-middleware@^1.11.0, webpack-dev-middleware@^1.12.0: +webpack-dev-middleware@^1.11.0: version "1.12.0" resolved "https://registry.yarnpkg.com/webpack-dev-middleware/-/webpack-dev-middleware-1.12.0.tgz#d34efefb2edda7e1d3b5dbe07289513219651709" dependencies: @@ -5376,15 +5346,6 @@ webpack-dev-server@^2.4.4: webpack-dev-middleware "^1.11.0" yargs "^6.6.0" -webpack-hot-middleware@^2.19.1: - version "2.19.1" - resolved "https://registry.yarnpkg.com/webpack-hot-middleware/-/webpack-hot-middleware-2.19.1.tgz#5db32c31c955c1ead114d37c7519ea554da0d405" - dependencies: - ansi-html "0.0.7" - html-entities "^1.2.0" - querystring "^0.2.0" - strip-ansi "^3.0.0" - webpack-sources@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/webpack-sources/-/webpack-sources-1.0.1.tgz#c7356436a4d13123be2e2426a05d1dad9cbe65cf"