From 7e600dc44e1302f0619e6c4759dd24f16a91c3a8 Mon Sep 17 00:00:00 2001 From: geek Date: Tue, 23 May 2017 19:08:11 +0900 Subject: [PATCH] shared project --- .gitignore | 61 +++++++++++++++++++++++++++++++++++++++++++++ gateway/services.go | 48 +++++++++++++++++++++++++++++++++++ gwrpc_service.go | 41 ++++++++++++++++++++++++++++++ rpc_service.go | 36 ++++++++++++++++++++++++++ 4 files changed, 186 insertions(+) create mode 100644 .gitignore create mode 100644 gateway/services.go create mode 100644 gwrpc_service.go create mode 100644 rpc_service.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8e2ebe9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,61 @@ +# Created by .ignore support plugin (hsz.mobi) +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties +### Go template +# Binaries for programs and plugins +*.exe +*.dll +*.so +*.dylib + +# Test binary, build with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 +.glide/ + diff --git a/gateway/services.go b/gateway/services.go new file mode 100644 index 0000000..0d383e6 --- /dev/null +++ b/gateway/services.go @@ -0,0 +1,48 @@ +package gateway + +import ( + "golang.org/x/net/context" + pb "loafle.com/overflow/overflow_api_service/grpc" + "loafle.com/overflow/overflow_proxy_service/proxy" + "github.com/golang/glog" + "reflect" + "log" +) + +var g_services map[string]interface{} + +func AddServices(name string, s interface{}) { + if g_services == nil { + g_services = make(map[string]interface{},0) + } + + g_services[name] = s +} + +func InitServices() { + g_services = make(map[string]interface{},0) + + // proxy services save + AddServices("Member", proxy.NewMember()) +} +type ServiceImpl struct { + +} + +func (s *ServiceImpl) ExecServices(c context.Context, in *pb.ServiceInput) (*pb.ServiceOutput, error) { + // Todo Check Service Name + serviceName, ok := g_services[in.ServiceName] + + log.Println(serviceName) + if !ok { + glog.Error("Not Exist Service Name") + } + + // Todo Param Unmarshal + + // Todo Getting a structure by service name + + // Todo Call Service Method + + return nil, nil +} \ No newline at end of file diff --git a/gwrpc_service.go b/gwrpc_service.go new file mode 100644 index 0000000..e684f3f --- /dev/null +++ b/gwrpc_service.go @@ -0,0 +1,41 @@ +package main + +import ( + "flag" + "github.com/golang/glog" + "golang.org/x/net/context" + "github.com/grpc-ecosystem/grpc-gateway/runtime" + "google.golang.org/grpc" + pb "loafle.com/overflow/overflow_api_service/grpc" + "net/http" +) + +var ( + overflowEndpoint = flag.String("echo_endpoint", "localhost:9090", "/v1/overflow/services") +) + +func runGwRpc() (err error) { + ctx := context.Background() + ctx, cancel := context.WithCancel(ctx) + defer cancel() + + mux := runtime.NewServeMux() + opts := []grpc.DialOption{grpc.WithInsecure()} + + err = pb.RegisterOverflowGatewayHandlerFromEndpoint(ctx, mux, *overflowEndpoint, opts) + + if err != nil { + return err + } + + return http.ListenAndServe(":8080", mux) +} + +func main() { + flag.Parse() + defer glog.Flush() + + if err := runGwRpc(); err != nil { + glog.Fatal(err) + } +} diff --git a/rpc_service.go b/rpc_service.go new file mode 100644 index 0000000..36296c3 --- /dev/null +++ b/rpc_service.go @@ -0,0 +1,36 @@ +package main + +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 { + l, err := net.Listen("tcp", ":9090") + + if err != nil { + return err + } + + s := grpc.NewServer() + + pb.RegisterOverflowGatewayServer(s, &gateway.ServiceImpl{}) + 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) + } +}