Skip to content

Commit

Permalink
examples/node: updated all node examples
Browse files Browse the repository at this point in the history
  • Loading branch information
lithdew committed Jun 17, 2020
1 parent 6df8fed commit 57a5345
Show file tree
Hide file tree
Showing 23 changed files with 8,455 additions and 434 deletions.
24 changes: 13 additions & 11 deletions examples/nodejs/counter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ $ flatend
2020/06/17 02:12:53 Listening for HTTP requests on '[::]:3000'.
2020/06/17 02:12:59 <anon> has connected to you. Services: [count]
$ node index.js
Successfully dialed 127.0.0.1:9000. Services: []
$ DEBUG=* node index.js
flatend You are now connected to 127.0.0.1:9000. Services: [] +0ms
flatend Discovered 0 peer(s). +14ms
$ http://localhost:3000
0
Expand All @@ -33,17 +34,18 @@ service = "count"
```

```js
const {Node} = require("flatend");
const { Node } = require("flatend");

let counter = 0
let counter = 0;

const count = ctx => ctx.send(`${counter++}`);
const count = (ctx) => ctx.send(`${counter++}`);

const main = async () => {
const node = new Node();
node.register('count', count);
await node.dial("127.0.0.1:9000");
}
await Node.start({
addrs: ["127.0.0.1:9000"],
services: { count: count },
});
};

main().catch(err => console.error(err));
```
main().catch((err) => console.error(err));
```
17 changes: 9 additions & 8 deletions examples/nodejs/counter/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const {Node} = require("flatend");
const { Node } = require("flatend");

let counter = 0
let counter = 0;

const count = ctx => ctx.send(`${counter++}`);
const count = (ctx) => ctx.send(`${counter++}`);

const main = async () => {
const node = new Node();
node.register('count', count);
await node.dial("127.0.0.1:9000");
}
await Node.start({
addrs: ["127.0.0.1:9000"],
services: { count: count },
});
};

main().catch(err => console.error(err));
main().catch((err) => console.error(err));
4 changes: 1 addition & 3 deletions examples/nodejs/counter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"flatend": "^0.0.1"
}
"dependencies": {}
}
38 changes: 0 additions & 38 deletions examples/nodejs/counter/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,3 @@
# yarn lockfile v1


blake2b-wasm@^1.1.0:
version "1.1.7"
resolved "https://registry.yarnpkg.com/blake2b-wasm/-/blake2b-wasm-1.1.7.tgz#e4d075da10068e5d4c3ec1fb9accc4d186c55d81"
integrity sha512-oFIHvXhlz/DUgF0kq5B1CqxIDjIJwh9iDeUUGQUcvgiGz7Wdw03McEO7CfLBy7QKGdsydcMCgO9jFNBAFCtFcA==
dependencies:
nanoassert "^1.0.0"

blake2b@^2.1.3:
version "2.1.3"
resolved "https://registry.yarnpkg.com/blake2b/-/blake2b-2.1.3.tgz#f5388be424768e7c6327025dad0c3c6d83351bca"
integrity sha512-pkDss4xFVbMb4270aCyGD3qLv92314Et+FsKzilCLxDz5DuZ2/1g3w4nmBbu6nKApPspnjG7JcwTjGZnduB1yg==
dependencies:
blake2b-wasm "^1.1.0"
nanoassert "^1.0.0"

flatend@^0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/flatend/-/flatend-0.0.1.tgz#b4d4155dccf7ad696f585929accb8c9196e57b0f"
integrity sha512-QtIEOOCvWhl22KUdpRVgkvJK4ztYutXaGw9Dgyy4++h0esNoQ+pFek8JwyGbyiUXMO++0Lt/mR9IOsPLsHmn5Q==
dependencies:
blake2b "^2.1.3"
ip "^1.1.5"
tweetnacl "^1.0.3"

ip@^1.1.5:
version "1.1.5"
resolved "https://registry.yarnpkg.com/ip/-/ip-1.1.5.tgz#bdded70114290828c0a039e72ef25f5aaec4354a"
integrity sha1-vd7XARQpCCjAoDnnLvJfWq7ENUo=

nanoassert@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/nanoassert/-/nanoassert-1.1.0.tgz#4f3152e09540fde28c76f44b19bbcd1d5a42478d"
integrity sha1-TzFS4JVA/eKMdvRLGbvNHVpCR40=

tweetnacl@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-1.0.3.tgz#ac0af71680458d8a6378d0d0d050ab1407d35596"
integrity sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw==
105 changes: 29 additions & 76 deletions examples/nodejs/file/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,24 @@ $ flatend
2020/06/17 02:36:30 Listening for HTTP requests on '[::]:3000'.
2020/06/17 02:36:41 <anon> has connected to you. Services: [file]
$ go run main.go
2020/06/17 02:36:41 You are now connected to 127.0.0.1:9000. Services: []
$ DEBUG=* node index.js
flatend You are now connected to 127.0.0.1:9000. Services: [] +0ms
flatend Discovered 0 peer(s). +10ms
$ http://localhost:3000/
package main
import (
"github.com/lithdew/flatend"
"io"
"os"
"os/signal"
)
func check(err error) {
if err != nil {
panic(err)
}
}
func main() {
node := &flatend.Node{
Services: map[string]flatend.Handler{
"file": func(ctx *flatend.Context) {
f, err := os.Open("main.go")
if err != nil {
ctx.Write([]byte(err.Error()))
return
}
_, _ = io.Copy(ctx, f)
f.Close()
},
},
}
check(node.Start("127.0.0.1:9000"))
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
<-ch
node.Shutdown()
}
const { Node } = require("flatend");
const fs = require("fs");
const main = async () => {
await Node.start({
addrs: ["127.0.0.1:9000"],
services: {
file: (ctx) => fs.createReadStream("index.js").pipe(ctx),
},
});
};
main().catch((err) => console.error(err));
```

```toml
Expand All @@ -62,42 +39,18 @@ path = "GET /"
service = "file"
```

```go
package main
```js
const { Node } = require("flatend");
const fs = require("fs");

import (
"github.com/lithdew/flatend"
"io"
"os"
"os/signal"
)
const main = async () => {
await Node.start({
addrs: ["127.0.0.1:9000"],
services: {
file: (ctx) => fs.createReadStream("index.js").pipe(ctx),
},
});
};

func check(err error) {
if err != nil {
panic(err)
}
}

func main() {
node := &flatend.Node{
Services: map[string]flatend.Handler{
"file": func(ctx *flatend.Context) {
f, err := os.Open("main.go")
if err != nil {
ctx.Write([]byte(err.Error()))
return
}
_, _ = io.Copy(ctx, f)
f.Close()
},
},
}
check(node.Start("127.0.0.1:9000"))

ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
<-ch

node.Shutdown()
}
```
main().catch((err) => console.error(err));
```
15 changes: 9 additions & 6 deletions examples/nodejs/file/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const {Node} = require("flatend");
const { Node } = require("flatend");
const fs = require("fs");

const main = async () => {
const node = new Node();
node.register('file', ctx => fs.createReadStream("index.js").pipe(ctx));
await node.dial("127.0.0.1:9000");
}
await Node.start({
addrs: ["127.0.0.1:9000"],
services: {
file: (ctx) => fs.createReadStream("index.js").pipe(ctx),
},
});
};

main().catch(err => console.error(err));
main().catch((err) => console.error(err));
4 changes: 1 addition & 3 deletions examples/nodejs/file/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"flatend": "^0.0.1"
}
"dependencies": {}
}
38 changes: 0 additions & 38 deletions examples/nodejs/file/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,3 @@
# yarn lockfile v1


blake2b-wasm@^1.1.0:
version "1.1.7"
resolved "https://registry.yarnpkg.com/blake2b-wasm/-/blake2b-wasm-1.1.7.tgz#e4d075da10068e5d4c3ec1fb9accc4d186c55d81"
integrity sha512-oFIHvXhlz/DUgF0kq5B1CqxIDjIJwh9iDeUUGQUcvgiGz7Wdw03McEO7CfLBy7QKGdsydcMCgO9jFNBAFCtFcA==
dependencies:
nanoassert "^1.0.0"

blake2b@^2.1.3:
version "2.1.3"
resolved "https://registry.yarnpkg.com/blake2b/-/blake2b-2.1.3.tgz#f5388be424768e7c6327025dad0c3c6d83351bca"
integrity sha512-pkDss4xFVbMb4270aCyGD3qLv92314Et+FsKzilCLxDz5DuZ2/1g3w4nmBbu6nKApPspnjG7JcwTjGZnduB1yg==
dependencies:
blake2b-wasm "^1.1.0"
nanoassert "^1.0.0"

flatend@^0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/flatend/-/flatend-0.0.1.tgz#b4d4155dccf7ad696f585929accb8c9196e57b0f"
integrity sha512-QtIEOOCvWhl22KUdpRVgkvJK4ztYutXaGw9Dgyy4++h0esNoQ+pFek8JwyGbyiUXMO++0Lt/mR9IOsPLsHmn5Q==
dependencies:
blake2b "^2.1.3"
ip "^1.1.5"
tweetnacl "^1.0.3"

ip@^1.1.5:
version "1.1.5"
resolved "https://registry.yarnpkg.com/ip/-/ip-1.1.5.tgz#bdded70114290828c0a039e72ef25f5aaec4354a"
integrity sha1-vd7XARQpCCjAoDnnLvJfWq7ENUo=

nanoassert@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/nanoassert/-/nanoassert-1.1.0.tgz#4f3152e09540fde28c76f44b19bbcd1d5a42478d"
integrity sha1-TzFS4JVA/eKMdvRLGbvNHVpCR40=

tweetnacl@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-1.0.3.tgz#ac0af71680458d8a6378d0d0d050ab1407d35596"
integrity sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw==
24 changes: 14 additions & 10 deletions examples/nodejs/hello_world/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ $ flatend
$ http://localhost:3000/hello
no nodes were able to process your request for service(s): [hello_world]
$ node index.js
Successfully dialed 127.0.0.1:9000. Services: []
$ DEBUG=* node index.js
flatend You are now connected to 127.0.0.1:9000. Services: [] +0ms
flatend Discovered 0 peer(s). +11ms
$ http://localhost:3000/hello
Hello world!
Expand All @@ -31,13 +32,16 @@ service = "hello_world"
```

```js
const {Node} = require("flatend");
const { Node } = require("flatend");

const main = async () => {
const node = new Node();
node.register('hello_world', ctx => ctx.send("Hello world!"));
await node.dial("127.0.0.1:9000");
}

main().catch(err => console.error(err));
```
await Node.start({
addrs: ["127.0.0.1:9000"],
services: {
hello_world: (ctx) => ctx.send("Hello world!"),
},
});
};

main().catch((err) => console.error(err));
```
18 changes: 9 additions & 9 deletions examples/nodejs/hello_world/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const {Node} = require("flatend");
const { Node } = require("flatend");

const main = async () => {
await Node.start({
addrs: ["127.0.0.1:9000"],
services: {
"hello_world": ctx => ctx.send("Hello world!"),
}
});
}
await Node.start({
addrs: ["127.0.0.1:9000"],
services: {
hello_world: (ctx) => ctx.send("Hello world!"),
},
});
};

main().catch(err => console.error(err));
main().catch((err) => console.error(err));
4 changes: 1 addition & 3 deletions examples/nodejs/hello_world/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"flatend": "^0.0.1"
}
"dependencies": {}
}
Loading

0 comments on commit 57a5345

Please sign in to comment.