Skip to content
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

Merged
merged 2 commits into from
Jan 17, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions framework/README.md
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
7 changes: 7 additions & 0 deletions framework/app/app/controller/home.js
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);
};
5 changes: 5 additions & 0 deletions framework/app/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = app => {
app.get('/', 'home');
};
1 change: 1 addition & 0 deletions framework/app/app/view/home.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hi, {{ name }}
13 changes: 13 additions & 0 deletions framework/app/package.json
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2.0.0

},
"scripts": {
"dev": "egg-bin dev"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

单测加一下

},
"egg": {
"framework": "yadan"
}
}
5 changes: 5 additions & 0 deletions framework/yadan/app/extend/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = {

};
5 changes: 5 additions & 0 deletions framework/yadan/app/extend/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = {

};
17 changes: 17 additions & 0 deletions framework/yadan/app/service/test.js
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 };
}
}
);
16 changes: 16 additions & 0 deletions framework/yadan/config/config.default.js
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;
};
8 changes: 8 additions & 0 deletions framework/yadan/config/plugin.js
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',
};

12 changes: 12 additions & 0 deletions framework/yadan/index.js
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;
13 changes: 13 additions & 0 deletions framework/yadan/lib/agent.js
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;
13 changes: 13 additions & 0 deletions framework/yadan/lib/application.js
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;
19 changes: 19 additions & 0 deletions framework/yadan/package.json
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"
}
}
39 changes: 39 additions & 0 deletions framework/yadan/test/lib/framework.test.js
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'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里不是直接写 fixtures 下的路径就可以了?

Copy link
Member Author

@popomore popomore Jan 16, 2017

Choose a reason for hiding this comment

The 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',
});
});
});
2 changes: 1 addition & 1 deletion hackernews/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"private": true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

框架不应该 private

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是 app 吧

Copy link
Member

Choose a reason for hiding this comment

The 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": {
Expand Down