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: init version sources #15

Merged
merged 1 commit into from
Jul 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 19 additions & 0 deletions .autod.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'ues strict';

module.exports = {
write: true,
plugin: 'autod-egg',
prefix: '^',
devprefix: '^',
exclude: [
'test/fixtures',
],
devdep: [
'autod',
'autod-egg'
],
keep: [
],
semver: [
],
};
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test/fixtures
test/benchmark
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "eslint-config-egg"
}
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
node_modules
coverage
*.log
npm-debug.log
.logs
logs
*.swp
run
*-run
.idea
.DS_Store
.tmp
docs/CONTRIBUTING.md
docs/README.md
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
sudo: false
language: node_js
node_js:
- '4'
- '6'
install:
- npm i npminstall && npminstall
script:
- npm run ci
after_script:
- npm i codecov && codecov
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Ordered by date of first contribution.
5 changes: 5 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

0.0.1 / 2016-07-13
==================

* init version
15 changes: 15 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
environment:
matrix:
- nodejs_version: '4'
- nodejs_version: '6'

install:
- ps: Install-Product node $env:nodejs_version
- npm i npminstall && npminstall

test_script:
- node --version
- npm --version
- npm run ci

build: off
32 changes: 32 additions & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Summary

* [介绍](README.md)
* [快速入门](user_guide/quickstart.md)
* 用户指南
* [上下文](app_dev/context/index.md)
* [配置](app_dev/config/index.md)
* [应用配置](app_dev/config/config.md)
* [路由配置](app_dev/config/router.md)
* 插件
* [安全](app_dev/plugins/security.md)
* [Cookie](app_dev/plugins/cookie.md)
* [模板渲染](app_dev/plugins/view.md)
* [数据 Mock](app_dev/plugins/mock.md)
* [缓存](app_dev/plugins/cache.md)
* [日志](app_dev/plugins/logger.md)
* [测试](app_dev/test.md)
* 发布
* 开发者指南
* [基于 egg 开发框架](core_dev/app.md)
* [loader](core_dev/loader.md)
* [Agent](core_dev/agent.md)
* [Extend](core_dev/extend.md)
* [Plugin](core_dev/plugin.md)
* [编写 View 插件](core_dev/view.md)
* [进程的信号](core_dev/singals.md)
* [版本号](core_dev/semver.md)
* [API 文档](api/Egg.html)
* [如何贡献](CONTRIBUTING.md)
* [FAQ](faq.md)
* CHANGELOG
* [1.0.0](changelog/1.0.0.md)
3 changes: 3 additions & 0 deletions docs/app_dev/config/config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# config

`config/config.js` 也是系统的配置
1 change: 1 addition & 0 deletions docs/app_dev/config/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 配置
2 changes: 2 additions & 0 deletions docs/app_dev/config/router.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# 路由配置

3 changes: 3 additions & 0 deletions docs/app_dev/context/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 上下文

上下文(context)被认为是koa框架中最好的抽象之一。在koa里,它本身具有很多好用的[api](https://github.com/koajs/koa/blob/master/docs/api/context.md)。egg结合实际业务开发场景,在保持这些api不变的同时还进行了扩展。
1 change: 1 addition & 0 deletions docs/app_dev/plugins/cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cache.md
17 changes: 17 additions & 0 deletions docs/app_dev/plugins/cookie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
cookie
=====

## API

* ctx.getCookie(name, opts)
* ctx.setCookie(name, val, opts)
* ctx.deleteCookie(name)

## cookie options

对于 cookie 的使用注意点:

* 对于前端需要可见的数据,有两种情况
* 前端可以修改 signed: false, encrypt: false, httpOnly: false
* 前端只能读,不允许写 signed: true, encrypt: false, httpOnly: false
* 对于只是后端用的数据,使用 cookie 作为 store,那么设置 encrypt: true, signed: false, httpOnly:true
1 change: 1 addition & 0 deletions docs/app_dev/plugins/logger.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
logger.md
1 change: 1 addition & 0 deletions docs/app_dev/plugins/mock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mock.md
1 change: 1 addition & 0 deletions docs/app_dev/plugins/restful.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# RESTful API
1 change: 1 addition & 0 deletions docs/app_dev/plugins/security.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
security.md
1 change: 1 addition & 0 deletions docs/app_dev/plugins/view.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
view.md
5 changes: 5 additions & 0 deletions docs/app_dev/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Egg 测试

没有测试的代码不是好应用。Egg 提供了完整的测试方案和工具,适合各种测试场景。

TBD
Binary file added docs/assets/egg-framework.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/quickstart-coverage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/quickstart-index.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/changelog/1.0.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TBD
3 changes: 3 additions & 0 deletions docs/core_dev/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 开发者手册

适合 egg 开发团队成员阅读, 如果你也想了解 egg 到底是如何实现的, 也不妨看看.
3 changes: 3 additions & 0 deletions docs/core_dev/agent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# AgentWorker 与 AppWorker

TBD
3 changes: 3 additions & 0 deletions docs/core_dev/app.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 如何基于 egg 开发框架

TBD
1 change: 1 addition & 0 deletions docs/core_dev/extend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 扩展 extend
1 change: 1 addition & 0 deletions docs/core_dev/loader.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# egg loader
1 change: 1 addition & 0 deletions docs/core_dev/plugin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 插件 plugin
3 changes: 3 additions & 0 deletions docs/core_dev/semver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 如何正确的使用版本号

TBD
3 changes: 3 additions & 0 deletions docs/core_dev/singals.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## egg 进程的信号

TBD
131 changes: 131 additions & 0 deletions docs/core_dev/singleton.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# singleton 单例模式

egg 的许多插件都有单例模式的需求,例如:mysql, oss, mongodb 等等,我们常常会通过实现插件的方式来给 egg 加上这些功能。
但是由于这些单例模式往往又要支持多实例的形式,例如一个项目中可能用到多个 mysql 的数据库,用到多个 oss bucket,
这时如果任由各个插件自己实现,各种配置和接口都会五花八门,因此 egg 提供了一套统一的通用单例实现实践。

## app.addSingleton(name, create)

在 `app` 和 `agent` 实例上,egg 都提供了一个 `addSingleton` 的方法,
通过这个方法,我们可以创建一个支持单实例、多实例和动态创建实例的单例。同时也能统一各个单例的配置。

### 用户配置和接口

假设我们要添加的单例名称为 `mysql`,我们可以通过 `app.addSingleton('mysql', createMysql)` 方法来创建 `app.mysql` 单例。

#### 配置

用户的配置中可以配置是单实例还是多实例,同时还可以配置这些实例的公共配置:

```js
// 单实例
exports.mysql = {
client: {
host: 'host',
port: 'port',
user: 'user',
password: 'password',
database: 'database',
},
};

// 多实例
exports.mysql = {
clients: {
db1: {
host: 'host',
port: 'port',
database: 'db1',
},
db1: {
host: 'host',
port: 'port',
database: 'db2',
},
},

// 多实例共享的默认配置
default: {
user: 'user',
password: 'password',
},
};
```

通过 `default` 指定的默认配置不仅仅可以在多实例的时候生效,在单实例以及后面将会提到的动态创建实例中也会生效,这样我们在编写插件的时候可以通过指定 `default` 参数来给所有的实例都设置这些默认配置。

#### 使用

由于大部分情况下,我们都只需要使用单实例,因此 egg 对单实例场景做了特殊优化,如果配置中配置成单实例了,那我们可以直接通过 `app.mysql` 获取到这个实例:

```js
const data = yield app.mysql.query('select * from table limit 10');
```

而当用户配置了多个实例的时候,我们需要通过配置中的 client key 来获取对应的实例:

```js
const db1 = app.mysql.get('db1');
const data = yield db1('select * from table limit 10');
```

有时候我们并没有办法在配置中就写明各个实例的配置,我们可以通过动态创建的方式来实现(注意,不管是用户配置成单实例还是多实例,动态创建的方法一定会存在,而且会继承配置中的默认配置)。

```js
app.db3 = app.mysql.createInstance({
host: 'host',
port: 'port',
database: 'db3',
});
```

### 插件示例

下面我们通过一个简单的示例插件来展示一下怎样通过 `app.addSingleton` 编写一个单例插件。

- create.js

```js
class DataService {
constructor(config) {
this.config = config;
}

* getConfig() {
return this.config;
}

ready(done) {
setTimeout(done, 1000);
}
}

let count = 0;
module.exports = function create(config, app) {
const done = app.readyCallback(`DataService-${count++}`);
const dataService = new DataService(config);
dataService.ready(done);
return dataService;
};
```

- app.js

```js
const createDataService = require('./create');
module.exports = function(app) {
app.addSingleton('dataService', createDataService);
};
```

- agent.js

```js
const createDataService = require('./create');
module.exports = function(agent) {
agent.addSingleton('dataService', createDataService);
};
```

通过上面三个文件,我们就可以将 `DataService` 创建成 `[app|agent].dataService` 单例,并提供给用户上面描述的配置方式和接口。
更详细的使用方式可以参照 [egg-mysql](https://github.com/egg/egg-mysql) 的实现。
3 changes: 3 additions & 0 deletions docs/core_dev/view.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 编写 egg 的 view 插件

TBD
1 change: 1 addition & 0 deletions docs/faq.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# FAQ
Binary file added docs/logo/EGG-LOGO.psd
Binary file not shown.
Binary file added docs/logo/egg1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/logo/egg10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/logo/egg11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/logo/egg2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/logo/egg3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/logo/egg4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/logo/egg5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/logo/egg6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/logo/egg7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/logo/egg8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/logo/egg9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/logo/thumb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions docs/plugins.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

@startuml
digraph world {
"onerror";
"userservice";
"userrole";
"session";
"i18n";
"validate";
"watcher";
"multipart";
"security" -> "session";
"development" -> "watcher";
"rest";
"static";
"cors" -> "security";
"logrotater";
"schedule";
}
@enduml
1 change: 1 addition & 0 deletions docs/user_guide/quickstart.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TBD
6 changes: 6 additions & 0 deletions examples/cookie/app/controller/forget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = function* () {
this.deleteCookie('remember');
this.redirect('/');
};
12 changes: 12 additions & 0 deletions examples/cookie/app/controller/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

module.exports = function* () {
if (this.getCookie('remember')) {
this.body = '<p>Remembered :). Click to <a href="/forget">forget</a>!.</p>';
return;
}

this.body = `<form method="post" action="/remember"><p>Check to <label>
<input type="checkbox" name="remember"/> remember me</label>
<input type="submit" value="Submit"/>.</p></form>`;
};
7 changes: 7 additions & 0 deletions examples/cookie/app/controller/remember.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = function* () {
const minute = 60000;
if (this.request.body.remember) this.setCookie('remember', 1, { maxAge: minute });
this.redirect('/');
};
7 changes: 7 additions & 0 deletions examples/cookie/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = app => {
app.get('/', app.controller.home);
app.get('/forget', app.controller.forget);
app.post('/remember', app.controller.remember);
};
3 changes: 3 additions & 0 deletions examples/cookie/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'my cooo00ooooool keys';
Loading