Skip to content
This repository has been archived by the owner on Sep 28, 2018. It is now read-only.

Commit

Permalink
Merge pull request #22 from vgno/feature/better-path-managment
Browse files Browse the repository at this point in the history
Mainly updates how paths in Koa are handled
  • Loading branch information
dlmr committed Jan 21, 2016
2 parents 26b7bd3 + b94c5d4 commit 431b60a
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
lib
docs
esdocs
examples
coverage
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ node_modules
*.log
.DS_Store
lib
docs
esdocs
build
coverage
95 changes: 95 additions & 0 deletions docs/Settings.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion esdoc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"source": "./src",
"destination": "./docs",
"destination": "./esdocs",
"excludes": ["module\/entry.js$"],
"plugins": [{
"name": "esdoc-es7-plugin"
Expand Down
1 change: 1 addition & 0 deletions examples/client-server/files/test/path/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
World
2 changes: 1 addition & 1 deletion examples/client-server/roc.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
settings: {
runtime: {
port: 8080,
serve: 'files',
serve: ['files', 'build/client'],
favicon: 'files/roc.png'
},
build: {
Expand Down
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
"scripts": {
"build": "babel src/roc --out-dir lib --source-maps",
"build:watch": "npm run build -- --watch",
"clean": "rimraf lib docs",
"clean": "rimraf lib esdocs coverage",
"test": "npm run lint && npm run test:coverage",
"test:unit": "mocha --compilers js:babel/register --recursive test/",
"test:unit:watch": "npm run test:unit -- --watch",
"test:coverage": "babel-node ./node_modules/.bin/isparta cover _mocha -- --recursive test/",
"lint": "eslint .",
"check": "ncu",
"docs": "esdoc -c esdoc.json",
"prepublish": "npm run lint && npm run clean && npm run build && npm run docs && npm run test:unit"
"esdocs": "esdoc -c esdoc.json",
"prepublish": "npm run lint && npm run clean && npm run build && npm run esdocs && npm run test:unit"
},
"keywords": [
"roc"
Expand Down Expand Up @@ -58,13 +58,19 @@
"json-loader": "~0.5.3",
"koa": "~1.1.1",
"koa-accesslog": "~0.0.2",
"koa-add-trailing-slashes": "~1.1.0",
"koa-compressor": "~1.0.3",
"koa-conditional-get": "~1.0.3",
"koa-errors": "~1.0.1",
"koa-etag": "~2.0.0",
"koa-favicon": "~1.2.0",
"koa-helmet": "~0.3.0",
"koa-logger": "~1.3.0",
"koa-static": "~1.5.1",
"koa-lowercase-path": "~1.0.0",
"koa-mount": "~1.3.0",
"koa-normalize-path": "~1.0.0",
"koa-remove-trailing-slashes": "~1.0.0",
"koa-static": "~2.0.0",
"koa-webpack-dev-middleware": "~1.0.1",
"koa-webpack-hot-middleware": "~1.0.1",
"lodash.isfunction": "~3.0.6",
Expand Down
33 changes: 29 additions & 4 deletions src/app/server/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
/* global USE_DEFAULT_KOA_MIDDLEWARES HAS_KOA_MIDDLEWARES KOA_MIDDLEWARES */
/* global USE_DEFAULT_KOA_MIDDLEWARES HAS_KOA_MIDDLEWARES KOA_MIDDLEWARES ROC_PATH */

import debug from 'debug';
import koa from 'koa';
import serve from 'koa-static';
import mount from 'koa-mount';

import addTrailingSlash from 'koa-add-trailing-slashes';
import normalizePath from 'koa-normalize-path';
import lowercasePath from 'koa-lowercase-path';
import removeTrailingSlash from 'koa-remove-trailing-slashes';

import { merge, getSettings } from 'roc';

Expand Down Expand Up @@ -44,22 +50,41 @@ export default function createServer(options = {}, beforeUserMiddlewares = []) {
const makeServe = (toServe = []) => {
toServe = [].concat(toServe);
for (const path of toServe) {
server.use(serve(path));
// We use defer here to no try to fetch a file if we have set something on body, something that is done in
// redirect. This because https://github.com/koajs/send/issues/51
server.use(serve(path, { defer: true }));
}
};

if (settings.koa.normalize.enabled) {
server.use(normalizePath({ defer: settings.koa.normalize.defer }));
}

if (settings.koa.lowercase.enabled) {
server.use(lowercasePath({ defer: settings.koa.lowercase.defer }));
}

if (settings.koa.trailingSlashes.enabled === true) {
server.use(addTrailingSlash({ defer: settings.koa.trailingSlashes.defer }));
} else if (settings.koa.trailingSlashes.enabled === false) {
server.use(removeTrailingSlash());
}

// Serve folders
makeServe(settings.serve);

function start(port) {
port = port || process.env.PORT || settings.port;
server.listen(port);

const app = koa();
app.use(mount(ROC_PATH, server));
app.listen(port);

if (process.send) {
process.send('online');
}

debug('roc:server')(`Server started on port ${port}`);
debug('roc:server')(`Server started on port ${port} and served from ${ROC_PATH}`);
}

return {
Expand Down
7 changes: 6 additions & 1 deletion src/app/server/middlewares.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* global __DIST__ */
/* global __DEV__ __DIST__ */

import koaErrors from 'koa-errors';
import helmet from 'koa-helmet';
import koaEtag from 'koa-etag';
import koaCompressor from 'koa-compressor';
Expand All @@ -16,6 +17,10 @@ import koaLogger from 'koa-logger';
export default function middlewares(config) {
const middlewaresList = [];

if (__DEV__) {
middlewaresList.push(koaErrors());
}

// Security headers
middlewaresList.push(helmet());

Expand Down
16 changes: 15 additions & 1 deletion src/roc/config/roc.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,21 @@ const config = {
'build/client'
],
favicon: '',
startBundle: ''
startBundle: '',
koa: {
lowercase: {
enabled: true,
defer: true
},
normalize: {
enabled: true,
defer: false
},
trailingSlashes: {
enabled: true,
defer: true
}
}
},

dev: {
Expand Down
37 changes: 35 additions & 2 deletions src/roc/config/roc.config.meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import {

const configMeta = {
settings: {
groups: {
runtime: {
koa: 'Settings for how Koa should handle paths'
}
},
descriptions: {
runtime: {
port: 'Port for the server to use',
Expand All @@ -20,7 +25,21 @@ const configMeta = {
},
serve: 'What folder the server should expose',
favicon: 'Path to the favicon file, specially handled on the server',
startBundle: 'Relative path to a bundle to start when calling "start", will not be needed in most cases'
startBundle: 'Relative path to a bundle to start when calling "start", is not needed in most cases',
koa: {
lowercase: {
enabled: 'If paths should be transformed to lowercase',
defer: 'If this should be performed after looking for a file on disk'
},
normalize: {
enabled: 'If paths should be normalized, that is remove extra slashes',
defer: 'If this should be performed after looking for a file on disk'
},
trailingSlashes: {
enabled: 'Set to true to enforce trailing slashes, false to remove them and null for no rule',
defer: 'If this should be performed after looking for a file on disk'
}
}
},

dev: {
Expand Down Expand Up @@ -74,7 +93,21 @@ const configMeta = {
},
serve: isArrayOrSingle(isPath),
favicon: isString,
startBundle: isPath
startBundle: isPath,
koa: {
lowercase: {
enabled: isBoolean,
defer: isBoolean
},
normalize: {
enabled: isBoolean,
defer: isBoolean
},
trailingSlashes: {
enabled: isBoolean,
defer: isBoolean
}
}
},

dev: {
Expand Down
4 changes: 3 additions & 1 deletion src/roc/runtime/watch-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ export default function watchServer(compiler) {
};

const initBrowsersync = () => {
const basePath = getSettings('build').path;
browserSync({
port: parseInt(getDevPort(), 10) + 1,
proxy: `0.0.0.0:${getPort()}`,
// This proxy will remove extra slashes from the path, important to note
proxy: `0.0.0.0:${getPort()}${basePath}`,
snippetOptions: {
rule: {
match: /<\/body>/i,
Expand Down

0 comments on commit 431b60a

Please sign in to comment.