Skip to content

Commit 8567235

Browse files
authored
Merge pull request #43 from AElfProject/dev
can not use now
2 parents 7f60562 + 1d19b2e commit 8567235

File tree

75 files changed

+2089
-966
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+2089
-966
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ app/public/js/*
88
test.js
99
test.data.js
1010
app/view/
11-
app/public/assets/output/
11+
app/public/assets/output/
12+
.DS_Store

README.md

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,84 @@ bash build.sh === bash build.sh pro
1818
1919
### 0.Dependencies
2020
21-
1.The RPC URL of AElf Node.
21+
- Start up
22+
[AElf Chain](https://github.com/AElfProject/AElf),
23+
[aelf-block-scan](https://github.com/AElfProject/aelf-block-scan),
24+
[aelf-block-api](https://github.com/AElfProject/aelf-block-api)
25+
at first
2226
23-
2.[aelf-block-api](https://github.com/AElfProject/aelf-block-api)
27+
- `important`call the API[POST:api/nodes/info] of aelf-block-api to insert the AElf Node information.
28+
- Detail the APIs [Postman share link](https://www.getpostman.com/collections/6332e0fab94cdacc9c35)
29+
- You can update the information throught the PUT API.
30+
31+
- NodeJS: You can see the JS dependencies in pakage.json, we use egg.js(Node.js & Koa).
2432
25-
3.NodeJS: You can see the JS dependencies in pakage.json, we use egg.js(Node.js & Koa).
33+
- Correct config.
2634
27-
4.Nginx (or others) // Use SSL for Secure, And Use Proxy for cross-origin.
28-
29-
5.Correct config.
35+
- `optional`Nginx (or others) // Use SSL for Secure, And Use Proxy for cross-origin.
3036
3137
### 1.Change config
3238
3339
```shell
3440
cp config/demo.config.default.js config/config.default.js
3541
// set your own config.keys
3642

37-
cp config/demo.config.node.js config./config.node.js
43+
cp config/demo.config.node.js config/config.node.js
3844
// set you own httpProvider
39-
// set your own default mainContract
45+
// set your own default mainTokenContract
46+
// set your own proxy
4047
```
4148
42-
### 2.Nginx
43-
44-
Please set your own RPC URL.
45-
46-
Please ser your own server_name.
49+
### 2.Install Webpack
4750
48-
online(https): nginx/servers/wallet.online.conf
49-
50-
dev(http): nginx/servers/wallet.conf
51+
```javascript
52+
npm install -g webpack
53+
npm install -g webpack-cli
54+
```
5155
5256
### 3.Start the node server
5357
54-
npm install
58+
try build.sh at first
5559
56-
dev: npm run dev
60+
```bash
61+
npm install
62+
# If meet permisson problem.
63+
# sudo npm install --unsafe-perm=true --allow-root
5764

65+
# dev
66+
npm run dev
67+
# pro
5868
pro: npm start
69+
```
5970
6071
default port: 7001
6172
62-
# Docker[TODO]
73+
## Nginx(For Your Information.)
74+
75+
How to open gizp.
76+
77+
How to config https.(We do not config https server in nodejs.)
78+
79+
How to proxy in nginx instead of proxy in nodejs.
80+
81+
- nginx.conf
82+
- see how to open gizp
83+
84+
- servsers/wallet.online.conf & servsers/wallet.conf
85+
- set your own RPC URL.
86+
- set your own server_name.
87+
- `when use online(https)`set your own ssl_certificate and ssl_certificate_key.
88+
89+
## FAQ
90+
91+
### permission denied, open '/home/zhengyue/github/aelf-web-wallet/.travis.yml'
92+
93+
https://github.com/eggjs/egg/issues/2442#issuecomment-421895549
94+
95+
```bash
96+
sudo npm install --unsafe-perm=true --allow-root
97+
# Then
98+
sh build.sh pro # or npm start
99+
```
100+
101+
# Docker[TODO]

app/controller/address.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* @file address.js
3+
* @author huangzongzhe
4+
*/
5+
'use strict';
6+
7+
const Controller = require('../core/baseController.js');
8+
const {apiServerProvider} = require('../../config/config.node.js');
9+
const {getNodesInfo} = require('../utils/utils.js');
10+
11+
class addressController extends Controller {
12+
// the structure of return data is almost the same as the getTokens API of aelf-block-api.
13+
async getTokens() {
14+
let ctx = this.ctx;
15+
16+
const keysRule = {
17+
address: 'string',
18+
limit: {
19+
type: 'int',
20+
required: false,
21+
allowEmpty: true,
22+
max: 500,
23+
min: 0
24+
},
25+
page: {
26+
type: 'int',
27+
required: false,
28+
allowEmpty: true,
29+
min: 0
30+
}
31+
};
32+
33+
try {
34+
let {
35+
address,
36+
limit,
37+
page
38+
} = ctx.request.query;
39+
let options = {
40+
address,
41+
limit: limit ? parseInt(limit, 0) : 0,
42+
page: page ? parseInt(page, 0) : 0
43+
};
44+
45+
ctx.validate(keysRule, options);
46+
47+
const nodesInfo = (await ctx.curl(apiServerProvider
48+
+ `/api/address/tokens?address=${address}&nodes_info=1&limit=${limit}&page=${page}`, {
49+
dataType: 'json'
50+
})).data;
51+
52+
let result = [];
53+
if (nodesInfo.length) {
54+
result = await ctx.service.address.getTokens(options, nodesInfo);
55+
}
56+
57+
this.formatOutput('get', result);
58+
}
59+
catch (error) {
60+
this.formatOutput('error', error, 422);
61+
}
62+
}
63+
}
64+
65+
module.exports = addressController;

app/controller/home.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ const Controller = require('egg').Controller;
44
const nodeConfig = require('../../config/config.node.js');
55

66
class HomeController extends Controller {
7-
async index() {
8-
await this.ctx.render('index.tpl', {
9-
...nodeConfig
10-
});
11-
}
12-
async transactionDetail() {
13-
await this.ctx.render('transactionDetail.tpl', {
14-
...nodeConfig
15-
});
16-
}
7+
async index() {
8+
await this.ctx.render('index.tpl', {
9+
...nodeConfig
10+
});
11+
}
12+
async transactionDetail() {
13+
await this.ctx.render('transactionDetail.tpl', {
14+
...nodeConfig
15+
});
16+
}
1717
}
1818

1919
module.exports = HomeController;

app/controller/proxy.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**
2+
* @file proxy.js
3+
* @author huangzongzhe
4+
*/
5+
// The framework recommends that the Controller layer is responsible for processing request parameters(verification and transformation)
6+
// from user's requests, then calls related business methods in Service, encapsulates and sends back business result:
7+
// 1.retrieves parameters passed by HTTP.
8+
// 2.verifies and assembles parameters.
9+
// 3.calls the Service to handle business, if necessary,
10+
// transforms Service process results to satisfy user's requirement.
11+
// 4.sends results back to user by HTTP.
12+
// 框架推荐 Controller 层主要对用户的请求参数进行处理(校验、转换),然后调用对应的 service 方法处理业务,得到业务结果后封装并返回:
13+
// 获取用户通过 HTTP 传递过来的请求参数。
14+
// 校验、组装参数。
15+
// 调用 Service 进行业务处理,必要时处理转换 Service 的返回结果,让它适应用户的需求。
16+
// 通过 HTTP 将结果响应给用户。
17+
'use strict';
18+
19+
const Controller = require('../core/baseController.js');
20+
const {getNodesInfo} = require('../utils/utils.js');
21+
22+
// token=ELF&action=address_transactions&params=xxx
23+
const getKeysRule = {
24+
contract_address: 'string',
25+
ptype: 'string',
26+
action: 'string',
27+
query: 'object',
28+
nodesInfo: 'object'
29+
};
30+
31+
const postKeysRule = {
32+
contract_address: 'string',
33+
ptype: 'string',
34+
action: {
35+
type: 'string',
36+
required: false,
37+
allowEmpty: true
38+
},
39+
body: 'object',
40+
nodesInfo: 'object'
41+
};
42+
43+
class ProxyController extends Controller {
44+
async getProxy() {
45+
let ctx = this.ctx;
46+
47+
try {
48+
let {
49+
contract_address,
50+
ptype,
51+
action
52+
} = ctx.request.query;
53+
let options = {
54+
contract_address,
55+
ptype,
56+
action,
57+
nodesInfo: await getNodesInfo(ctx, {
58+
dataType: 'json'
59+
}),
60+
query: ctx.request.query
61+
};
62+
63+
let result;
64+
if (options.nodesInfo.length) {
65+
ctx.validate(getKeysRule, options);
66+
result = await ctx.service.proxy.getProxy(options);
67+
}
68+
else {
69+
result = {
70+
error: 1,
71+
message: 'No information of the AElf Node'
72+
};
73+
}
74+
75+
this.formatOutput('get', result);
76+
}
77+
catch (error) {
78+
this.formatOutput('error', error, 422);
79+
}
80+
}
81+
82+
async postProxy() {
83+
let ctx = this.ctx;
84+
85+
try {
86+
let {
87+
contract_address,
88+
ptype,
89+
action
90+
} = ctx.request.query;
91+
let body = ctx.request.body;
92+
let options = {
93+
contract_address,
94+
ptype,
95+
action,
96+
body,
97+
nodesInfo: await getNodesInfo(ctx, {
98+
dataType: 'json'
99+
})
100+
};
101+
102+
let result;
103+
if (options.nodesInfo.length) {
104+
ctx.validate(postKeysRule, options);
105+
result = await ctx.service.proxy.postProxy(options);
106+
}
107+
else {
108+
result = {
109+
error: 1,
110+
message: 'No information of the AElf Node'
111+
};
112+
}
113+
114+
this.formatOutput('post', result);
115+
}
116+
catch (error) {
117+
this.formatOutput('error', error, 422);
118+
}
119+
}
120+
}
121+
122+
module.exports = ProxyController;

app/core/baseController.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @file baseController.js
3+
* @author huangzongzhe
4+
*/
5+
const {Controller} = require('egg');
6+
class BaseController extends Controller {
7+
// demo
8+
// get user() {
9+
// return this.ctx.session.user;
10+
// }
11+
12+
formatOutput(type, result, errCode) {
13+
const ctx = this.ctx;
14+
switch (type) {
15+
case 'get':
16+
ctx.status = 200;
17+
break;
18+
case 'post':
19+
ctx.status = 201;
20+
break;
21+
case 'put':
22+
ctx.status = 204;
23+
break;
24+
case 'error':
25+
ctx.status = parseInt(errCode, 10);
26+
result = {
27+
message: result.message
28+
};
29+
break;
30+
default:
31+
}
32+
ctx.body = JSON.stringify(result);
33+
}
34+
}
35+
module.exports = BaseController;

app/core/baseService.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @file baseService.js
3+
* @author huangzongzhe
4+
*/
5+
const {Service} = require('egg');
6+
7+
class BaseService extends Service {
8+
// demo
9+
// get user() {
10+
// return this.ctx.session.user;
11+
// }
12+
}
13+
module.exports = BaseService;
Lines changed: 11 additions & 0 deletions
Loading

app/public/assets/svgs.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)