Skip to content
Permalink
Browse files
重新整理hello-egg example
  • Loading branch information
hufeng committed Jun 12, 2018
1 parent 9607bce commit 02be4bc930885663e87a3960422765b341931116
Show file tree
Hide file tree
Showing 37 changed files with 523 additions and 210 deletions.
@@ -1,30 +1,28 @@
'use strict';
'ues strict';

module.exports = {
write: true,
prefix: '^',
plugin: 'autod-egg',
test: [
'test',
'benchmark',
prefix: '^',
devprefix: '^',
exclude: [
'test/fixtures',
'coverage',
],
dep: [
'egg',
'egg-scripts',
],
devdep: [
'egg-ci',
'egg-bin',
'egg-mock',
'autod',
'autod-egg',
'eslint',
'eslint-config-egg',
'webstorm-disable-index',
'egg-bin',
'tslib',
'typescript',
],
exclude: [
'./test/fixtures',
'./dist',
keep: [
],
semver: [
],
test: 'scripts',
};

This file was deleted.

This file was deleted.

@@ -1,12 +1,20 @@
logs/
npm-debug.log
yarn-error.log
node_modules/
package-lock.json
yarn.lock
coverage/
.idea/
run/
logs/
.DS_Store
*.sw*
*.un~
.vscode
*.swp
*.lock
*.js
!.autod.conf.js

app/**/*.js
test/**/*.js
config/**/*.js
app/**/*.map
test/**/*.map
config/**/*.map
@@ -1,13 +1,9 @@
# hello
# hackernews-async-ts

a simple a simple web app invoke dubbo service
[Hacker News](https://news.ycombinator.com/) showcase using typescript && egg

## QuickStart

<!-- add docs here for user -->

see [egg docs][egg] for more detail.

### Development

```bash
@@ -16,18 +12,22 @@ $ npm run dev
$ open http://localhost:7001/
```

Don't tsc compile at development mode, if you had run `tsc` then you need to `npm run clean` before `npm run dev`.

### Deploy

```bash
$ npm run tsc
$ npm start
$ npm stop
```

### npm scripts
### Npm Scripts

- Use `npm run lint` to check code style.
- Use `npm test` to run unit test.
- Use `npm run autod` to auto detect dependencies upgrade, see [autod](https://www.npmjs.com/package/autod) for more detail.
- Use `npm run lint` to check code style
- Use `npm test` to run unit test
- se `npm run clean` to clean compiled js at development mode once

### Requirement

[egg]: https://eggjs.org
- Node.js 8.x
- Typescript 2.8+

This file was deleted.

@@ -0,0 +1,8 @@
import {EggApplication} from 'egg';
import dubbo from './app/dubbo';

export default async (app: EggApplication) => {
dubbo(app);
await app.dubbo.ready();
app.coreLogger.info('dubbo was ready');
};

This file was deleted.

This file was deleted.

@@ -0,0 +1,36 @@
import {java} from 'dubbo2.js';
import {Controller} from 'egg';
import {TypeRequest} from '../dubbo/providers/com/alibaba/dubbo/demo/TypeRequest';

export default class HomeController extends Controller {
async index() {
const {ctx} = this;
ctx.body = await ctx.service.test.sayHi('egg');
}

async sayHello() {
const {res, err} = await this.ctx.app.dubbo.service.DemoProvider.sayHello(
java.String('hello from node world'),
);

this.ctx.body = err ? err.message : res;
}

async echo() {
const {res, err} = await this.ctx.app.dubbo.service.DemoProvider.echo();
this.ctx.body = err ? err.message : res;
}

async basicType() {
const {
res,
err,
} = await this.ctx.app.dubbo.service.BasicTypeProvider.testBasicType(
new TypeRequest({
bigDecimal: {value: '100.00'},
map: {hello: 'hello'},
}),
);
this.ctx.body = err ? err.message : res;
}
}
@@ -0,0 +1,42 @@
import {dubboInvoker, matcher} from 'dubbo-invoker';
import {Context, Dubbo} from 'dubbo2.js';
import {EggApplication} from 'egg';
import service from './service';

declare module 'egg' {
export interface EggApplication {
dubbo: Dubbo<typeof service>;
}
}

export default (app: EggApplication) => {
const dubbo = new Dubbo<typeof service>({
application: {name: 'node-egg-bff'},
register: 'localhost:2181',
service,
});

// extends middleware
dubbo.use(async (ctx: Context, next: any) => {
const start = Date.now();
await next();
const end = Date.now();
app.coreLogger.info(
`${ctx.dubboInterface} was invoked, cost-time ${end - start}`,
);
});

// setting runtime version etc.
dubbo.use(
dubboInvoker(
matcher
.match('com.alibaba.dubbo.demo.BasicTypeProvider', {
version: '2.0.0',
})
.match('com.alibaba.dubbo.demo.DemoProvider', {version: '1.0.0'})
.match('com.alibaba.dubbo.demo.ErrorProvider', {version: '1.0.0'}),
),
);

app.dubbo = dubbo;
};
@@ -0,0 +1,16 @@
import {Dubbo, TDubboCallResult} from 'dubbo2.js';
import {argumentMap} from 'interpret-util';
import {TypeRequest} from './TypeRequest';

export interface IBasicTypeProvider {
testBasicType(request: TypeRequest): TDubboCallResult<TypeRequest>;
}

export const BasicTypeProviderWrapper = {testBasicType: argumentMap};

export function BasicTypeProvider(dubbo: Dubbo): IBasicTypeProvider {
return dubbo.proxyService<IBasicTypeProvider>({
dubboInterface: 'com.alibaba.dubbo.demo.BasicTypeProvider',
methods: BasicTypeProviderWrapper,
});
}
@@ -0,0 +1,25 @@
import {Dubbo, TDubboCallResult} from 'dubbo2.js';
import {argumentMap, JavaString} from 'interpret-util';
import {UserRequest} from './UserRequest';
import {UserResponse} from './UserResponse';

export interface IDemoProvider {
sayHello(name: JavaString): TDubboCallResult<string>;
test(): TDubboCallResult<void>;
echo(): TDubboCallResult<string>;
getUserInfo(request: UserRequest): TDubboCallResult<UserResponse>;
}

export const DemoProviderWrapper = {
sayHello: argumentMap,
test: argumentMap,
echo: argumentMap,
getUserInfo: argumentMap,
};

export function DemoProvider(dubbo: Dubbo): IDemoProvider {
return dubbo.proxyService<IDemoProvider>({
dubboInterface: 'com.alibaba.dubbo.demo.DemoProvider',
methods: DemoProviderWrapper,
});
}
@@ -0,0 +1,15 @@
import {Dubbo, TDubboCallResult} from 'dubbo2.js';
import {argumentMap} from 'interpret-util';

export interface IErrorProvider {
errorTest(): TDubboCallResult<void>;
}

export const ErrorProviderWrapper = {errorTest: argumentMap};

export function ErrorProvider(dubbo: Dubbo): IErrorProvider {
return dubbo.proxyService<IErrorProvider>({
dubboInterface: 'com.alibaba.dubbo.demo.ErrorProvider',
methods: ErrorProviderWrapper,
});
}

0 comments on commit 02be4bc

Please sign in to comment.