aboutsummaryrefslogtreecommitdiff
path: root/transport
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@mail.ru>2022-05-22 16:37:48 +0300
committerAlexander Kiryukhin <a.kiryukhin@mail.ru>2022-05-22 16:37:48 +0300
commit4a81eff217c40c459c9a9ed4f318b4dd9bc5ee8a (patch)
tree35e6d3b1f80af80fddedb26d5543377931abfe2f /transport
parentc74596c6a6a741e3365a2f372de6e7cdf2583fdc (diff)
Middlewares and optionsv1.1.0
Diffstat (limited to 'transport')
-rw-r--r--transport/http.go37
-rw-r--r--transport/tcp.go33
-rw-r--r--transport/transport.go21
3 files changed, 84 insertions, 7 deletions
diff --git a/transport/http.go b/transport/http.go
index a795b10..bb2321b 100644
--- a/transport/http.go
+++ b/transport/http.go
@@ -1,3 +1,22 @@
+//Package transport provides transports for rpc server
+//
+//Copyright (C) 2022 Alexander Kiryukhin <i@neonxp.dev>
+//
+//This file is part of go.neonxp.dev/jsonrpc2 project.
+//
+//This program is free software: you can redistribute it and/or modify
+//it under the terms of the GNU General Public License as published by
+//the Free Software Foundation, either version 3 of the License, or
+//(at your option) any later version.
+//
+//This program is distributed in the hope that it will be useful,
+//but WITHOUT ANY WARRANTY; without even the implied warranty of
+//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+//GNU General Public License for more details.
+//
+//You should have received a copy of the GNU General Public License
+//along with this program. If not, see <https://www.gnu.org/licenses/>.
+
package transport
import (
@@ -8,21 +27,33 @@ import (
)
type HTTP struct {
- Bind string
- TLS *tls.Config
+ Bind string
+ TLS *tls.Config
+ CORSOrigin string
+ Parallel bool
}
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.MethodOptions && h.CORSOrigin != "" {
+ w.Header().Set("Access-Control-Allow-Origin", h.CORSOrigin)
+ w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
+ w.WriteHeader(http.StatusOK)
+ return
+ }
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
+ if h.CORSOrigin != "" {
+ w.Header().Set("Access-Control-Allow-Origin", h.CORSOrigin)
+ w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
+ }
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
- resolver.Resolve(ctx, r.Body, w)
+ resolver.Resolve(ctx, r.Body, w, h.Parallel)
}),
BaseContext: func(l net.Listener) context.Context {
return ctx
diff --git a/transport/tcp.go b/transport/tcp.go
index 2ab946a..e8bbabe 100644
--- a/transport/tcp.go
+++ b/transport/tcp.go
@@ -1,3 +1,22 @@
+//Package transport provides transports for rpc server
+//
+//Copyright (C) 2022 Alexander Kiryukhin <i@neonxp.dev>
+//
+//This file is part of go.neonxp.dev/jsonrpc2 project.
+//
+//This program is free software: you can redistribute it and/or modify
+//it under the terms of the GNU General Public License as published by
+//the Free Software Foundation, either version 3 of the License, or
+//(at your option) any later version.
+//
+//This program is distributed in the hope that it will be useful,
+//but WITHOUT ANY WARRANTY; without even the implied warranty of
+//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+//GNU General Public License for more details.
+//
+//You should have received a copy of the GNU General Public License
+//along with this program. If not, see <https://www.gnu.org/licenses/>.
+
package transport
import (
@@ -6,17 +25,25 @@ import (
)
type TCP struct {
- Bind string
+ Bind string
+ Parallel bool
}
func (t *TCP) Run(ctx context.Context, resolver Resolver) error {
- ln, _ := net.Listen("tcp", t.Bind)
+ ln, err := net.Listen("tcp", t.Bind)
+ if err != nil {
+ return err
+ }
+ go func() {
+ <-ctx.Done()
+ ln.Close()
+ }()
for {
conn, err := ln.Accept()
if err != nil {
return err
}
- go resolver.Resolve(ctx, conn, conn)
+ go resolver.Resolve(ctx, conn, conn, t.Parallel)
}
}
diff --git a/transport/transport.go b/transport/transport.go
index 2a54295..ff9c1ba 100644
--- a/transport/transport.go
+++ b/transport/transport.go
@@ -1,3 +1,22 @@
+//Package transport provides transports for rpc server
+//
+//Copyright (C) 2022 Alexander Kiryukhin <i@neonxp.dev>
+//
+//This file is part of go.neonxp.dev/jsonrpc2 project.
+//
+//This program is free software: you can redistribute it and/or modify
+//it under the terms of the GNU General Public License as published by
+//the Free Software Foundation, either version 3 of the License, or
+//(at your option) any later version.
+//
+//This program is distributed in the hope that it will be useful,
+//but WITHOUT ANY WARRANTY; without even the implied warranty of
+//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+//GNU General Public License for more details.
+//
+//You should have received a copy of the GNU General Public License
+//along with this program. If not, see <https://www.gnu.org/licenses/>.
+
package transport
import (
@@ -10,5 +29,5 @@ type Transport interface {
}
type Resolver interface {
- Resolve(context.Context, io.Reader, io.Writer)
+ Resolve(ctx context.Context, reader io.Reader, writer io.Writer, isParallel bool)
}