This commit is contained in:
crusader
2017-09-01 15:42:23 +09:00
parent d3f40ec6be
commit 1e1e212954
15 changed files with 365 additions and 288 deletions

View File

@@ -1,67 +0,0 @@
package file
import (
"context"
"log"
"time"
"go.uber.org/zap"
"git.loafle.net/commons_go/config"
"git.loafle.net/commons_go/logging"
"git.loafle.net/overflow/overflow_gateway_web/handler"
gws "git.loafle.net/overflow/overflow_gateway_websocket"
"git.loafle.net/overflow/overflow_gateway_websocket/protocol/jsonrpc"
)
type FileHandler interface {
handler.Handler
}
type fileHandler struct {
ctx context.Context
logger *zap.Logger
co *gws.SocketOptions
ho *jsonrpc.Options
handler gws.MessageHandler
}
func New(ctx context.Context) FileHandler {
h := &fileHandler{
ctx: ctx,
logger: logging.WithContext(ctx),
}
h.ho = &jsonrpc.Options{
OnRequest: h.onRequest,
OnNotify: h.onNotify,
}
h.handler = jsonrpc.NewHandler(ctx, h.ho)
h.co = &gws.SocketOptions{
Handler: h.handler,
MaxMessageSize: config.GetInt64("handlers.file.socket.MaxMessageSize"),
WriteTimeout: time.Duration(config.GetInt("handlers.file.socket.WriteTimeout")) * time.Second,
ReadTimeout: time.Duration(config.GetInt("handlers.file.socket.ReadTimeout")) * time.Second,
PongTimeout: time.Duration(config.GetInt("handlers.file.socket.PongTimeout")) * time.Second,
PingTimeout: time.Duration(config.GetInt("handlers.file.socket.PingTimeout")) * time.Second,
PingPeriod: time.Duration(config.GetInt("handlers.file.socket.PingPeriod")) * time.Second,
BinaryMessage: config.GetBool("handlers.file.socket.BinaryMessage"),
}
return h
}
func (h *fileHandler) GetSocketOption() *gws.SocketOptions {
return h.co
}
func (h *fileHandler) onRequest(soc gws.Socket, method string, params []string) (interface{}, error) {
log.Printf("path: %s, m: %s, params: %v", soc.Path(), method, params)
return nil, nil
}
func (h *fileHandler) onNotify(soc gws.Socket, method string, params []string) error {
return nil
}

View File

@@ -1,9 +0,0 @@
package handler
import (
gws "git.loafle.net/overflow/overflow_gateway_websocket"
)
type Handler interface {
GetSocketOption() *gws.SocketOptions
}

View File

@@ -1,106 +0,0 @@
package web
import (
"context"
"strings"
"time"
"go.uber.org/zap"
"google.golang.org/grpc/metadata"
"git.loafle.net/commons_go/config"
"git.loafle.net/commons_go/logging"
backGRpc "git.loafle.net/overflow/overflow_api_server/golang"
"git.loafle.net/overflow/overflow_gateway_web/handler"
gws "git.loafle.net/overflow/overflow_gateway_websocket"
"git.loafle.net/overflow/overflow_gateway_websocket/protocol/jsonrpc"
grpcPool "git.loafle.net/overflow/overflow_grpc_pool"
)
type WebHandler interface {
handler.Handler
}
type webHandler struct {
ctx context.Context
logger *zap.Logger
so *gws.SocketOptions
ro *jsonrpc.Options
handler gws.MessageHandler
pool grpcPool.Pool
}
func New(ctx context.Context, pool grpcPool.Pool) WebHandler {
h := &webHandler{
ctx: ctx,
logger: logging.WithContext(ctx),
pool: pool,
}
h.ro = &jsonrpc.Options{
OnRequest: h.onRequest,
OnNotify: h.onNotify,
}
h.handler = jsonrpc.NewHandler(ctx, h.ro)
h.so = &gws.SocketOptions{
Handler: h.handler,
MaxMessageSize: config.GetInt64("handlers.web.socket.MaxMessageSize"),
WriteTimeout: time.Duration(config.GetInt("handlers.web.socket.WriteTimeout")) * time.Second,
ReadTimeout: time.Duration(config.GetInt("handlers.web.socket.ReadTimeout")) * time.Second,
PongTimeout: time.Duration(config.GetInt("handlers.web.socket.PongTimeout")) * time.Second,
PingTimeout: time.Duration(config.GetInt("handlers.web.socket.PingTimeout")) * time.Second,
PingPeriod: time.Duration(config.GetInt("handlers.web.socket.PingPeriod")) * time.Second,
BinaryMessage: config.GetBool("handlers.web.socket.BinaryMessage"),
}
return h
}
func (h *webHandler) GetSocketOption() *gws.SocketOptions {
return h.so
}
func (h *webHandler) onRequest(soc gws.Socket, method string, params []string) (interface{}, error) {
h.logger.Info("OnRequest",
zap.String("path", soc.Path()),
zap.String("method", method),
zap.Any("params", params),
)
c, err := h.pool.Get()
if err != nil {
h.logger.Error("cannot retrive GRPC Client")
return nil, err
}
defer h.pool.Put(c)
sm := strings.Split(method, ".")
si := &backGRpc.ServerInput{
Target: sm[0],
Method: sm[1],
Params: params,
}
md := metadata.Pairs("email", "overflow@loafle.com")
ctx := metadata.NewOutgoingContext(context.Background(), md)
so, err := c.(backGRpc.OverflowApiServerClient).Exec(ctx, si)
if err != nil {
return nil, err
}
return so.Result, nil
}
func (h *webHandler) onNotify(soc gws.Socket, method string, params []string) error {
h.logger.Info("OnRequest",
zap.String("path", soc.Path()),
zap.String("method", method),
zap.Any("params", params),
)
return nil
}

View File

@@ -0,0 +1,72 @@
package handler
import (
"context"
"strings"
"google.golang.org/grpc/metadata"
"git.loafle.net/commons_go/logging"
oas "git.loafle.net/overflow/overflow_api_server/golang"
ogw "git.loafle.net/overflow/overflow_gateway_websocket"
"git.loafle.net/overflow/overflow_gateway_websocket/protocol/jsonrpc"
ogp "git.loafle.net/overflow/overflow_grpc_pool"
"go.uber.org/zap"
)
func newWebJSONRpcHandler(ctx context.Context, gPool ogp.Pool) ogw.ProtocolHandler {
h := &webJSONRpcHandlers{
ctx: ctx,
logger: logging.WithContext(ctx),
gPool: gPool,
}
p := jsonrpc.New(ctx, h)
return p
}
type webJSONRpcHandlers struct {
jsonrpc.JSONRpcHandlers
ctx context.Context
logger *zap.Logger
gPool ogp.Pool
}
func (h *webJSONRpcHandlers) OnRequest(soc ogw.Socket, method string, params []string) (interface{}, error) {
h.logger.Info("OnRequest",
zap.String("path", soc.Path()),
zap.String("method", method),
zap.Any("params", params),
)
c, err := h.gPool.Get()
if err != nil {
h.logger.Error("cannot retrive GRPC Client")
return nil, err
}
defer h.gPool.Put(c)
sm := strings.Split(method, ".")
si := &oas.ServerInput{
Target: sm[0],
Method: sm[1],
Params: params,
}
md := metadata.Pairs("email", "overflow@loafle.com")
ctx := metadata.NewOutgoingContext(context.Background(), md)
so, err := c.(oas.OverflowApiServerClient).Exec(ctx, si)
if err != nil {
return nil, err
}
return so.Result, nil
}
func (wjh *webJSONRpcHandlers) OnNotify(soc ogw.Socket, method string, params []string) error {
return nil
}

View File

@@ -0,0 +1,40 @@
package handler
import (
"context"
"time"
"git.loafle.net/commons_go/config"
"git.loafle.net/commons_go/logging"
ogw "git.loafle.net/overflow/overflow_gateway_websocket"
ogp "git.loafle.net/overflow/overflow_grpc_pool"
"go.uber.org/zap"
)
type webSocketHandlers struct {
ogw.SocketHandlers
ctx context.Context
logger *zap.Logger
cfg config.Configurator
}
func NewWebSocketHandler(ctx context.Context, gPool ogp.Pool) ogw.SocketHandler {
h := &webSocketHandlers{
ctx: ctx,
logger: logging.WithContext(ctx),
}
h.cfg = config.Sub("handlers.web")
h.MaxMessageSize = h.cfg.GetInt64("socket.MaxMessageSize")
h.WriteTimeout = h.cfg.GetDuration("socket.WriteTimeout") * time.Second
h.ReadTimeout = h.cfg.GetDuration("socket.ReadTimeout") * time.Second
h.PongTimeout = h.cfg.GetDuration("socket.PongTimeout") * time.Second
h.PingTimeout = h.cfg.GetDuration("socket.PingTimeout") * time.Second
h.PingPeriod = h.cfg.GetDuration("socket.PingPeriod") * time.Second
h.BinaryMessage = h.cfg.GetBool("socket.BinaryMessage")
ph := newWebJSONRpcHandler(ctx, gPool)
h.Protocol = ph
return h
}