jsonrpc2/transport/http.go

72 lines
2 KiB
Go
Raw Normal View History

2022-05-22 16:37:48 +03:00
//Package transport provides transports for rpc server
//
//Copyright (C) 2022 Alexander Kiryukhin <i@neonxp.dev>
//
//This file is part of go.neonxp.ru/jsonrpc2 project.
2022-05-22 16:37:48 +03:00
//
//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/>.
2022-05-21 20:38:21 +03:00
package transport
import (
"context"
"crypto/tls"
"net"
"net/http"
)
type HTTP struct {
2022-05-22 16:37:48 +03:00
Bind string
TLS *tls.Config
CORSOrigin string
Parallel bool
2022-05-21 20:38:21 +03:00
}
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) {
2022-05-22 16:37:48 +03:00
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
}
2022-05-21 20:38:21 +03:00
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
2022-05-22 16:37:48 +03:00
if h.CORSOrigin != "" {
w.Header().Set("Access-Control-Allow-Origin", h.CORSOrigin)
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
}
2022-05-22 02:56:12 +03:00
w.Header().Add("Content-Type", "application/json")
2022-05-21 20:38:21 +03:00
w.WriteHeader(http.StatusOK)
2022-05-22 16:37:48 +03:00
resolver.Resolve(ctx, r.Body, w, h.Parallel)
2022-05-21 20:38:21 +03:00
}),
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
}