From 3566309c3d98565a648688da62e752b85bcc13f4 Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Wed, 23 Aug 2023 10:43:57 -0400 Subject: [PATCH] refactoring telnet code out of main() --- cmd/echo/opentel.go | 3 +++ cmd/echo/server.go | 48 ++++++++++++++++++++++++++++++++++++--------- go.mod | 9 +++++++-- go.sum | 21 ++++++++++++++++---- 4 files changed, 66 insertions(+), 15 deletions(-) create mode 100644 cmd/echo/opentel.go diff --git a/cmd/echo/opentel.go b/cmd/echo/opentel.go new file mode 100644 index 0000000..221e923 --- /dev/null +++ b/cmd/echo/opentel.go @@ -0,0 +1,3 @@ +package main + +import () diff --git a/cmd/echo/server.go b/cmd/echo/server.go index 4950c99..fe7ef68 100644 --- a/cmd/echo/server.go +++ b/cmd/echo/server.go @@ -2,31 +2,61 @@ package main import ( "bufio" - "fmt" + "context" "log" "net" + + "github.com/ThreeDotsLabs/watermill" + "github.com/ThreeDotsLabs/watermill/message" ) -type MUDHandler struct { +type TELNETServer struct { + l net.Listener } -func main() { - l, err := net.Listen("tcp", ":5555") - if err != nil { - log.Fatal(err) +func NewTELNETServer(l net.Listener) *TELNETServer { + ts := TELNETServer{ + l: l, } - defer l.Close() + + return &ts +} + +func (ts *TELNETServer) Run(ctx context.Context) { for { - conn, err := l.Accept() + conn, err := ts.l.Accept() if err != nil { log.Fatal(err) } + go func(c net.Conn) { s := bufio.NewScanner(c) defer c.Close() + for s.Scan() { - fmt.Printf("Received user input: %q\n", s.Text()) + t := s.Text() + err := ts.Publish("raw_telnet", message.NewMessage(watermill.NewUUID(), []byte(t))) + if err != nil { + log.Fatalln("couldn't write to raw_telnet queue") + } + log.Printf("Received user input: %q\n", t) } }(conn) } } + +func (ts *TELNETServer) Publish(topic string, messages ...*message.Message) error { + + return nil +} + +func main() { + l, err := net.Listen("tcp", ":5555") + if err != nil { + log.Fatal(err) + } + defer l.Close() + + ts := NewTELNETServer(l) + ts.Run(context.Background()) +} diff --git a/go.mod b/go.mod index f42edd5..0d9246a 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,11 @@ module code.ndumas.com/ndumas/gomud go 1.19 -require github.com/therealfakemoot/go-telnet v0.0.0-20230818185913-4f336ab6b975 +require github.com/ThreeDotsLabs/watermill v1.3.3 -require github.com/reiver/go-oi v1.0.0 // indirect +require ( + github.com/google/uuid v1.3.0 // indirect + github.com/lithammer/shortuuid/v3 v3.0.7 // indirect + github.com/oklog/ulid v1.3.1 // indirect + github.com/pkg/errors v0.9.1 // indirect +) diff --git a/go.sum b/go.sum index 89546dd..62d054d 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,17 @@ -github.com/reiver/go-oi v1.0.0 h1:nvECWD7LF+vOs8leNGV/ww+F2iZKf3EYjYZ527turzM= -github.com/reiver/go-oi v1.0.0/go.mod h1:RrDBct90BAhoDTxB1fenZwfykqeGvhI6LsNfStJoEkI= -github.com/therealfakemoot/go-telnet v0.0.0-20230818185913-4f336ab6b975 h1:0ya9MAIQtQnj2zj4XA/lAkd/PHXCD1sIFh/vBMQxADM= -github.com/therealfakemoot/go-telnet v0.0.0-20230818185913-4f336ab6b975/go.mod h1:W73X7Jmde9dHeL8DTG7y3+F4fcPhyNt9HSLe/oyOX5U= +github.com/ThreeDotsLabs/watermill v1.3.3 h1:ulVgkk7n/hhWmqKKJrquWJWwIDGr9LXJ8W5XmwgSDJ4= +github.com/ThreeDotsLabs/watermill v1.3.3/go.mod h1:FUH1a4BEmr5UCmCtg7CIYvEL11mdeVBDp1404+eLP+c= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I= +github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= +github.com/lithammer/shortuuid/v3 v3.0.7 h1:trX0KTHy4Pbwo/6ia8fscyHoGA+mf1jWbPJVuvyJQQ8= +github.com/lithammer/shortuuid/v3 v3.0.7/go.mod h1:vMk8ke37EmiewwolSO1NLW8vP4ZaKlRuDIi8tWWmAts= +github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4= +github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=