Skip to content

Commit

Permalink
examples: add pipe example in go and nodejs
Browse files Browse the repository at this point in the history
  • Loading branch information
lithdew committed Jun 16, 2020
1 parent 7a9c22a commit e9b201c
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
**/node_modules/
**/dist/
**/dist/
./flatend
4 changes: 3 additions & 1 deletion examples/go/hello_world/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# hello_world

Create a service `hello_world` that replies with "Hello world!".

```
$ ./flatend
$ flatend
2020/06/17 00:44:34 Listening for microservices on '127.0.0.1:9000'.
2020/06/17 00:44:34 Listening for HTTP requests on '[::]:3000'.
2020/06/17 00:44:37 ??? has connected to you. Services: [hello_world]
Expand Down
15 changes: 15 additions & 0 deletions examples/go/pipe/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# pipe

Whatever comes in must come out. Simple piping example: upload a file to POST /pipe.

```
$ flatend
2020/06/17 01:07:12 Listening for microservices on '127.0.0.1:9000'.
2020/06/17 01:07:12 Listening for HTTP requests on '[::]:3000'.
2020/06/17 01:07:17 ??? has connected to you. Services: [pipe]
$ go run main.go
2020/06/17 01:07:17 You are now connected to 127.0.0.1:9000. Services: []
$ POST /pipe (1.6MiB GIF)
```
8 changes: 8 additions & 0 deletions examples/go/pipe/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
addr = "127.0.0.1:9000"

[[http]]
addr = ":3000"

[[http.routes]]
path = "POST /pipe"
service = "pipe"
33 changes: 33 additions & 0 deletions examples/go/pipe/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"github.com/lithdew/flatend"
"io"
"os"
"os/signal"
)

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

func pipe(ctx *flatend.Context) {
_, _ = io.Copy(ctx, ctx.Body)
}

func main() {
node := &flatend.Node{
Services: map[string]flatend.Handler{
"pipe": pipe,
},
}
check(node.Start("127.0.0.1:9000"))

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

node.Shutdown()
}
4 changes: 3 additions & 1 deletion examples/nodejs/hello_world/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# hello_world

Create a service `hello_world` that replies with "Hello world!".

```
$ ./flatend
$ flatend
2020/06/17 00:44:34 Listening for microservices on '127.0.0.1:9000'.
2020/06/17 00:44:34 Listening for HTTP requests on '[::]:3000'.
2020/06/17 00:44:37 ??? has connected to you. Services: [hello_world]
Expand Down
3 changes: 0 additions & 3 deletions examples/nodejs/hello_world/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
"name": "hello_world",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"license": "MIT",
"dependencies": {
"flatend": "^0.0.1"
Expand Down
15 changes: 15 additions & 0 deletions examples/nodejs/pipe/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# pipe

Whatever comes in must come out. Simple piping example: upload a file to POST /pipe.

```
$ flatend
2020/06/17 01:07:12 Listening for microservices on '127.0.0.1:9000'.
2020/06/17 01:07:12 Listening for HTTP requests on '[::]:3000'.
2020/06/17 01:07:17 ??? has connected to you. Services: [pipe]
$ node index.js
Successfully dialed 127.0.0.1:9000. Services: []
$ POST /pipe (1.6MiB GIF)
```
9 changes: 9 additions & 0 deletions examples/nodejs/pipe/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const {Node} = require("flatend");

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

main().catch(err => console.error(err));
9 changes: 9 additions & 0 deletions examples/nodejs/pipe/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "pipe",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"flatend": "^0.0.1"
}
}
42 changes: 42 additions & 0 deletions examples/nodejs/pipe/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# 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==

0 comments on commit e9b201c

Please sign in to comment.