-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: resolve config and custom server
- Loading branch information
Showing
8 changed files
with
129 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
"extends": "standard" | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,3 +59,6 @@ typings/ | |
|
||
# next.js build output | ||
.next | ||
|
||
# package-lock file | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |