-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: add hello_world in go and nodejs
- Loading branch information
Showing
8 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# hello_world | ||
|
||
``` | ||
$ ./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] | ||
2020/06/17 00:44:56 ??? has disconnected from you. Services: [hello_world] | ||
$ http://localhost:3000/hello | ||
no nodes were able to process your request for service(s): [hello_world] | ||
$ go run main.go | ||
2020/06/17 00:44:37 You are now connected to 127.0.0.1:9000. Services: [] | ||
$ http://localhost:3000/hello | ||
Hello world! | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "GET /hello" | ||
service = "hello_world" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/lithdew/flatend" | ||
"os" | ||
"os/signal" | ||
) | ||
|
||
func check(err error) { | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func helloWorld(ctx *flatend.Context) { | ||
ctx.WriteHeader("Content-Type", "text/plain; charset=utf-8") | ||
ctx.Write([]byte("Hello world!")) | ||
} | ||
|
||
func main() { | ||
node := &flatend.Node{ | ||
Services: map[string]flatend.Handler{ | ||
"hello_world": helloWorld, | ||
}, | ||
} | ||
check(node.Start("127.0.0.1:9000")) | ||
|
||
ch := make(chan os.Signal, 1) | ||
signal.Notify(ch, os.Interrupt) | ||
<-ch | ||
|
||
node.Shutdown() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# hello_world | ||
|
||
``` | ||
$ ./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] | ||
2020/06/17 00:44:56 ??? has disconnected from you. Services: [hello_world] | ||
$ 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: [] | ||
$ http://localhost:3000/hello | ||
Hello world! | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "GET /hello" | ||
service = "hello_world" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('hello_world', ctx => ctx.send("Hello world!")); | ||
await node.dial("127.0.0.1:9000"); | ||
} | ||
|
||
main().catch(err => console.error(err)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "hello_world", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "node index.js" | ||
}, | ||
"license": "MIT", | ||
"dependencies": { | ||
"flatend": "^0.0.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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== |