github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/edgetest/server.go (about)

     1  package edgetest
     2  
     3  import (
     4  	"github.com/ronaksoft/rony"
     5  	"github.com/ronaksoft/rony/edge"
     6  	dummyGateway "github.com/ronaksoft/rony/internal/gateway/dummy"
     7  )
     8  
     9  /*
    10     Creation Time: 2020 - Dec - 09
    11     Created by:  (ehsan)
    12     Maintainers:
    13        1.  Ehsan N. Moosa (E2)
    14     Auditor: Ehsan N. Moosa (E2)
    15     Copyright Ronak Software Group 2020
    16  */
    17  
    18  type CheckFunc func(b []byte, kv ...*rony.KeyValue) error
    19  
    20  var (
    21  	connID uint64 = 1
    22  )
    23  
    24  type Server struct {
    25  	edge *edge.Server
    26  	gw   *dummyGateway.Gateway
    27  }
    28  
    29  func NewServer(serverID string, d edge.Dispatcher) *Server {
    30  	s := &Server{}
    31  	s.edge = edge.NewServer(serverID,
    32  		edge.WithCustomDispatcher(d),
    33  		edge.WithTestGateway(edge.DummyGatewayConfig{
    34  			Exposer: func(gw *dummyGateway.Gateway) {
    35  				s.gw = gw
    36  			},
    37  		}),
    38  	)
    39  
    40  	return s
    41  }
    42  
    43  func (s *Server) Start() {
    44  	_ = s.edge.StartGateway()
    45  }
    46  
    47  func (s *Server) Shutdown() {
    48  	s.edge.Shutdown()
    49  }
    50  
    51  func (s *Server) RealEdge() *edge.Server {
    52  	return s.edge
    53  }
    54  
    55  func (s *Server) SetGlobalPreHandlers(h ...edge.Handler) {
    56  	s.edge.SetGlobalPreHandlers(h...)
    57  }
    58  
    59  func (s *Server) SetHandlers(constructor uint64, h ...edge.Handler) {
    60  	s.edge.SetHandler(edge.NewHandlerOptions().SetConstructor(constructor).SetHandler(h...))
    61  }
    62  
    63  func (s *Server) SetGlobalPostHandlers(h ...edge.Handler) {
    64  	s.edge.SetGlobalPostHandlers(h...)
    65  }
    66  
    67  func (s *Server) RPC() *rpcCtx {
    68  	return newRPCContext(s.gw)
    69  }
    70  
    71  func (s *Server) JsonRPC() *jrpcCtx {
    72  	return newJSONRPCContext(s.gw)
    73  }
    74  
    75  func (s *Server) REST() *restCtx {
    76  	return newRESTContext(s.gw)
    77  }
    78  
    79  func (s *Server) Scenario() *scenario {
    80  	return newScenario()
    81  }