From 46937f97699e2d93647260b5c5eb44f67179bb07 Mon Sep 17 00:00:00 2001 From: Heleth Date: Sat, 21 Apr 2018 12:15:20 +0800 Subject: [PATCH] feat: resolve config and custom server --- .eslintrc.js | 3 +++ .gitignore | 3 +++ bin/build.js | 7 ++++++ index.js | 3 --- package.json | 16 +++++++++++++- server/config.js | 41 +++++++++++++++++++++++++++++++++++ server/index.js | 33 ++++++++++++++++++++++++++++ server/utils/modulesLoader.js | 27 +++++++++++++++++++++++ 8 files changed, 129 insertions(+), 4 deletions(-) create mode 100644 .eslintrc.js create mode 100644 bin/build.js delete mode 100644 index.js create mode 100644 server/config.js create mode 100644 server/index.js create mode 100644 server/utils/modulesLoader.js diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..a5b82de --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,3 @@ +module.exports = { + "extends": "standard" +}; \ No newline at end of file diff --git a/.gitignore b/.gitignore index ad46b30..94533cd 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,6 @@ typings/ # next.js build output .next + +# package-lock file +package-lock.json diff --git a/bin/build.js b/bin/build.js new file mode 100644 index 0000000..f31580c --- /dev/null +++ b/bin/build.js @@ -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) diff --git a/index.js b/index.js deleted file mode 100644 index f738b63..0000000 --- a/index.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = () => { - console.log('Hello kid!') -} \ No newline at end of file diff --git a/package.json b/package.json index 688ef84..09a3030 100644 --- a/package.json +++ b/package.json @@ -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" + } } diff --git a/server/config.js b/server/config.js new file mode 100644 index 0000000..ad9e71c --- /dev/null +++ b/server/config.js @@ -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 } diff --git a/server/index.js b/server/index.js new file mode 100644 index 0000000..38639b2 --- /dev/null +++ b/server/index.js @@ -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 + }) + }) diff --git a/server/utils/modulesLoader.js b/server/utils/modulesLoader.js new file mode 100644 index 0000000..2dd4227 --- /dev/null +++ b/server/utils/modulesLoader.js @@ -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 +}