-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Baiang/test
新增test分支
- Loading branch information
Showing
23 changed files
with
309 additions
and
45 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
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,17 @@ | ||
module.exports = { | ||
development: { | ||
staticUrl: '', | ||
host: '127.0.0.1', | ||
port: '3000', | ||
}, | ||
test: { | ||
staticUrl: '', | ||
host: '0.0.0.0', | ||
port: '3000', | ||
}, | ||
production: { | ||
staticUrl: '', | ||
host: '0.0.0.0', | ||
port: '3000', | ||
} | ||
}; |
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 @@ | ||
exports.welcome = ctx => ctx.res.success('Hello!'); |
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,18 @@ | ||
import chalk from 'chalk'; | ||
import moment from 'moment'; | ||
|
||
export const log = (color, level) => (message) => { | ||
const prefix = `${moment().format()} [${level}] `; | ||
if (typeof message === 'object') { | ||
return console[level](chalk[color]('%o'), `${prefix}${message}`); | ||
} | ||
return console[level](chalk[color](`${prefix}${message}`)); | ||
}; | ||
|
||
export const debug = log('white', 'debug'); | ||
|
||
export const info = log('white', 'info'); | ||
|
||
export const warn = log('yellow', 'warn'); | ||
|
||
export const error = log('red', 'error'); |
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 |
---|---|---|
@@ -1,33 +1,54 @@ | ||
import { createServer } from 'http' | ||
import { parse } from 'url' | ||
import * as next from 'next' | ||
const port = parseInt(process.env.PORT, 10) || 3000 | ||
const dev = process.env.NODE_ENV !== 'production' | ||
const conf = require('../config/next.config.js') | ||
|
||
const app = next({ | ||
import Koa from 'koa'; | ||
import next from 'next'; | ||
import bodyParser from 'koa-bodyparser'; | ||
import cors from 'kcors'; | ||
import helmet from 'koa-helmet'; | ||
import logger from 'koa-logger'; | ||
|
||
import * as log from './helpers/log'; | ||
import config from '../config/config.global'; | ||
// import requestId from './middleware/requestId'; | ||
import requestId from 'koa-requestid'; | ||
import responseHandler from './middleware/responseHandler'; | ||
import router from './routes'; | ||
// import conf from '../config/next.config.js'; | ||
|
||
const env = process.env.NODE_ENV ? process.env.NODE_ENV : 'development'; | ||
const dev = process.env.NODE_ENV !== 'production'; | ||
|
||
|
||
const nextApp = next({ | ||
dev, | ||
conf, | ||
dir:'./src' | ||
}) | ||
const handle = app.getRequestHandler() | ||
|
||
app.prepare() | ||
.then(() => { | ||
createServer((req, res) => { | ||
const parsedUrl = parse(req.url, true) | ||
const { pathname, query } = parsedUrl | ||
|
||
if (pathname === '/a') { | ||
app.render(req, res, '/a', query) | ||
} else if (pathname === '/b') { | ||
app.render(req, res, '/b', query) | ||
} else { | ||
handle(req, res, parsedUrl) | ||
} | ||
}) | ||
.listen(port, (err) => { | ||
if (err) throw err | ||
console.log(`> Ready on http://localhost:${port}`) | ||
}) | ||
}) | ||
}); | ||
|
||
const handle = nextApp.getRequestHandler(); | ||
router.nextRoute(handle); | ||
const app = new Koa(); | ||
|
||
app.use(logger()); | ||
app.use(bodyParser()); | ||
app.use(requestId()); | ||
app.use(helmet()); | ||
app.use(cors({ | ||
exposeHeaders: ['X-Request-Id'] | ||
})); | ||
app.use(responseHandler()); | ||
|
||
if (!module.parent) { | ||
nextApp.prepare() | ||
.then(() => { | ||
app.use(router.routes()); | ||
app.use(router.allowedMethods()); | ||
app.listen(config[env].port, config[env].host, () => { | ||
log.info(`API server listening on ${config[env].host}:${config[env].port}, in ${env}`); | ||
}); | ||
}); | ||
} else { | ||
// test | ||
app.use(router.routes()); | ||
app.use(router.allowedMethods()); | ||
} | ||
app.on('error', err => log.error(`Unhandled exception occured. message: ${err.message}`)); | ||
|
||
export default app; |
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,22 @@ | ||
import httpStatus from 'http-status'; | ||
|
||
const responseHandler = () => async (ctx, next) => { | ||
ctx.res.success = (data = null) => { | ||
ctx.body = { | ||
success: true, | ||
code: httpStatus.OK, | ||
data | ||
}; | ||
}; | ||
ctx.res.failure = (code = null) => (message = null) => { | ||
ctx.body = { | ||
success: false, | ||
code, | ||
message | ||
}; | ||
}; | ||
ctx.res.serverError = ctx.res.failure(httpStatus.INTERNAL_SERVER_ERROR); | ||
await next(); | ||
}; | ||
|
||
export default responseHandler; |
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,17 @@ | ||
import Router from 'koa-router'; | ||
|
||
// import mockController from './controllers/mock.controller'; | ||
|
||
const router = new Router(); | ||
// router.get('/api/get-welcome', mockController.welcome); | ||
|
||
router.nextRoute = (handle) => { | ||
router.get(/^(?!\/api)/, async (ctx) => { | ||
await handle(ctx.req, ctx.res); | ||
ctx.respond = false; | ||
}); | ||
}; | ||
|
||
|
||
export default router | ||
// module.exports = router; |
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,20 @@ | ||
import supertest from 'supertest'; | ||
import httpStatus from 'http-status'; | ||
import app from '../../index'; | ||
|
||
/*describe("## Mock", () => { | ||
const request = supertest(app.listen()); | ||
describe('# GET /api/get-welcome', () => { | ||
it('should always return welcome string', async () => { | ||
const res = await request | ||
.get('/api/get-welcome') | ||
.expect(httpStatus.OK); | ||
const { success, code, data } = res.body; | ||
expect(success).toBe(true); | ||
expect(code).toBe(httpStatus.OK); | ||
expect(data).toBe('Hello, OLAF!'); | ||
}); | ||
}); | ||
});*/ |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import Layout from '../index.jsx' | ||
import Layout from '../index' | ||
|
||
export default () => ( | ||
<Layout title="关于我们"> | ||
|
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,26 @@ | ||
export const actionTypes = { | ||
SELECT_DESCRIPTION: 'SELECT_DESCRIPTION', | ||
REQUEST_INIT: 'REQUEST_INIT', | ||
REQUEST_FAILURE: 'REQUEST_FAILURE', | ||
RECEIVE_GETS: 'RECEIVE_GETS' | ||
}; | ||
|
||
export const selectDescription = descriptionType => ({ | ||
type: actionTypes.SELECT_DESCRIPTION, | ||
descriptionType | ||
}); | ||
|
||
export const requestInit = selectedDescription => ({ | ||
type: actionTypes.REQUEST_INIT, | ||
selectedDescription | ||
}); | ||
|
||
export const requestFailure = error => ({ | ||
type: actionTypes.REQUEST_FAILURE, | ||
error | ||
}); | ||
|
||
export const requestSuccess = result => ({ | ||
type: actionTypes.RECEIVE_GETS, | ||
data: result.data | ||
}); |
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,35 @@ | ||
import { fromJS } from 'immutable'; | ||
|
||
import { actionTypes } from '../actions/entries'; | ||
import { allDescriptionType } from '../../index.tsx'; | ||
|
||
const initialStateSelectedDescription = allDescriptionType[0]; | ||
export const selectedDescription = (state = initialStateSelectedDescription, action = {}) => { | ||
switch (action.type) { | ||
case actionTypes.SELECT_DESCRIPTION: | ||
return action.descriptionType; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
const initialStateReceiveData = fromJS({ | ||
description: null, | ||
error: null | ||
}); | ||
|
||
export const receiveData = (state = initialStateReceiveData, action = {}) => { | ||
switch (action.type) { | ||
case actionTypes.RECEIVE_GETS: | ||
return state.set('description', action.data.description); | ||
case actionTypes.REQUEST_FAILURE: | ||
return state.set('error', action.error); | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
export const entriesState = { | ||
selectedDescription: initialStateSelectedDescription, | ||
receiveData: initialStateReceiveData | ||
}; |
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,12 @@ | ||
import { combineReducers } from 'redux'; | ||
|
||
import { selectedDescription, receiveData, entriesState } from './entries'; | ||
|
||
export const rootReducer = combineReducers({ | ||
selectedDescription, | ||
receiveData | ||
}); | ||
|
||
export const rootInitialState = { | ||
...entriesState | ||
}; |
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,16 @@ | ||
import { put } from 'redux-saga/effects'; | ||
|
||
import { get } from '../../utilities/fetch'; | ||
import { requestFailure, requestSuccess } from '../actions/entries'; | ||
|
||
function* requestDataSaga({ selectedDescription }) { | ||
try { | ||
const res = yield get(selectedDescription); | ||
const data = yield res.json(); | ||
yield put(requestSuccess(data)); | ||
} catch (err) { | ||
yield put(requestFailure(err.message)); | ||
} | ||
} | ||
|
||
export default requestDataSaga; |
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,12 @@ | ||
import { all, takeLatest } from 'redux-saga/effects'; | ||
|
||
import requestDataSaga from './entries'; | ||
import { actionTypes } from '../actions/entries'; | ||
|
||
function* rootSaga() { | ||
yield all([ | ||
takeLatest(actionTypes.REQUEST_INIT, requestDataSaga) | ||
]); | ||
} | ||
|
||
export default rootSaga; |
Oops, something went wrong.