gateway service impl
This commit is contained in:
parent
7e600dc44e
commit
7ae897f147
55
gateway/service_test.go
Normal file
55
gateway/service_test.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
package gateway
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"reflect"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/golang/glog"
|
||||||
|
"loafle.com/overflow/overflow_proxy_service/proxy"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestServices(t *testing.T) {
|
||||||
|
InitServices()
|
||||||
|
|
||||||
|
meb, _ := g_services["Member"]
|
||||||
|
|
||||||
|
m := make(map[string]string)
|
||||||
|
m["email"] = "geek@loafle.com"
|
||||||
|
m["pw"] = "qwer5795"
|
||||||
|
|
||||||
|
convertParam(meb, m)
|
||||||
|
//val := []reflect.Value{reflect.ValueOf(meb.(*proxy.MemberService))}
|
||||||
|
|
||||||
|
va := reflect.ValueOf(meb).MethodByName("Regist").Call([]reflect.Value{})
|
||||||
|
|
||||||
|
|
||||||
|
//reflect.ValueOf(meb).MethodByName("Read").Call(reflect.ValueOf(meb))
|
||||||
|
|
||||||
|
assert.Equal(t, 0, len(va))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServiceParam(t *testing.T) {
|
||||||
|
|
||||||
|
InitServices()
|
||||||
|
|
||||||
|
meb, _ := g_services["Member"]
|
||||||
|
|
||||||
|
m := make(map[string]string)
|
||||||
|
m["email"] = "geek@loafle.com"
|
||||||
|
m["pw"] = "qwer5795"
|
||||||
|
|
||||||
|
|
||||||
|
paramStr, err := json.Marshal(m)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
glog.Fatal("Json Marshal Failed : ", err.Error())
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(paramStr, meb)
|
||||||
|
if err != nil {
|
||||||
|
glog.Fatal("Json Unmarshal Failed : ", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.ObjectsAreEqual(proxy.MemberService{}, meb)
|
||||||
|
}
|
||||||
|
|
@ -5,8 +5,8 @@ import (
|
|||||||
pb "loafle.com/overflow/overflow_api_service/grpc"
|
pb "loafle.com/overflow/overflow_api_service/grpc"
|
||||||
"loafle.com/overflow/overflow_proxy_service/proxy"
|
"loafle.com/overflow/overflow_proxy_service/proxy"
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
|
"encoding/json"
|
||||||
"reflect"
|
"reflect"
|
||||||
"log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var g_services map[string]interface{}
|
var g_services map[string]interface{}
|
||||||
@ -30,19 +30,38 @@ type ServiceImpl struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *ServiceImpl) ExecServices(c context.Context, in *pb.ServiceInput) (*pb.ServiceOutput, error) {
|
func (s *ServiceImpl) ExecServices(c context.Context, in *pb.ServiceInput) (*pb.ServiceOutput, error) {
|
||||||
// Todo Check Service Name
|
// Check Service Name
|
||||||
serviceName, ok := g_services[in.ServiceName]
|
serviceName, ok := g_services[in.ServiceName]
|
||||||
|
|
||||||
log.Println(serviceName)
|
glog.Infoln(serviceName)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
glog.Error("Not Exist Service Name")
|
glog.Error("Not Exist Service Name")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Todo Param Unmarshal
|
convertParam(serviceName, in.Param)
|
||||||
|
|
||||||
// Todo Getting a structure by service name
|
pbs := &pb.ServiceOutput{}
|
||||||
|
|
||||||
// Todo Call Service Method
|
// Todo Call Service Method
|
||||||
|
result := reflect.ValueOf(serviceName).MethodByName("Read").Call([]reflect.Value{})[0].String()
|
||||||
|
|
||||||
|
pbs.ResultStr = result
|
||||||
|
|
||||||
|
return pbs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertParam(sn interface{}, param map[string]string) {
|
||||||
|
|
||||||
|
// param convert string
|
||||||
|
paramStr, err := json.Marshal(param)
|
||||||
|
if err != nil {
|
||||||
|
glog.Fatal("Json Marshal Failed : ", err.Error())
|
||||||
|
}
|
||||||
|
// service converting
|
||||||
|
err = json.Unmarshal(paramStr, sn)
|
||||||
|
if err != nil {
|
||||||
|
glog.Fatal("Json Unmarshal Failed : ", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
return nil, nil
|
|
||||||
}
|
}
|
17
gwrpc_main.go
Normal file
17
gwrpc_main.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"github.com/golang/glog"
|
||||||
|
"loafle.com/overflow/overflow_gateway_service/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
|
defer glog.Flush()
|
||||||
|
|
||||||
|
if err := server.RunGwRpc(); err != nil {
|
||||||
|
glog.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
20
rpc_main.go
Normal file
20
rpc_main.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"loafle.com/overflow/overflow_gateway_service/gateway"
|
||||||
|
"flag"
|
||||||
|
"github.com/golang/glog"
|
||||||
|
"loafle.com/overflow/overflow_gateway_service/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
|
defer glog.Flush()
|
||||||
|
|
||||||
|
gateway.InitServices()
|
||||||
|
|
||||||
|
if err := server.RunRpc(); err != nil {
|
||||||
|
glog.Fatal("Gateway Server Failed: ", err)
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,7 @@
|
|||||||
package main
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"github.com/golang/glog"
|
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
"github.com/grpc-ecosystem/grpc-gateway/runtime"
|
"github.com/grpc-ecosystem/grpc-gateway/runtime"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
@ -14,7 +13,7 @@ var (
|
|||||||
overflowEndpoint = flag.String("echo_endpoint", "localhost:9090", "/v1/overflow/services")
|
overflowEndpoint = flag.String("echo_endpoint", "localhost:9090", "/v1/overflow/services")
|
||||||
)
|
)
|
||||||
|
|
||||||
func runGwRpc() (err error) {
|
func RunGwRpc() (err error) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
@ -30,12 +29,3 @@ func runGwRpc() (err error) {
|
|||||||
|
|
||||||
return http.ListenAndServe(":8080", mux)
|
return http.ListenAndServe(":8080", mux)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
|
||||||
flag.Parse()
|
|
||||||
defer glog.Flush()
|
|
||||||
|
|
||||||
if err := runGwRpc(); err != nil {
|
|
||||||
glog.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,15 +1,13 @@
|
|||||||
package main
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
"flag"
|
|
||||||
"github.com/golang/glog"
|
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
pb "loafle.com/overflow/overflow_api_service/grpc"
|
pb "loafle.com/overflow/overflow_api_service/grpc"
|
||||||
"loafle.com/overflow/overflow_gateway_service/gateway"
|
"loafle.com/overflow/overflow_gateway_service/gateway"
|
||||||
)
|
)
|
||||||
|
|
||||||
func runRpc() error {
|
func RunRpc() error {
|
||||||
l, err := net.Listen("tcp", ":9090")
|
l, err := net.Listen("tcp", ":9090")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -23,14 +21,3 @@ func runRpc() error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
|
||||||
flag.Parse()
|
|
||||||
defer glog.Flush()
|
|
||||||
|
|
||||||
gateway.InitServices()
|
|
||||||
|
|
||||||
if err := runRpc(); err != nil {
|
|
||||||
glog.Fatal("Gateway Server Failed: ", err)
|
|
||||||
}
|
|
||||||
}
|
|
89
server/rpc_test.go
Normal file
89
server/rpc_test.go
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"loafle.com/overflow/overflow_gateway_service/gateway"
|
||||||
|
"flag"
|
||||||
|
"github.com/golang/glog"
|
||||||
|
"time"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"net/http"
|
||||||
|
"bytes"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func startRpcServerTest() <- chan error{
|
||||||
|
ch := make(chan error, 2)
|
||||||
|
go func() {
|
||||||
|
if err := RunRpc(); err != nil {
|
||||||
|
ch <- fmt.Errorf("cannot run grpc service: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
if err := RunGwRpc(); err != nil {
|
||||||
|
ch <- fmt.Errorf("cannot run gateway service: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return ch
|
||||||
|
}
|
||||||
|
|
||||||
|
func callGatewayRpc() string {
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
|
||||||
|
url := "http://localhost:8080/v1/overflow/services"
|
||||||
|
glog.Infoln("URL : ", url)
|
||||||
|
|
||||||
|
var jsonStr = []byte(`{"serviceName":"Member","methodName":"Regist","param":{"email":"geek@loafle.com","password":"qwer5795"}}`)
|
||||||
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
glog.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("X-Custom-Header", "myvalue")
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
glog.Error(err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
log.Println("response Status:", resp.Status)
|
||||||
|
log.Println("response Headers:", resp.Header)
|
||||||
|
body, _ := ioutil.ReadAll(resp.Body)
|
||||||
|
log.Println("response Body:", string(body))
|
||||||
|
|
||||||
|
return string(body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRpcServer(t *testing.T) {
|
||||||
|
|
||||||
|
flag.Parse()
|
||||||
|
defer glog.Flush()
|
||||||
|
|
||||||
|
gateway.InitServices()
|
||||||
|
|
||||||
|
errch := startRpcServerTest()
|
||||||
|
|
||||||
|
chStr := make(chan string , 1)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
chStr <- callGatewayRpc()
|
||||||
|
}()
|
||||||
|
|
||||||
|
|
||||||
|
select {
|
||||||
|
case err := <- errch:
|
||||||
|
log.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
case status := <-chStr:
|
||||||
|
log.Println( status)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user