Skip to content

Commit

Permalink
cmd/flatend, nodejs: make configuration for a flatend node simpler, a…
Browse files Browse the repository at this point in the history
…nd port nodejs sdk to typescript
  • Loading branch information
lithdew committed Jun 14, 2020
1 parent a36cc01 commit 65ce479
Show file tree
Hide file tree
Showing 15 changed files with 786 additions and 743 deletions.
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,26 @@
[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/github.com/lithdew/flatend)
[![Discord Chat](https://img.shields.io/discord/697002823123992617)](https://discord.gg/HZEbkeQ)

```ts
import {Node} from "flatend";

async function main() {
const node = new Node();
node.register("get_todos", (data: any) => data);
node.register("all_todos", () => "hello world");
await node.dial("0.0.0.0:9000");
}

main().catch(err => console.error(err));
```

```go
package main

import (
"github.com/lithdew/flatend"
"os"
"os/signal"
"strconv"
"sync/atomic"
)
Expand All @@ -30,9 +45,18 @@ func handleGetTodos(ctx *flatend.Context) []byte {
}

func main() {
service := &flatend.Service{Addr: "127.0.0.1:9000"}
service.Register("all_todos", handleAllTodos)
service.Register("get_todos", handleGetTodos)
check(service.Start())
node := &flatend.Node{
Services: map[string]flatend.Handler{
"all_todos": handleAllTodos,
"get_todos": handleGetTodos,
},
}
check(node.Start("0.0.0.0:9000"))

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

node.Shutdown()
}
```
8 changes: 1 addition & 7 deletions cmd/flatend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,7 @@ func main() {

addr := flatend.Addr(bindHost, bindPort)

node := &flatend.Node{
PublicAddr: addr,
SecretKey: flatend.GenerateSecretKey(),
BindAddrs: []flatend.BindFunc{
flatend.BindTCP(addr),
},
}
node := &flatend.Node{PublicAddr: addr}
check(node.Start())

fmt.Printf("Listening for microservices on %s.\n", addr)
Expand Down
17 changes: 16 additions & 1 deletion node.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func GenerateSecretKey() kademlia.PrivateKey {

func (n *Node) Start(addrs ...string) error {
if n.PublicAddr != "" && n.SecretKey == kademlia.ZeroPrivateKey {
return errors.New("secret key must be provided if microservice has a public address to advertise")
n.SecretKey = GenerateSecretKey()
}

var (
Expand Down Expand Up @@ -95,6 +95,21 @@ func (n *Node) Start(addrs ...string) error {
ConnState: n,
}

if n.id != nil && len(n.BindAddrs) == 0 {
ln, err := BindTCP(Addr(n.id.Host, n.id.Port))()
if err != nil {
return err
}

n.wg.Add(1)
go func() {
defer n.wg.Done()
n.srv.Serve(ln)
}()

n.lns = append(n.lns, ln)
}

for _, fn := range n.BindAddrs {
ln, err := fn()
if err != nil {
Expand Down
123 changes: 0 additions & 123 deletions nodejs/flatend.js

This file was deleted.

40 changes: 0 additions & 40 deletions nodejs/main.js

This file was deleted.

Loading

0 comments on commit 65ce479

Please sign in to comment.