-
Notifications
You must be signed in to change notification settings - Fork 737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add framework example #9
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Framework Example | ||
|
||
This example contains [app] and [framework] | ||
|
||
## Quick Start | ||
|
||
Start an application using custom framework called yadan | ||
|
||
```bash | ||
$ cd app | ||
$ npm install | ||
$ npm link ../yadan | ||
$ npm run dev | ||
``` | ||
|
||
Yadan is a framework, it should be published to npm normally. With this example, you just `npm link` it. | ||
|
||
## Run Test | ||
|
||
Application | ||
|
||
```bash | ||
$ cd app | ||
$ npm test | ||
``` | ||
|
||
Framework | ||
|
||
```bash | ||
$ cd yadan | ||
$ npm test | ||
``` | ||
|
||
## Questions & Suggestions | ||
|
||
Please open an issue [here](https://github.com/eggjs/egg/issues). | ||
|
||
[app]: https://github.com/eggjs/examples/tree/master/framework/app | ||
[framework]: https://github.com/eggjs/examples/tree/master/framework/yadan |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
module.exports = function* home() { | ||
// use service defined in framework | ||
const data = yield this.service.test.get(123); | ||
yield this.render('home.tpl', data); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = app => { | ||
app.get('/', 'home'); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
hi, {{ name }} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "framework-example", | ||
"version": "1.0.0", | ||
"devDependencies": { | ||
"egg-bin": "^1.0.0" | ||
}, | ||
"scripts": { | ||
"dev": "egg-bin dev" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 单测加一下 |
||
}, | ||
"egg": { | ||
"framework": "yadan" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
|
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
|
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
module.exports = app => ( | ||
/** | ||
* Test Service | ||
*/ | ||
class Test extends app.Service { | ||
constructor(ctx) { | ||
super(ctx); | ||
this.config = this.app.config.test; | ||
} | ||
|
||
* get(id) { | ||
return { id, name: this.config.key }; | ||
} | ||
} | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict'; | ||
|
||
module.exports = appInfo => { | ||
const config = {}; | ||
|
||
/** | ||
* some description | ||
* @member Config#test | ||
* @property {String} key - some description | ||
*/ | ||
config.test = { | ||
key: appInfo.name + '_123456', | ||
}; | ||
|
||
return config; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
|
||
// add you build-in plugin here, example: | ||
exports.view = { | ||
enable: true, | ||
package: 'egg-view-nunjucks', | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
const Application = require('./lib/application'); | ||
const Agent = require('./lib/agent'); | ||
const egg = require('egg'); | ||
|
||
// clone egg API | ||
Object.assign(exports, egg); | ||
|
||
// override Application and Agent | ||
exports.Application = Application; | ||
exports.Agent = Agent; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const egg = require('egg'); | ||
const EGG_PATH = Symbol.for('egg#eggPath'); | ||
|
||
class YadanAgent extends egg.Agent { | ||
get [EGG_PATH]() { | ||
return path.dirname(__dirname); | ||
} | ||
} | ||
|
||
module.exports = YadanAgent; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const egg = require('egg'); | ||
const EGG_PATH = Symbol.for('egg#eggPath'); | ||
|
||
class YadanApplication extends egg.Application { | ||
get [EGG_PATH]() { | ||
return path.dirname(__dirname); | ||
} | ||
} | ||
|
||
module.exports = YadanApplication; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "yadan", | ||
"version": "1.0.0", | ||
"dependencies": { | ||
"egg": "^0.7.0", | ||
"egg-view-nunjucks": "1.0.0" | ||
}, | ||
"devDependencies": { | ||
"egg-bin": "^1.10.0", | ||
"egg-mock": "^2.0.0", | ||
"supertest": "^2.0.1" | ||
}, | ||
"engines": { | ||
"node": ">=6.0.0" | ||
}, | ||
"scripts": { | ||
"test": "egg-bin test" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const assert = require('assert'); | ||
const request = require('supertest'); | ||
const mm = require('egg-mock'); | ||
|
||
describe('test/lib/framework.test.js', () => { | ||
let app; | ||
before(() => { | ||
app = mm.app({ | ||
baseDir: path.join(__dirname, '../../../app'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里不是直接写 fixtures 下的路径就可以了? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 移到 app 了,不要要建一个重复的 fixture |
||
customEgg: true, | ||
}); | ||
return app.ready(); | ||
}); | ||
after(() => app.close()); | ||
afterEach(mm.restore); | ||
|
||
it('should GET /', () => { | ||
return request(app.callback()) | ||
.get('/') | ||
.expect('hi, framework-example_123456') | ||
.expect(200); | ||
}); | ||
|
||
it('should load config', () => { | ||
assert(app.config.test.key === 'framework-example_123456'); | ||
}); | ||
|
||
it('should load service', function* () { | ||
const ctx = app.mockContext(); | ||
const data = yield ctx.service.test.get(123); | ||
assert.deepEqual(data, { | ||
id: 123, | ||
name: 'framework-example_123456', | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
"private": true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 框架不应该 private There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个是 app 吧 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 喔, 没注意看, 以为是 framework |
||
"dependencies": { | ||
"egg": "^0.7.0", | ||
"egg-view-nunjucks": "^0.6.0", | ||
"egg-view-nunjucks": "^1.0.0", | ||
"moment": "^2.17.1" | ||
}, | ||
"devDependencies": { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2.0.0