Skip to content

Commit

Permalink
node, cmd/flatend, sdk/nodejs: use log pkg, parse addr from config.to…
Browse files Browse the repository at this point in the history
…ml, update example
  • Loading branch information
lithdew committed Jun 16, 2020
1 parent 67dbe55 commit 6f97dcd
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ The session handshake protocol is well-documented [here](https://github.com/lith
Create a new `config.toml`, paste the following in, and run the command below.

```toml
addr = ":9000"
addr = "127.0.0.1:9000"

[[http]]
addr = ":3000"
Expand Down Expand Up @@ -106,7 +106,7 @@ const helloWorld = (ctx: Context) => ctx.send("Hello world!");
async function main() {
const node = new Node();
node.register("hello_world", helloWorld);
await node.dial("0.0.0.0:3000");
await node.dial("127.0.0.1:9000");
}

main().catch(err => console.error(err));
Expand Down
1 change: 1 addition & 0 deletions cmd/flatend/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

type Config struct {
Addr string
HTTP []ConfigHTTP
}

Expand Down
20 changes: 17 additions & 3 deletions cmd/flatend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"crypto/tls"
"errors"
"fmt"
"github.com/BurntSushi/toml"
"github.com/caddyserver/certmagic"
"github.com/julienschmidt/httprouter"
Expand All @@ -16,6 +15,7 @@ import (
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
)

Expand All @@ -39,7 +39,7 @@ func main() {
var bindPort uint16

pflag.StringVarP(&configPath, "config", "c", "config.toml", "path to config file")
pflag.IPVarP(&bindHost, "host", "h", net.ParseIP("0.0.0.0"), "bind host")
pflag.IPVarP(&bindHost, "host", "h", net.ParseIP("127.0.0.1"), "bind host")
pflag.Uint16VarP(&bindPort, "port", "p", 9000, "bind port")
pflag.Parse()

Expand All @@ -50,12 +50,26 @@ func main() {
check(toml.Unmarshal(buf, &cfg))
check(cfg.Validate())

if cfg.Addr != "" {
host, port, err := net.SplitHostPort(cfg.Addr)
check(err)

bindHost = net.ParseIP(host)

{
port, err := strconv.ParseUint(port, 10, 16)
check(err)

bindPort = uint16(port)
}
}

addr := flatend.Addr(bindHost, bindPort)

node := &flatend.Node{PublicAddr: addr}
check(node.Start())

fmt.Printf("Listening for microservices on %s.\n", addr)
log.Printf("Listening for microservices on %s.", addr)

defer node.Shutdown()

Expand Down
8 changes: 8 additions & 0 deletions 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 = "GET /hello"
service = "hello_world"
9 changes: 5 additions & 4 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/lithdew/kademlia"
"github.com/lithdew/monte"
"io"
"log"
"math"
"net"
"sync"
Expand Down Expand Up @@ -171,7 +172,7 @@ func (n *Node) HandleConnState(conn *monte.Conn, state monte.ConnState) {

addr := provider.Addr()

fmt.Printf("%s has disconnected from you. Services: %s\n", addr, provider.Services())
log.Printf("%s has disconnected from you. Services: %s", addr, provider.Services())

n.clientsLock.Lock()
_, exists := n.clients[addr]
Expand All @@ -197,7 +198,7 @@ func (n *Node) HandleConnState(conn *monte.Conn, state monte.ConnState) {

duration := b.Duration()

fmt.Printf("Trying to reconnect to %s. Sleeping for %s.\n", addr, duration)
log.Printf("Trying to reconnect to %s. Sleeping for %s.", addr, duration)
time.Sleep(duration)
}
}()
Expand Down Expand Up @@ -227,7 +228,7 @@ func (n *Node) HandleMessage(ctx *monte.Context) error {
// register the microservice if it hasn't been registered before

provider := n.providers.register(ctx.Conn(), packet.ID, packet.Services, false)
fmt.Printf("%s has connected to you. Services: %s\n", provider.Addr(), provider.Services())
log.Printf("%s has connected to you. Services: %s", provider.Addr(), provider.Services())

// always reply back with what services we provide, and our
// id if we want to publicly advertise our microservice
Expand Down Expand Up @@ -432,7 +433,7 @@ func (n *Node) Probe(addr string) error {
n.providers.register(conn, packet.ID, packet.Services, true)
}

fmt.Printf("You are now connected to %s. Services: %s\n", addr, packet.Services)
log.Printf("You are now connected to %s. Services: %s", addr, packet.Services)

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion nodejs/example/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function main() {
ctx.json(JSON.parse(body.toString("utf8")));
});

await node.dial("0.0.0.0:9000");
await node.dial("127.0.0.1:9000");
}

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

0 comments on commit 6f97dcd

Please sign in to comment.