diff --git a/gateway/service_test.go b/gateway/service_test.go new file mode 100644 index 0000000..6f053c9 --- /dev/null +++ b/gateway/service_test.go @@ -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) +} + diff --git a/gateway/services.go b/gateway/services.go index 0d383e6..3318023 100644 --- a/gateway/services.go +++ b/gateway/services.go @@ -5,8 +5,8 @@ import ( pb "loafle.com/overflow/overflow_api_service/grpc" "loafle.com/overflow/overflow_proxy_service/proxy" "github.com/golang/glog" + "encoding/json" "reflect" - "log" ) 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) { - // Todo Check Service Name + // Check Service Name serviceName, ok := g_services[in.ServiceName] - log.Println(serviceName) + glog.Infoln(serviceName) + if !ok { 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 + result := reflect.ValueOf(serviceName).MethodByName("Read").Call([]reflect.Value{})[0].String() - return nil, nil -} \ No newline at end of file + 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()) + } + +} diff --git a/gwrpc_main.go b/gwrpc_main.go new file mode 100644 index 0000000..23963e4 --- /dev/null +++ b/gwrpc_main.go @@ -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) + } +} diff --git a/rpc_main.go b/rpc_main.go new file mode 100644 index 0000000..b91cdd7 --- /dev/null +++ b/rpc_main.go @@ -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) + } +} diff --git a/gwrpc_service.go b/server/gwrpc.go similarity index 77% rename from gwrpc_service.go rename to server/gwrpc.go index e684f3f..9acf87f 100644 --- a/gwrpc_service.go +++ b/server/gwrpc.go @@ -1,8 +1,7 @@ -package main +package server import ( "flag" - "github.com/golang/glog" "golang.org/x/net/context" "github.com/grpc-ecosystem/grpc-gateway/runtime" "google.golang.org/grpc" @@ -14,7 +13,7 @@ var ( overflowEndpoint = flag.String("echo_endpoint", "localhost:9090", "/v1/overflow/services") ) -func runGwRpc() (err error) { +func RunGwRpc() (err error) { ctx := context.Background() ctx, cancel := context.WithCancel(ctx) defer cancel() @@ -29,13 +28,4 @@ func runGwRpc() (err error) { } return http.ListenAndServe(":8080", mux) -} - -func main() { - flag.Parse() - defer glog.Flush() - - if err := runGwRpc(); err != nil { - glog.Fatal(err) - } -} +} \ No newline at end of file diff --git a/rpc_service.go b/server/rpc.go similarity index 59% rename from rpc_service.go rename to server/rpc.go index 36296c3..eae5514 100644 --- a/rpc_service.go +++ b/server/rpc.go @@ -1,15 +1,13 @@ -package main +package server import ( "net" - "flag" - "github.com/golang/glog" "google.golang.org/grpc" pb "loafle.com/overflow/overflow_api_service/grpc" "loafle.com/overflow/overflow_gateway_service/gateway" ) -func runRpc() error { +func RunRpc() error { l, err := net.Listen("tcp", ":9090") if err != nil { @@ -22,15 +20,4 @@ func runRpc() error { s.Serve(l) return nil -} - -func main() { - flag.Parse() - defer glog.Flush() - - gateway.InitServices() - - if err := runRpc(); err != nil { - glog.Fatal("Gateway Server Failed: ", err) - } -} +} \ No newline at end of file diff --git a/server/rpc_test.go b/server/rpc_test.go new file mode 100644 index 0000000..9cb88db --- /dev/null +++ b/server/rpc_test.go @@ -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) + } +} \ No newline at end of file