68 lines
1.4 KiB
Go
68 lines
1.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"golang.org/x/net/context"
|
|
pb "git.loafle.net/overflow/overflow_api_service/grpc"
|
|
"git.loafle.net/overflow/overflow_proxy_service/proxy/member"
|
|
"github.com/golang/glog"
|
|
"encoding/json"
|
|
"reflect"
|
|
)
|
|
|
|
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", member.NewMember())
|
|
}
|
|
type ServiceImpl struct {
|
|
|
|
}
|
|
|
|
func (s *ServiceImpl) ExecServices(c context.Context, in *pb.ServiceInput) (*pb.ServiceOutput, error) {
|
|
// Check Service Name
|
|
serviceName, ok := g_services[in.ServiceName]
|
|
|
|
glog.Infoln(serviceName)
|
|
|
|
if !ok {
|
|
glog.Error("Not Exist Service Name")
|
|
}
|
|
|
|
convertParam(serviceName, in.Param)
|
|
|
|
pbs := &pb.ServiceOutput{}
|
|
|
|
// Todo Call Service Method
|
|
result := reflect.ValueOf(serviceName).MethodByName(in.MethodName).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())
|
|
}
|
|
|
|
}
|