90 lines
2.0 KiB
Go
90 lines
2.0 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"
|
|
"fmt"
|
|
"git.loafle.net/overflow/overflow_proxy_service/proxy/noauthagent"
|
|
"git.loafle.net/overflow/overflow_proxy_service/proxy/target"
|
|
"git.loafle.net/overflow/overflow_proxy_service/proxy/agent"
|
|
)
|
|
|
|
var g_services map[string]Services
|
|
|
|
func AddServices(name string, s Services) {
|
|
if g_services == nil {
|
|
g_services = make(map[string]Services,0)
|
|
}
|
|
|
|
g_services[name] = s
|
|
}
|
|
|
|
type Services interface {
|
|
GetModel()(interface{})
|
|
}
|
|
|
|
func InitServices() {
|
|
g_services = make(map[string]Services,0)
|
|
|
|
// proxy services save
|
|
AddServices("Agent", agent.NewAgentService())
|
|
AddServices("Member", member.NewMemberService())
|
|
AddServices("NoAuthAgent", noauthagent.NewNoAuthAgentService())
|
|
AddServices("Target", target.NewTargetService())
|
|
|
|
}
|
|
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.GetModel())
|
|
|
|
if !ok {
|
|
glog.Error("Not Exist Service Name")
|
|
}
|
|
|
|
model := serviceName.GetModel()
|
|
convertParam(model, in.Param)
|
|
|
|
pbs := &pb.ServiceOutput{}
|
|
methodName := reflect.ValueOf(serviceName).MethodByName(in.MethodName)
|
|
params := make([]reflect.Value, methodName.Type().NumIn())
|
|
|
|
for i := 0; i < methodName.Type().NumIn(); i++{
|
|
temp := methodName.Type().In(i)
|
|
|
|
params[i] = reflect.ValueOf(model)
|
|
fmt.Println(temp)
|
|
}
|
|
|
|
// Todo Call Service Method
|
|
result := methodName.Call(params)[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())
|
|
}
|
|
|
|
}
|