Skip to content

Commit

Permalink
feat: resolve config and custom server
Browse files Browse the repository at this point in the history
  • Loading branch information
boizz committed Jul 10, 2018
1 parent fbd119b commit 46937f9
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
"extends": "standard"
};
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ typings/

# next.js build output
.next

# package-lock file
package-lock.json
7 changes: 7 additions & 0 deletions bin/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

const build = require('next/dist/build').default
const { resolve } = require('path')

const config = require('../server/config')
build(resolve(process.cwd(), './'), config)
3 changes: 0 additions & 3 deletions index.js

This file was deleted.

16 changes: 15 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,19 @@
"bugs": {
"url": "https://github.com/BoizZ/kidjs/issues"
},
"homepage": "https://github.com/BoizZ/kidjs#readme"
"homepage": "https://github.com/BoizZ/kidjs#readme",
"dependencies": {
"koa": "^2.5.1",
"koa-router": "^7.4.0",
"next": "^6.1.1",
"react": "^16.4.1",
"react-dom": "^16.4.1"
},
"devDependencies": {
"eslint-config-standard": "^11.0.0",
"eslint-plugin-import": "^2.13.0",
"eslint-plugin-node": "^6.0.1",
"eslint-plugin-promise": "^3.8.0",
"eslint-plugin-standard": "^3.1.0"
}
}
41 changes: 41 additions & 0 deletions server/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'

const { resolve } = require('path')
const moduleLoader = require('./utils/moduleLoader')

const dirPath = resolve(process.cwd(), './config')

const configs = moduleLoader(dirPath)

let env = 'local'

switch (process.env.NODE_ENV) {
case 'production':
case 'prod':
env = 'prod'
break
case 'test':
env = 'test'
break
case 'development':
case 'dev':
env = 'dev'
break
default:
env = 'local'
}

let server = configs['config.default'] || {}
if (configs[`config.${env}`]) {
server = Object.assign(server, configs[`config.${env}`])
}

const client = server.page
delete server.page

server.privateKey = `${+new Date()}_${parseInt(Math.random() * 10000)}`

client.publicRuntimeConfig = server.public
client.distDir = resolve(process.cwd(), '.build')

module.exports = { server, client }
33 changes: 33 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'

const Koa = require('koa')
const next = require('next')
const Router = require('koa-router')

const config = require('./config')

const port = config.server.port || 8001
const dev = !['prod', 'production'].includes(process.env.NODE_ENV)
const app = next({ dev, conf: config.client })
const handle = app.getRequestHandler()

app.prepare()
.then(() => {
const server = new Koa()
const router = new Router()

router.get('*', async ctx => {
await handle(ctx.req, ctx.res)
})

server.use(async (ctx, next) => {
ctx.res.statusCode = 200
ctx.config = config
await next()
})

server.use(router.routes())
server.listen(port, () => {
console.log(`> Ready on http://localhost:${port}`) // eslint-disable-line
})
})
27 changes: 27 additions & 0 deletions server/utils/modulesLoader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'

const fs = require('fs')

module.exports = (rootPath) => {
const moduleList = {}

const readAllControllers = (fileList, moduleList, path) => {
for (let fileName of fileList) {
const ext = /\.js$/
if (ext.test(fileName)) {
const moduleName = fileName.split(ext)[0]
moduleList[moduleName] = require(`${path}/${fileName}`)
} else {
moduleList[fileName] = {}
const subPath = `${path}/${fileName}`
const fileList = fs.readdirSync(subPath)
readAllControllers(fileList, moduleList[fileName], subPath)
}
}
}

const fileList = fs.readdirSync(rootPath)
readAllControllers(fileList, moduleList, rootPath)

return moduleList
}

0 comments on commit 46937f9

Please sign in to comment.