Skip to content

Commit

Permalink
feat: listen and then server for http server
Browse files Browse the repository at this point in the history
  • Loading branch information
alidevjimmy committed Mar 29, 2024
1 parent 6bb1fb1 commit 381dbc8
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions www/jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jsonrpc
import (
"context"
"fmt"
"net"
"net/http"
"time"

Expand All @@ -18,6 +19,7 @@ type Server struct {
cancel context.CancelFunc
config *Config
server *http.Server
listener net.Listener
grpcClient *grpc.ClientConn
logger *logger.SubLogger
}
Expand Down Expand Up @@ -63,10 +65,15 @@ func (s *Server) StartServer(grpcServer string) error {
jgw := jrpc.NewServer()
jgw.RegisterServices(&blockchainService, &networkService, &transactionService, &walletService)

listener, err := net.Listen("tcp", s.config.Listen)
if err != nil {
s.logger.Error("unable to establish tcp connection", "error", err)
}

mux := http.NewServeMux()
mux.HandleFunc("/", jgw.HttpHandler)
server := &http.Server{
Addr: s.config.Listen,
Addr: listener.Addr().String(),
ReadHeaderTimeout: 3 * time.Second,
Handler: mux,
}
Expand All @@ -77,13 +84,14 @@ func (s *Server) StartServer(grpcServer string) error {
case <-s.ctx.Done():
return
default:
if err := server.ListenAndServe(); err != nil {
if err := server.Serve(listener); err != nil {
s.logger.Error("error while establishing JSON-RPC connection", "error", err)
}
}
}
}()
s.logger.Info("json-rpc started listening", "address", s.config.Listen)

s.logger.Info("json-rpc started listening", "address", listener.Addr().String())
s.server = server

return nil
Expand All @@ -94,7 +102,9 @@ func (s *Server) StopServer() {
s.logger.Debug("context closed", "reason", s.ctx.Err())

if s.server != nil {
s.server.Close()
_ = s.server.Shutdown(s.ctx)
_ = s.server.Close()
_ = s.listener.Close()
}

if s.grpcClient != nil {
Expand Down

0 comments on commit 381dbc8

Please sign in to comment.