This commit is contained in:
crusader 2017-08-28 18:41:41 +09:00
parent f8655e116c
commit 9c0856c2a5
4 changed files with 70 additions and 9 deletions

View File

@ -1,3 +1,9 @@
package: git.loafle.net/overflow/overflow_gateway_web
import:
- package: git.loafle.net/overflow/overflow_gateway_websocket
- package: git.loafle.net/overflow/overflow_gateway_websocket
- package: google.golang.org/grpc
version: v1.5.2
- package: git.loafle.net/overflow/overflow_api_server
subpackages:
- golang
- package: git.loafle.net/overflow/overflow_grpc_pool

View File

@ -39,11 +39,11 @@ func (h *fileHandler) GetSocketOption() *gws.SocketOptions {
return h.co
}
func (h *fileHandler) onRequest(soc gws.Socket, method string, params interface{}) (interface{}, error) {
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 interface{}) error {
func (h *fileHandler) onNotify(soc gws.Socket, method string, params []string) error {
return nil
}

View File

@ -1,11 +1,17 @@
package web
import (
"context"
"log"
"strings"
"google.golang.org/grpc/metadata"
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 {
@ -16,10 +22,13 @@ type webHandler struct {
co *gws.SocketOptions
ho *jsonrpc.Options
handler gws.MessageHandler
pool grpcPool.Pool
}
func New() WebHandler {
h := &webHandler{}
func New(pool grpcPool.Pool) WebHandler {
h := &webHandler{
pool: pool,
}
h.ho = &jsonrpc.Options{
OnRequest: h.onRequest,
@ -39,12 +48,35 @@ func (h *webHandler) GetSocketOption() *gws.SocketOptions {
return h.co
}
func (h *webHandler) onRequest(soc gws.Socket, method string, params interface{}) (interface{}, error) {
func (h *webHandler) 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
c, err := h.pool.Get()
if err != nil {
log.Println("cannot retrive GRPC Client")
return nil, err
}
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 interface{}) error {
func (h *webHandler) onNotify(soc gws.Socket, method string, params []string) error {
log.Printf("path: %s, m: %s, params: %v", soc.Path(), method, params)
return nil
}

25
main.go
View File

@ -3,11 +3,15 @@ package main
import (
"log"
"google.golang.org/grpc"
"github.com/valyala/fasthttp"
backGRpc "git.loafle.net/overflow/overflow_api_server/golang"
"git.loafle.net/overflow/overflow_gateway_web/handler/file"
"git.loafle.net/overflow/overflow_gateway_web/handler/web"
gws "git.loafle.net/overflow/overflow_gateway_websocket"
grpcPool "git.loafle.net/overflow/overflow_grpc_pool"
)
func main() {
@ -18,7 +22,26 @@ func main() {
}
s := gws.NewServer(o)
wh := web.New()
bo := &grpcPool.Options{
MaxIdle: 1,
MaxCapacity: 3,
Creators: func() (*grpc.ClientConn, interface{}, error) {
var err error
conn, err := grpc.Dial(":50006", grpc.WithInsecure())
if nil != err {
return nil, nil, err
}
c := backGRpc.NewOverflowApiServerClient(conn)
return conn, c, nil
},
}
bPool, err := grpcPool.New(bo)
if nil != err {
log.Panic("Cannot create Pool of GRPC")
}
wh := web.New(bPool)
fh := file.New()
s.HandleSocket("/web", wh.GetSocketOption())