From 6f6bedb07acfb0ec402366eb9c64dbfb284da6b2 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Mon, 16 Jul 2018 13:26:41 -0600 Subject: [PATCH] feat: production updates --- config/paths.js | 2 +- config/webpack.config.prod.js | 10 +++++----- src/core/server/app/handlers/auth/local.ts | 3 ++- src/core/server/app/index.ts | 8 +++++--- src/core/server/app/middleware/notFound.ts | 5 +++++ src/core/server/app/router.ts | 14 +++++++++----- src/core/server/config.ts | 5 ----- src/index.ts | 6 ++++++ 8 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 src/core/server/app/middleware/notFound.ts diff --git a/config/paths.js b/config/paths.js index 7d98ac998..73cc43370 100644 --- a/config/paths.js +++ b/config/paths.js @@ -46,7 +46,7 @@ module.exports = { appPostCssConfig: resolveApp("config/postcss.config.js"), appJestConfig: resolveApp("config/jest.config.js"), appLoaders: resolveApp("loaders"), - appDist: resolveApp("dist"), + appDist: resolveApp("dist/static"), appPublic: resolveApp("public"), appPackageJson: resolveApp("package.json"), appSrc: resolveApp("src"), diff --git a/config/webpack.config.prod.js b/config/webpack.config.prod.js index 4980d73be..d1ad40d3b 100644 --- a/config/webpack.config.prod.js +++ b/config/webpack.config.prod.js @@ -39,7 +39,7 @@ if (env.stringified["process.env"].NODE_ENV !== '"production"') { // because of this bug https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/763. // TODO: Repalce with mini-css-extract-plugin once it supports HMR. // https://github.com/webpack-contrib/mini-css-extract-plugin -const cssFilename = "static/css/[name].[md5:contenthash:hex:20].css"; +const cssFilename = "assets/css/[name].[md5:contenthash:hex:20].css"; // ExtractTextPlugin expects the build output to be flat. // (See https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/27) @@ -72,8 +72,8 @@ module.exports = { // Generated JS file names (with nested folders). // There will be one main bundle, and one file per asynchronous chunk. // We don't currently advertise code splitting but Webpack supports it. - filename: "static/js/[name].[chunkhash:8].js", - chunkFilename: "static/js/[name].[chunkhash:8].chunk.js", + filename: "assets/js/[name].[chunkhash:8].js", + chunkFilename: "assets/js/[name].[chunkhash:8].chunk.js", // We inferred the "public path" (such as / or /my-project) from homepage. publicPath: publicPath, // Point sourcemap entries to original disk location (format as URL on Windows) @@ -210,7 +210,7 @@ module.exports = { loader: require.resolve("url-loader"), options: { limit: 10000, - name: "static/media/[name].[hash:8].[ext]", + name: "assets/media/[name].[hash:8].[ext]", }, }, // Process JS with Babel. @@ -299,7 +299,7 @@ module.exports = { exclude: [/\.(js|jsx|mjs|ts|tsx)$/, /\.html$/, /\.json$/], loader: require.resolve("file-loader"), options: { - name: "static/media/[name].[hash:8].[ext]", + name: "assets/media/[name].[hash:8].[ext]", }, }, ], diff --git a/src/core/server/app/handlers/auth/local.ts b/src/core/server/app/handlers/auth/local.ts index a5e483d08..b1cfd11be 100644 --- a/src/core/server/app/handlers/auth/local.ts +++ b/src/core/server/app/handlers/auth/local.ts @@ -31,7 +31,7 @@ export interface SignupOptions { db: Db; } -export const signup = ({ db }: SignupOptions): RequestHandler => async ( +export const signupHandler = ({ db }: SignupOptions): RequestHandler => async ( req: Request, res, next @@ -42,6 +42,7 @@ export const signup = ({ db }: SignupOptions): RequestHandler => async ( // Tenant is guaranteed at this point. const tenant = req.tenant!; + // Check to ensure that the local integration has been enabled. if (!tenant.auth.integrations.local.enabled) { // TODO: replace with better error. return next(new Error("integration is disabled")); diff --git a/src/core/server/app/index.ts b/src/core/server/app/index.ts index 0fefb561f..c5dbd81a1 100644 --- a/src/core/server/app/index.ts +++ b/src/core/server/app/index.ts @@ -3,6 +3,7 @@ import http from "http"; import { Redis } from "ioredis"; import { Db } from "mongodb"; +import { notFoundMiddleware } from "talk-server/app/middleware/notFound"; import { createPassport } from "talk-server/app/middleware/passport"; import { Config } from "talk-server/config"; import { handleSubscriptions } from "talk-server/graph/common/subscriptions/middleware"; @@ -30,16 +31,17 @@ export async function createApp(options: AppOptions): Promise { // Logging parent.use(accessLogger); - // Static Files - parent.use(serveStatic); - // Create some services for the router. const passport = createPassport({ db: options.mongo }); // Mount the router. parent.use(await createRouter(options, { passport })); + // Static Files + parent.use(serveStatic); + // Error Handling + parent.use(notFoundMiddleware); parent.use(errorLogger); return parent; diff --git a/src/core/server/app/middleware/notFound.ts b/src/core/server/app/middleware/notFound.ts new file mode 100644 index 000000000..1d5dbde5c --- /dev/null +++ b/src/core/server/app/middleware/notFound.ts @@ -0,0 +1,5 @@ +import { RequestHandler } from "express"; + +export const notFoundMiddleware: RequestHandler = (req, res, next) => { + next(new Error("not found")); +}; diff --git a/src/core/server/app/router.ts b/src/core/server/app/router.ts index 1011e532f..ad862d574 100644 --- a/src/core/server/app/router.ts +++ b/src/core/server/app/router.ts @@ -1,14 +1,14 @@ import express from "express"; import passport from "passport"; +import { signupHandler } from "talk-server/app/handlers/auth/local"; +import { apiErrorHandler } from "talk-server/app/middleware/error"; +import { errorLogger } from "talk-server/app/middleware/logging"; +import { authenticate } from "talk-server/app/middleware/passport"; import tenantMiddleware from "talk-server/app/middleware/tenant"; import managementGraphMiddleware from "talk-server/graph/management/middleware"; import tenantGraphMiddleware from "talk-server/graph/tenant/middleware"; -import { signup } from "talk-server/app/handlers/auth/local"; -import { apiErrorHandler } from "talk-server/app/middleware/error"; -import { errorLogger } from "talk-server/app/middleware/logging"; -import { authenticate } from "talk-server/app/middleware/passport"; import { AppOptions } from "./index"; import playground from "./middleware/playground"; @@ -59,7 +59,11 @@ function createNewAuthRouter(app: AppOptions, options: RouterOptions) { express.json(), authenticate(options.passport, "local") ); - router.post("/local/signup", express.json(), signup({ db: app.mongo })); + router.post( + "/local/signup", + express.json(), + signupHandler({ db: app.mongo }) + ); router.get("/oidc", authenticate(options.passport, "oidc")); router.get("/oidc/callback", authenticate(options.passport, "oidc")); // router.get("/google", options.passport.authenticate("google")); diff --git a/src/core/server/config.ts b/src/core/server/config.ts index f82bb46b8..1954de907 100644 --- a/src/core/server/config.ts +++ b/src/core/server/config.ts @@ -1,11 +1,6 @@ import convict from "convict"; -import dotenv from "dotenv"; import Joi from "joi"; -// Apply all the configuration provided in the .env file if it isn't already in -// the environment. -dotenv.config(); - // Add custom format for the mongo uri scheme. convict.addFormat({ name: "mongo-uri", diff --git a/src/index.ts b/src/index.ts index ec3479156..985b3476e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,9 @@ +import dotenv from "dotenv"; + +// Apply all the configuration provided in the .env file if it isn't already in +// the environment. +dotenv.config(); + import express from "express"; import logger from "talk-server/logger";