Commit e077e8e4d717d2c79b41f7287c20906ac86841db
restructure the server stack, boil some more plates
Michael Williams committed on 11/28/2015, 9:16:51 AMParent: 0a19ff71e90207bdc5e6b22d5470c11617f9f7b0
Files changed
app/client.js | changed |
app/containers/root.js | changed |
app/containers/no-match.js | added |
app/routes.js | changed |
app/server.js | changed |
app/services/static.js | deleted |
app/store.js | changed |
app/assets/index.html | added |
app/stack/index.js | added |
app/stack/services.js | added |
app/stack/static.js | added |
config/defaults.js | changed |
package.json | changed |
server.js | changed |
app/client.js | ||
---|---|---|
@@ -15,9 +15,8 @@ | ||
15 | 15 | ) |
16 | 16 | |
17 | 17 | //store.dispatch(getAllTodos()) |
18 | 18 | |
19 | -const main = document.createElement('main') | |
20 | - | |
21 | -document.body.appendChild(main) | |
22 | - | |
23 | -render(<Root />, main) | |
19 | +render( | |
20 | + <Root />, | |
21 | + document.querySelector('main') | |
22 | +) |
app/containers/root.js | ||
---|---|---|
@@ -4,9 +4,9 @@ | ||
4 | 4 | |
5 | 5 | const routes = require('app/routes') |
6 | 6 | |
7 | 7 | if (process.env.NODE_ENV === 'development') { |
8 | - const DevTools = require('app/containers/dev-tools') | |
8 | + var DevTools = require('app/containers/dev-tools') | |
9 | 9 | } |
10 | 10 | |
11 | 11 | class Root extends React.Component { |
12 | 12 | render() { |
app/containers/no-match.js | ||
---|---|---|
@@ -1,0 +1,15 @@ | ||
1 | +const React = require('react') | |
2 | +const { connect } = require('react-redux') | |
3 | + | |
4 | +class NoMatch extends React.Component { | |
5 | + render () { | |
6 | + return <div> | |
7 | + no match! | |
8 | + </div> | |
9 | + } | |
10 | +} | |
11 | + | |
12 | +module.exports = connect( | |
13 | + NoMatch, | |
14 | + (state) => { return {} } | |
15 | +) |
app/routes.js | ||
---|---|---|
@@ -1,12 +1,15 @@ | ||
1 | +const React = require('react') | |
1 | 2 | const { Route } = require('react-router') |
2 | 3 | |
3 | 4 | const App = require('app/containers/app') |
4 | 5 | const TodoList = require('app/containers/todo-list') |
6 | +const NoMatch = require('app/containers/no-match') | |
5 | 7 | |
6 | 8 | const routes = ( |
7 | 9 | <Route path='/' component={App}> |
8 | 10 | <Route path='todos' component={TodoList} /> |
11 | + <Route path="*" component={NoMatch} /> | |
9 | 12 | </Route> |
10 | 13 | ) |
11 | 14 | |
12 | 15 | module.exports = routes |
app/server.js | ||
---|---|---|
@@ -1,29 +1,13 @@ | ||
1 | -const feathers = require('feathers') | |
2 | 1 | const { mapObjIndexed, reduce, toPairs } = require('ramda') |
3 | 2 | |
4 | -const serviceCreators = require('./services') | |
3 | +const createStack = require('app/stack') | |
5 | 4 | |
6 | 5 | module.exports = createServer |
7 | 6 | |
8 | -const useAll = function (app, services) { | |
9 | - return reduce((app, [name, service]) => { | |
10 | - return app.use(`/${name}`, service) | |
11 | - }, app, toPairs(services)) | |
12 | -} | |
13 | - | |
14 | 7 | function createServer (config) { |
8 | + const server = createStack(config) | |
15 | 9 | |
16 | - const services = mapObjIndexed( | |
17 | - (serviceCreator, name) => { | |
18 | - return serviceCreator(config.services[name]) | |
19 | - }, | |
20 | - serviceCreators | |
21 | - ) | |
10 | + server.listen(config.port) | |
22 | 11 | |
23 | - const server = feathers() | |
24 | - | |
25 | - useAll(server, services) | |
26 | - | |
27 | 12 | return server |
28 | 13 | } |
29 | - |
app/services/static.js | ||
---|---|---|
@@ -1,8 +1,0 @@ | ||
1 | -const serveStatic = require('serve-static') | |
2 | -const { join } = require('path') | |
3 | - | |
4 | -module.exports = createStaticService | |
5 | - | |
6 | -function createStaticService (config) { | |
7 | - return serveStatic(config.root, config) | |
8 | -} |
app/store.js | ||
---|---|---|
@@ -12,15 +12,17 @@ | ||
12 | 12 | |
13 | 13 | middleware.push(thunk) |
14 | 14 | |
15 | 15 | if (process.env.NODE_ENV === 'development') { |
16 | - const { persistState } = require('redux-devtools') | |
16 | + var { persistState } = require('redux-devtools') | |
17 | + | |
18 | + var DevTools = require('app/containers/dev-tools') | |
17 | 19 | |
18 | 20 | middleware.push(logger()) |
19 | 21 | } |
20 | 22 | |
21 | 23 | storesEnhancers.push( |
22 | - applyMiddleware(middleware) | |
24 | + applyMiddleware(...middleware) | |
23 | 25 | ) |
24 | 26 | |
25 | 27 | if (process.env.NODE_ENV === 'development') { |
26 | 28 | storesEnhancers.push(DevTools.instrument()) |
app/assets/index.html | ||
---|---|---|
@@ -1,0 +1,12 @@ | ||
1 | + | |
2 | +<html lang="en"> | |
3 | + <head> | |
4 | + <meta charset="utf-8" /> | |
5 | + <title>Production TodoMVC</title> | |
6 | + <meta name="viewport" content="width=device-width, initial-scale=1" /> | |
7 | + </head> | |
8 | + <body> | |
9 | + <main></main> | |
10 | + <script src="bundle.js"></script> | |
11 | + </body> | |
12 | +</html> |
app/stack/index.js | ||
---|---|---|
@@ -1,0 +1,35 @@ | ||
1 | +const feathers = require('feathers') | |
2 | +const { mapObjIndexed, reduce, toPairs } = require('ramda') | |
3 | + | |
4 | +const stackCreators = { | |
5 | + services: require('./services'), | |
6 | + static: require('./static') | |
7 | +} | |
8 | + | |
9 | +module.exports = createStack | |
10 | + | |
11 | +function createStack(config) { | |
12 | + const stacks = createStacks(config) | |
13 | + | |
14 | + const app = feathers() | |
15 | + | |
16 | + useAll(app, stacks) | |
17 | + | |
18 | + return app | |
19 | +} | |
20 | + | |
21 | +function createStacks (config) { | |
22 | + return mapObjIndexed( | |
23 | + (stackCreator, name) => { | |
24 | + return stackCreator(config[name]) | |
25 | + }, | |
26 | + stackCreators | |
27 | + ) | |
28 | +} | |
29 | + | |
30 | +function useAll (app, services) { | |
31 | + return reduce((app, [name, service]) => { | |
32 | + return app.use(service) | |
33 | + }, app, toPairs(services)) | |
34 | +} | |
35 | + |
app/stack/services.js | ||
---|---|---|
@@ -1,0 +1,21 @@ | ||
1 | +const feathers = require('feathers') | |
2 | +const { mapObjIndexed, reduce, toPairs } = require('ramda') | |
3 | + | |
4 | +const services = require('app/services') | |
5 | + | |
6 | +module.exports = createServices | |
7 | + | |
8 | +function createServices(config) { | |
9 | + const app = feathers() | |
10 | + | |
11 | + useAll(app, services) | |
12 | + | |
13 | + return app | |
14 | +} | |
15 | + | |
16 | +function useAll (app, services) { | |
17 | + return reduce((app, [name, service]) => { | |
18 | + return app.use(`/${name}`, service) | |
19 | + }, app, toPairs(services)) | |
20 | +} | |
21 | + |
app/stack/static.js | ||
---|---|---|
@@ -1,0 +1,8 @@ | ||
1 | +const serveStatic = require('serve-static') | |
2 | +const { join } = require('path') | |
3 | + | |
4 | +module.exports = createStatic | |
5 | + | |
6 | +function createStatic (config) { | |
7 | + return serveStatic(config.root, config) | |
8 | +} |
config/defaults.js | ||
---|---|---|
@@ -1,9 +1,10 @@ | ||
1 | 1 | const { join } = require('path') |
2 | +const env = process.env | |
3 | +const nodeEnv = env.NODE_ENV | |
2 | 4 | |
3 | 5 | module.exports = { |
4 | - services: { | |
5 | - static: { | |
6 | - root: join(__dirname, 'assets') | |
7 | - } | |
8 | - } | |
6 | + static: { | |
7 | + root: join(__dirname, '..', 'assets') | |
8 | + }, | |
9 | + port: env.PORT || 5000 | |
9 | 10 | } |
package.json | ||
---|---|---|
@@ -10,10 +10,12 @@ | ||
10 | 10 | "test": "(npm run spec & npm run feature)", |
11 | 11 | "spec": "node spec", |
12 | 12 | "feature": "node feature", |
13 | 13 | "dev:client": "watchify client -o assets/bundle.js -dv -p browserify-hmr", |
14 | + "dev:assets": "cpx \"app/assets/**/*\" assets -w", | |
14 | 15 | "dev:server": "nodemon server", |
15 | 16 | "prod:client": "browserify client -o assets/bundle.js -g uglifyify", |
17 | + "prod:assets": "cpx \"app/assets/**/*\" assets", | |
16 | 18 | "prod:server": "node server", |
17 | 19 | "dev": "NODE_ENV=development npm-run-all -p dev:*", |
18 | 20 | "prod": "NODE_ENV=production npm-run-all -s prod:*" |
19 | 21 | }, |
@@ -65,8 +67,9 @@ | ||
65 | 67 | "babelify": "^7.2.0", |
66 | 68 | "browserify": "^12.0.1", |
67 | 69 | "bulk-require": "^0.2.1", |
68 | 70 | "bulkify": "^1.1.1", |
71 | + "cpx": "^1.2.1", | |
69 | 72 | "envify": "^3.4.0", |
70 | 73 | "evalify": "^1.0.1", |
71 | 74 | "feathers": "^1.2.0", |
72 | 75 | "history": "^1.13.1", |
Built with git-ssb-web