aboutsummaryrefslogtreecommitdiff
path: root/transport
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@mail.ru>2022-05-21 20:38:21 +0300
committerAlexander Kiryukhin <a.kiryukhin@mail.ru>2022-05-21 20:38:21 +0300
commit81389df9484c28dfcec1cf7592b8d0f8b7e4e8e1 (patch)
tree7a7d0440481e45b999e828b1e5ba2b28129658bc /transport
parentd4708a3665e546eea57611b17441ad9b8c89e9a4 (diff)
Improvments. Breaking changes
Diffstat (limited to 'transport')
-rw-r--r--transport/http.go40
-rw-r--r--transport/tcp.go22
-rw-r--r--transport/transport.go14
3 files changed, 76 insertions, 0 deletions
diff --git a/transport/http.go b/transport/http.go
new file mode 100644
index 0000000..663bb31
--- /dev/null
+++ b/transport/http.go
@@ -0,0 +1,40 @@
+package transport
+
+import (
+ "context"
+ "crypto/tls"
+ "net"
+ "net/http"
+)
+
+type HTTP struct {
+ Bind string
+ TLS *tls.Config
+}
+
+func (h *HTTP) Run(ctx context.Context, resolver Resolver) error {
+ srv := http.Server{
+ Addr: h.Bind,
+ Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.Method != http.MethodPost {
+ w.WriteHeader(http.StatusMethodNotAllowed)
+ return
+ }
+ w.WriteHeader(http.StatusOK)
+ w.Header().Set("Content-Type", "application/json")
+ resolver.Resolve(ctx, r.Body, w)
+ }),
+ BaseContext: func(l net.Listener) context.Context {
+ return ctx
+ },
+ TLSConfig: h.TLS,
+ }
+ go func() {
+ <-ctx.Done()
+ srv.Close()
+ }()
+ if err := srv.ListenAndServe(); err != http.ErrServerClosed {
+ return err
+ }
+ return nil
+}
diff --git a/transport/tcp.go b/transport/tcp.go
new file mode 100644
index 0000000..2ab946a
--- /dev/null
+++ b/transport/tcp.go
@@ -0,0 +1,22 @@
+package transport
+
+import (
+ "context"
+ "net"
+)
+
+type TCP struct {
+ Bind string
+}
+
+func (t *TCP) Run(ctx context.Context, resolver Resolver) error {
+ ln, _ := net.Listen("tcp", t.Bind)
+
+ for {
+ conn, err := ln.Accept()
+ if err != nil {
+ return err
+ }
+ go resolver.Resolve(ctx, conn, conn)
+ }
+}
diff --git a/transport/transport.go b/transport/transport.go
new file mode 100644
index 0000000..2a54295
--- /dev/null
+++ b/transport/transport.go
@@ -0,0 +1,14 @@
+package transport
+
+import (
+ "context"
+ "io"
+)
+
+type Transport interface {
+ Run(ctx context.Context, resolver Resolver) error
+}
+
+type Resolver interface {
+ Resolve(context.Context, io.Reader, io.Writer)
+}