diff --git a/client/client.go b/client/client.go new file mode 100644 index 0000000..cdbea3f --- /dev/null +++ b/client/client.go @@ -0,0 +1,22 @@ +package client + +import ( + "git.loafle.net/commons_go/rpc" + "git.loafle.net/commons_go/rpc/client" + "git.loafle.net/commons_go/rpc/protocol" +) + +func NewClientHandler(addr string, codec protocol.ClientCodec, notifyRegistry rpc.Registry) ClientHandler { + ch := &ClientHandlers{} + ch.addr = addr + ch.RPCRegistry = notifyRegistry + ch.Codec = codec + + return ch +} + +type ClientHandlers struct { + client.ClientHandlers + + addr string +} diff --git a/client/client_handler.go b/client/client_handler.go new file mode 100644 index 0000000..7e0ce21 --- /dev/null +++ b/client/client_handler.go @@ -0,0 +1,7 @@ +package client + +import "git.loafle.net/commons_go/rpc/client" + +type ClientHandler interface { + client.ClientHandler +} diff --git a/client/client_handlers.go b/client/client_handlers.go new file mode 100644 index 0000000..b1a4ed5 --- /dev/null +++ b/client/client_handlers.go @@ -0,0 +1,2 @@ +package client + diff --git a/internal/server/rpc/gateway_rpc_servlet_handler.go b/internal/server/rpc/gateway_rpc_servlet_handler.go new file mode 100644 index 0000000..18e62ab --- /dev/null +++ b/internal/server/rpc/gateway_rpc_servlet_handler.go @@ -0,0 +1,9 @@ +package rpc + +import ( + "git.loafle.net/commons_go/rpc/gateway" +) + +type GatewayRPCServletHandler interface { + gateway.ServletHandler +} diff --git a/internal/server/rpc/gateway_rpc_servlet_handlers.go b/internal/server/rpc/gateway_rpc_servlet_handlers.go new file mode 100644 index 0000000..0a94188 --- /dev/null +++ b/internal/server/rpc/gateway_rpc_servlet_handlers.go @@ -0,0 +1,36 @@ +package rpc + +import ( + "fmt" + + "git.loafle.net/commons_go/logging" + crcwf "git.loafle.net/commons_go/rpc/connection/websocket/fasthttp" + "git.loafle.net/commons_go/rpc/gateway" + "git.loafle.net/commons_go/rpc/protocol" +) + +type GatewayRPCServletHandlers struct { + gateway.ServletHandlers + crcwf.ServletHandlers +} + +func (sh *GatewayRPCServletHandlers) Invoke(requestCodec protocol.RegistryCodec) (result interface{}, err error) { + if !sh.RPCRegistry.HasMethod(requestCodec.Method()) { + return nil, fmt.Errorf("RPC Servlet Handler: Method[%s] is not exist", requestCodec.Method()) + } + + result, err = sh.RPCRegistry.Invoke(requestCodec) + if nil != err { + + } + + return +} + +func (sh *GatewayRPCServletHandlers) Validate() { + sh.ServletHandlers.Validate() + + if nil == sh.RPCRegistry { + logging.Logger().Panic(fmt.Sprintf("RPC Servlet Handler: RPC Registry must be specified")) + } +} diff --git a/server/server.go b/server/server.go index c2c7660..0b16f7d 100644 --- a/server/server.go +++ b/server/server.go @@ -4,8 +4,8 @@ import ( cwf "git.loafle.net/commons_go/websocket_fasthttp" ) -func New(sh ServerHandler) Server { - s := cwf.New(sh) +func New(serverCTX ServerContext, sh ServerHandler) Server { + s := cwf.New(serverCTX, sh) return s } diff --git a/server/server_context.go b/server/server_context.go new file mode 100644 index 0000000..c31c054 --- /dev/null +++ b/server/server_context.go @@ -0,0 +1,21 @@ +package server + +import ( + cuc "git.loafle.net/commons_go/util/context" + cwf "git.loafle.net/commons_go/websocket_fasthttp" +) + +type ServerContext interface { + cwf.ServerContext +} + +type serverContext struct { + cwf.ServerContext +} + +func NewServerContext(parent cuc.Context) ServerContext { + sCTX := &serverContext{} + sCTX.ServerContext = cwf.NewServerContext(parent) + + return sCTX +} diff --git a/server/server_handlers.go b/server/server_handlers.go index 2caca1b..605db84 100644 --- a/server/server_handlers.go +++ b/server/server_handlers.go @@ -46,7 +46,7 @@ type ServerHandlers struct { // Init invoked before the server is started // If you override ths method, must call -func (sh *ServerHandlers) Init() error { +func (sh *ServerHandlers) Init(serverCTX ServerContext) error { if err := sh.ServerHandlers.Init(); nil != err { return err } @@ -56,11 +56,11 @@ func (sh *ServerHandlers) Init() error { return nil } -func (sh *ServerHandlers) Listen() (net.Listener, error) { +func (sh *ServerHandlers) Listen(serverCTX ServerContext) (net.Listener, error) { return net.Listen(config.Config.Server.Network, config.Config.Server.Addr) } -func (sh *ServerHandlers) OnStart() { +func (sh *ServerHandlers) OnStart(serverCTX ServerContext) { sh.ServerHandlers.OnStart() sh.redisSubscriber = oosr.New(redis.RedisPool.Get()) @@ -74,11 +74,11 @@ func (sh *ServerHandlers) OnStart() { } } -func (sh *ServerHandlers) OnError(ctx *fasthttp.RequestCtx, status int, reason error) { +func (sh *ServerHandlers) OnError(serverCTX ServerContext, ctx *fasthttp.RequestCtx, status int, reason error) { sh.OnError(ctx, status, reason) } -func (sh *ServerHandlers) OnStop() { +func (sh *ServerHandlers) OnStop(serverCTX ServerContext) { sh.redisSubscriber.Stop() external.ExternalDestroy()