refactoring stuff into libraries

main
Nick Dumas 1 year ago
parent 13fdf6160c
commit eaeb4b7ea7

@ -0,0 +1,50 @@
package main
import (
"context"
"log"
"net"
"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/message"
"github.com/ThreeDotsLabs/watermill/message/router/middleware"
"github.com/ThreeDotsLabs/watermill/message/router/plugin"
"github.com/ThreeDotsLabs/watermill/pubsub/gochannel"
"code.ndumas.com/ndumas/gomud/telnet"
)
func main() {
l, err := net.Listen("tcp", ":5555")
if err != nil {
log.Fatal(err)
}
defer l.Close()
wmLogger := watermill.NewStdLogger(false, false)
logger := NewWrappedSlogger()
router, err := message.NewRouter(message.RouterConfig{}, logger)
router.AddPlugin(plugin.SignalsHandler)
router.AddMiddleware(
middleware.CorrelationID,
middleware.Recoverer,
)
pubSub := gochannel.NewGoChannel(gochannel.Config{}, wmLogger)
ctx := context.Background()
ts := telnet.NewTELNETServer(ctx, l, pubSub, logger)
tp := telnet.NewTELNETParser(ctx, pubSub, logger)
go tp.Handle()
for {
conn, err := ts.l.Accept()
if err != nil {
log.Fatal(err)
}
go ts.Handle(conn)
}
}

@ -1,48 +1 @@
package main package main
import (
"context"
"log"
"net"
"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/message"
"github.com/ThreeDotsLabs/watermill/message/router/middleware"
"github.com/ThreeDotsLabs/watermill/message/router/plugin"
"github.com/ThreeDotsLabs/watermill/pubsub/gochannel"
)
func main() {
l, err := net.Listen("tcp", ":5555")
if err != nil {
log.Fatal(err)
}
defer l.Close()
wmLogger := watermill.NewStdLogger(false, false)
logger := NewWrappedSlogger()
router, err := message.NewRouter(message.RouterConfig{}, logger)
router.AddPlugin(plugin.SignalsHandler)
router.AddMiddleware(
middleware.CorrelationID,
middleware.Recoverer,
)
pubSub := gochannel.NewGoChannel(gochannel.Config{}, wmLogger)
ctx := context.Background()
ts := NewTELNETServer(ctx, l, pubSub, logger)
tp := NewTELNETParser(ctx, pubSub, logger)
go tp.Handle()
for {
conn, err := ts.l.Accept()
if err != nil {
log.Fatal(err)
}
go ts.Handle(conn)
}
}

@ -1 +0,0 @@
package protocol

@ -1,4 +1,4 @@
package main package telnet
import ( import (
"bufio" "bufio"
@ -11,10 +11,17 @@ import (
"github.com/ThreeDotsLabs/watermill/message/router/middleware" "github.com/ThreeDotsLabs/watermill/message/router/middleware"
) )
type TELNETOption struct {
Us, UsQ, Them, ThemQ bool
}
type TELNETOptions map[string]TELNETOption
type TELNETParser struct { type TELNETParser struct {
s message.Subscriber s message.Subscriber
c context.Context c context.Context
logger watermill.LoggerAdapter logger watermill.LoggerAdapter
to TELNETOptions
} }
func (tp *TELNETParser) Handle() { func (tp *TELNETParser) Handle() {
@ -27,7 +34,7 @@ func (tp *TELNETParser) Handle() {
fields := make(watermill.LogFields) fields := make(watermill.LogFields)
fields["body"] = string(message.Payload) fields["body"] = string(message.Payload)
fields["correlation_id"] = string(message.Metadata["correlation_id"]) fields["correlation_id"] = string(message.Metadata["correlation_id"])
tp.logger.Info("received raw TCP line", fields) tp.logger.Trace("received raw TCP line", fields)
message.Ack() message.Ack()
} }
@ -46,6 +53,7 @@ type TELNETServer struct {
p message.Publisher p message.Publisher
c context.Context c context.Context
logger watermill.LoggerAdapter logger watermill.LoggerAdapter
to TELNETOptions
} }
func NewTELNETServer(c context.Context, l net.Listener, p message.Publisher, wml watermill.LoggerAdapter) *TELNETServer { func NewTELNETServer(c context.Context, l net.Listener, p message.Publisher, wml watermill.LoggerAdapter) *TELNETServer {
@ -62,6 +70,8 @@ func NewTELNETServer(c context.Context, l net.Listener, p message.Publisher, wml
func (ts *TELNETServer) Handle(conn net.Conn) { func (ts *TELNETServer) Handle(conn net.Conn) {
s := bufio.NewScanner(conn) s := bufio.NewScanner(conn)
defer conn.Close() defer conn.Close()
// i need to create an ecs Renderer that targets a iw.Writer (net.Conn)
// the renderer
correlationID := watermill.NewUUID() correlationID := watermill.NewUUID()
ts.logger = ts.logger.With( ts.logger = ts.logger.With(
@ -77,6 +87,6 @@ func (ts *TELNETServer) Handle(conn net.Conn) {
if err != nil { if err != nil {
log.Fatalln("couldn't write to telnet.raw") log.Fatalln("couldn't write to telnet.raw")
} }
ts.logger.Info("received bytes over telnet", nil) ts.logger.Trace("received bytes over telnet", nil)
} }
} }
Loading…
Cancel
Save