diff --git a/pkg/service/rest_server.go b/pkg/server/rest_server.go similarity index 85% rename from pkg/service/rest_server.go rename to pkg/server/rest_server.go index 7290ad8d..80e842a6 100644 --- a/pkg/service/rest_server.go +++ b/pkg/server/rest_server.go @@ -1,6 +1,3 @@ -//go:build rpc || !cgo -// +build rpc !cgo - // Copyright The KCL Authors. All rights reserved. package service @@ -16,22 +13,18 @@ import ( "github.com/julienschmidt/httprouter" "kcl-lang.io/kcl-go/pkg/3rdparty/grpc_gateway_util" + "kcl-lang.io/kcl-go/pkg/kcl" + "kcl-lang.io/kcl-go/pkg/service" "kcl-lang.io/kcl-go/pkg/spec/gpyrpc" + "kcl-lang.io/lib/go/api" ) var _ = fmt.Sprint -// Client represents an restful method result. -type RestfulResult struct { - Error string `json:"error"` - Result proto.Message `json:"result"` -} - type restServer struct { address string router *httprouter.Router - builtin *BuiltinServiceClient - c *KclvmServiceClient + service api.ServiceClient } func RunRestServer(address string) error { @@ -46,8 +39,7 @@ func newRestServer(address string) *restServer { p := &restServer{ address: address, router: httprouter.New(), - builtin: NewBuiltinServiceClient(), - c: newKclvmServiceClient(), + service: kcl.Service(), } p.initHttpRrouter() return p @@ -60,7 +52,6 @@ func (p *restServer) Run() error { func (p *restServer) initHttpRrouter() { p.router.GET("/api:protorpc/BuiltinService.Ping", p.handle_Ping) - p.router.GET("/api:protorpc/BuiltinService.ListMethod", p.handle_ListMethod) p.router.GET("/api:protorpc/KclvmService.ExecProgram", p.handle_ExecProgram) p.router.GET("/api:protorpc/KclvmService.BuildProgram", p.handle_BuildProgram) @@ -83,7 +74,6 @@ func (p *restServer) initHttpRrouter() { p.router.GET("/api:protorpc/KclvmService.GetVersion", p.handle_GetVersion) p.router.POST("/api:protorpc/BuiltinService.Ping", p.handle_Ping) - p.router.POST("/api:protorpc/BuiltinService.ListMethod", p.handle_ListMethod) p.router.POST("/api:protorpc/KclvmService.ExecProgram", p.handle_ExecProgram) p.router.POST("/api:protorpc/KclvmService.BuildProgram", p.handle_BuildProgram) @@ -125,7 +115,7 @@ func (p *restServer) handle( w.Header().Set("Content-Type", "application/json") - var result RestfulResult + var result service.RestfulResult if x, err := fn(); err != nil { result.Error = err.Error() } else { @@ -144,162 +134,155 @@ func (p *restServer) handle( func (p *restServer) handle_Ping(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { var args = new(gpyrpc.Ping_Args) p.handle(w, r, args, func() (proto.Message, error) { - return p.builtin.Ping(args) - }) -} - -func (p *restServer) handle_ListMethod(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - var args = new(gpyrpc.ListMethod_Args) - p.handle(w, r, args, func() (proto.Message, error) { - return p.builtin.ListMethod(args) + return p.service.Ping(args) }) } func (p *restServer) handle_ExecProgram(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { var args = new(gpyrpc.ExecProgram_Args) p.handle(w, r, args, func() (proto.Message, error) { - return p.c.ExecProgram(args) + return p.service.ExecProgram(args) }) } -// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0. +// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecuteProgram method and will be removed in v0.11.0. func (p *restServer) handle_BuildProgram(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { var args = new(gpyrpc.BuildProgram_Args) p.handle(w, r, args, func() (proto.Message, error) { - return p.c.BuildProgram(args) + return p.service.BuildProgram(args) }) } -// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0. +// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecuteProgram method and will be removed in v0.11.0. func (p *restServer) handle_ExecArtifact(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { var args = new(gpyrpc.ExecArtifact_Args) p.handle(w, r, args, func() (proto.Message, error) { - return p.c.ExecArtifact(args) + return p.service.ExecArtifact(args) }) } func (p *restServer) handle_ParseFile(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { var args = new(gpyrpc.ParseFile_Args) p.handle(w, r, args, func() (proto.Message, error) { - return p.c.ParseFile(args) + return p.service.ParseFile(args) }) } func (p *restServer) handle_ParseProgram(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { var args = new(gpyrpc.ParseProgram_Args) p.handle(w, r, args, func() (proto.Message, error) { - return p.c.ParseProgram(args) + return p.service.ParseProgram(args) }) } func (p *restServer) handle_ListOptions(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { var args = new(gpyrpc.ParseProgram_Args) p.handle(w, r, args, func() (proto.Message, error) { - return p.c.ListOptions(args) + return p.service.ListOptions(args) }) } func (p *restServer) handle_ListVariables(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { var args = new(gpyrpc.ListVariables_Args) p.handle(w, r, args, func() (proto.Message, error) { - return p.c.ListVariables(args) + return p.service.ListVariables(args) }) } func (p *restServer) handle_LoadPackage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { var args = new(gpyrpc.LoadPackage_Args) p.handle(w, r, args, func() (proto.Message, error) { - return p.c.LoadPackage(args) + return p.service.LoadPackage(args) }) } func (p *restServer) handle_FormatCode(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { var args = new(gpyrpc.FormatCode_Args) p.handle(w, r, args, func() (proto.Message, error) { - return p.c.FormatCode(args) + return p.service.FormatCode(args) }) } func (p *restServer) handle_FormatPath(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { var args = new(gpyrpc.FormatPath_Args) p.handle(w, r, args, func() (proto.Message, error) { - return p.c.FormatPath(args) + return p.service.FormatPath(args) }) } func (p *restServer) handle_LintPath(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { args := new(gpyrpc.LintPath_Args) p.handle(w, r, args, func() (proto.Message, error) { - return p.c.LintPath(args) + return p.service.LintPath(args) }) } func (p *restServer) handle_OverrideFile(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { args := new(gpyrpc.OverrideFile_Args) p.handle(w, r, args, func() (proto.Message, error) { - return p.c.OverrideFile(args) + return p.service.OverrideFile(args) }) } func (p *restServer) handle_GetSchemaTypeMapping(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { args := new(gpyrpc.GetSchemaTypeMapping_Args) p.handle(w, r, args, func() (proto.Message, error) { - return p.c.GetSchemaTypeMapping(args) + return p.service.GetSchemaTypeMapping(args) }) } func (p *restServer) handle_ValidateCode(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { args := new(gpyrpc.ValidateCode_Args) p.handle(w, r, args, func() (proto.Message, error) { - return p.c.ValidateCode(args) + return p.service.ValidateCode(args) }) } func (p *restServer) handle_ListDepFiles(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { args := new(gpyrpc.ListDepFiles_Args) p.handle(w, r, args, func() (proto.Message, error) { - return p.c.ListDepFiles(args) + return p.service.ListDepFiles(args) }) } func (p *restServer) handle_LoadSettingsFiles(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { args := new(gpyrpc.LoadSettingsFiles_Args) p.handle(w, r, args, func() (proto.Message, error) { - return p.c.LoadSettingsFiles(args) + return p.service.LoadSettingsFiles(args) }) } func (p *restServer) handle_Rename(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { args := new(gpyrpc.Rename_Args) p.handle(w, r, args, func() (proto.Message, error) { - return p.c.Rename(args) + return p.service.Rename(args) }) } func (p *restServer) handle_RenameCode(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { args := new(gpyrpc.RenameCode_Args) p.handle(w, r, args, func() (proto.Message, error) { - return p.c.RenameCode(args) + return p.service.RenameCode(args) }) } func (p *restServer) handle_Test(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { args := new(gpyrpc.Test_Args) p.handle(w, r, args, func() (proto.Message, error) { - return p.c.Test(args) + return p.service.Test(args) }) } func (p *restServer) handle_UpdateDependencies(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { args := new(gpyrpc.UpdateDependencies_Args) p.handle(w, r, args, func() (proto.Message, error) { - return p.c.UpdateDependencies(args) + return p.service.UpdateDependencies(args) }) } func (p *restServer) handle_GetVersion(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { args := new(gpyrpc.GetVersion_Args) p.handle(w, r, args, func() (proto.Message, error) { - return p.c.GetVersion(args) + return p.service.GetVersion(args) }) } diff --git a/pkg/service/client_kclvm_service.go b/pkg/service/client_kclvm_service.go index d064d9c3..4c92b2f3 100644 --- a/pkg/service/client_kclvm_service.go +++ b/pkg/service/client_kclvm_service.go @@ -63,7 +63,7 @@ func (p *KclvmServiceClient) ExecProgram(args *gpyrpc.ExecProgram_Args) (resp *g return } -// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0. +// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecuteProgram method and will be removed in v0.11.0. func (p *KclvmServiceClient) BuildProgram(args *gpyrpc.BuildProgram_Args) (resp *gpyrpc.BuildProgram_Result, err error) { p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) { resp, err = p.getClient(c).BuildProgram(args) @@ -72,7 +72,7 @@ func (p *KclvmServiceClient) BuildProgram(args *gpyrpc.BuildProgram_Args) (resp return } -// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0. +// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecuteProgram method and will be removed in v0.11.0. func (p *KclvmServiceClient) ExecArtifact(args *gpyrpc.ExecArtifact_Args) (resp *gpyrpc.ExecProgram_Result, err error) { p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) { resp, err = p.getClient(c).ExecArtifact(args) diff --git a/pkg/service/grpc_server.go b/pkg/service/grpc_server.go index 3b64d9d9..cfd4e5b6 100644 --- a/pkg/service/grpc_server.go +++ b/pkg/service/grpc_server.go @@ -47,12 +47,12 @@ func (p *_KclvmServiceImpl) ExecProgram(ctx context.Context, args *gpyrpc.ExecPr return p.c.ExecProgram(args) } -// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0. +// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecuteProgram method and will be removed in v0.11.0. func (p *_KclvmServiceImpl) BuildProgram(ctx context.Context, args *gpyrpc.BuildProgram_Args) (*gpyrpc.BuildProgram_Result, error) { return p.c.BuildProgram(args) } -// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0. +// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecuteProgram method and will be removed in v0.11.0. func (p *_KclvmServiceImpl) ExecArtifact(ctx context.Context, args *gpyrpc.ExecArtifact_Args) (*gpyrpc.ExecProgram_Result, error) { return p.c.ExecArtifact(args) } diff --git a/pkg/service/rest_client_test.go b/pkg/service/rest_client_test.go deleted file mode 100644 index c8d19129..00000000 --- a/pkg/service/rest_client_test.go +++ /dev/null @@ -1,23 +0,0 @@ -//go:build rpc || !cgo -// +build rpc !cgo - -package service - -import ( - "testing" - - "kcl-lang.io/kcl-go/pkg/spec/gpyrpc" -) - -func TestCallRestMethod_noMethod(t *testing.T) { - var args = gpyrpc.Ping_Args{Value: "ping"} - var result gpyrpc.Ping_Result - - var err = CallRestMethod( - "http://"+tRestServerAddr, "UnknownService.noMethod", - &args, &result, - ) - if err == nil { - t.Fatalf("expect error, got %v", err) - } -} diff --git a/pkg/service/testmain_test.go b/pkg/service/testmain_test.go deleted file mode 100644 index 0ac45a74..00000000 --- a/pkg/service/testmain_test.go +++ /dev/null @@ -1,46 +0,0 @@ -//go:build rpc || !cgo -// +build rpc !cgo - -// Copyright The KCL Authors. All rights reserved. - -package service - -import ( - "fmt" - "log" - "net/http" - "os" - "strconv" - "testing" - "time" - - "kcl-lang.io/kcl-go/pkg/runtime" -) - -const tEnvNumCpu = "KCL_GO_API_TEST_NUM_CPU" -const tRestServerAddr = "127.0.0.1:7001" - -func TestMain(m *testing.M) { - if s := os.Getenv(tEnvNumCpu); s != "" { - if x, err := strconv.Atoi(s); err == nil { - fmt.Println("TestMain: nWorker =", x) - runtime.InitRuntime(x) - } - } - - go func() { - if err := RunRestServer(tRestServerAddr); err != nil { - log.Fatal(err) - } - }() - - // wait for http server ready - for i := 0; i < 10; i++ { - time.Sleep(time.Second) - if _, err := http.Get("http://" + tRestServerAddr); err == nil { - break - } - } - - os.Exit(m.Run()) -} diff --git a/pkg/service/types.go b/pkg/service/types.go new file mode 100644 index 00000000..3b390bca --- /dev/null +++ b/pkg/service/types.go @@ -0,0 +1,9 @@ +package service + +import "github.com/golang/protobuf/proto" + +// Client represents an restful method result. +type RestfulResult struct { + Error string `json:"error"` + Result proto.Message `json:"result"` +} diff --git a/pkg/spec/gpyrpc/gpyrpc.pb.protorpc.go b/pkg/spec/gpyrpc/gpyrpc.pb.protorpc.go index 736b242c..58b4ab01 100644 --- a/pkg/spec/gpyrpc/gpyrpc.pb.protorpc.go +++ b/pkg/spec/gpyrpc/gpyrpc.pb.protorpc.go @@ -213,9 +213,9 @@ func PROTORPC_DialBuiltinServiceTimeout(network, addr string, timeout time.Durat type PROTORPC_KclvmService interface { Ping(in *Ping_Args, out *Ping_Result) error ExecProgram(in *ExecProgram_Args, out *ExecProgram_Result) error - // Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0. + // Depreciated: Please use the env.EnableFastEvalMode() and c.ExecuteProgram method and will be removed in v0.11.0. BuildProgram(in *BuildProgram_Args, out *BuildProgram_Result) error - // Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0. + // Depreciated: Please use the env.EnableFastEvalMode() and c.ExecuteProgram method and will be removed in v0.11.0. ExecArtifact(in *ExecArtifact_Args, out *ExecProgram_Result) error ParseProgram(in *ParseProgram_Args, out *ParseProgram_Result) error ListOptions(in *ParseProgram_Args, out *ListOptions_Result) error @@ -392,7 +392,7 @@ func (c *PROTORPC_KclvmServiceClient) AsyncExecProgram(in *ExecProgram_Args, out ) } -// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0. +// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecuteProgram method and will be removed in v0.11.0. func (c *PROTORPC_KclvmServiceClient) BuildProgram(in *BuildProgram_Args) (out *BuildProgram_Result, err error) { if in == nil { in = new(BuildProgram_Args) @@ -421,7 +421,7 @@ func (c *PROTORPC_KclvmServiceClient) BuildProgram(in *BuildProgram_Args) (out * return out, nil } -// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0. +// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecuteProgram method and will be removed in v0.11.0. func (c *PROTORPC_KclvmServiceClient) AsyncBuildProgram(in *BuildProgram_Args, out *BuildProgram_Result, done chan *rpc.Call) *rpc.Call { if in == nil { in = new(BuildProgram_Args) @@ -433,7 +433,7 @@ func (c *PROTORPC_KclvmServiceClient) AsyncBuildProgram(in *BuildProgram_Args, o ) } -// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0. +// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecuteProgram method and will be removed in v0.11.0. func (c *PROTORPC_KclvmServiceClient) ExecArtifact(in *ExecArtifact_Args) (out *ExecProgram_Result, err error) { if in == nil { in = new(ExecArtifact_Args) @@ -462,7 +462,7 @@ func (c *PROTORPC_KclvmServiceClient) ExecArtifact(in *ExecArtifact_Args) (out * return out, nil } -// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0. +// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecuteProgram method and will be removed in v0.11.0. func (c *PROTORPC_KclvmServiceClient) AsyncExecArtifact(in *ExecArtifact_Args, out *ExecProgram_Result, done chan *rpc.Call) *rpc.Call { if in == nil { in = new(ExecArtifact_Args) diff --git a/pkg/spec/gpyrpc/service.go b/pkg/spec/gpyrpc/service.go index 1921696e..c0890b36 100644 --- a/pkg/spec/gpyrpc/service.go +++ b/pkg/spec/gpyrpc/service.go @@ -337,7 +337,7 @@ type KclvmServiceServer interface { // / "id": 1 // / } // / ``` - // Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0. + // Depreciated: Please use the env.EnableFastEvalMode() and c.ExecuteProgram method and will be removed in v0.11.0. BuildProgram(context.Context, *BuildProgram_Args) (*BuildProgram_Result, error) // / Execute the KCL artifact with args. **Note that it is not thread safe.** // / @@ -370,7 +370,7 @@ type KclvmServiceServer interface { // / "id": 1 // / } // / ``` - // Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0. + // Depreciated: Please use the env.EnableFastEvalMode() and c.ExecuteProgram method and will be removed in v0.11.0. ExecArtifact(context.Context, *ExecArtifact_Args) (*ExecProgram_Result, error) // / Override KCL file with args. // / @@ -763,12 +763,12 @@ func (*UnimplementedKclvmServiceServer) ExecProgram(context.Context, *ExecProgra return nil, status.Errorf(codes.Unimplemented, "method ExecProgram not implemented") } -// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0. +// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecuteProgram method and will be removed in v0.11.0. func (*UnimplementedKclvmServiceServer) BuildProgram(context.Context, *BuildProgram_Args) (*BuildProgram_Result, error) { return nil, status.Errorf(codes.Unimplemented, "method BuildProgram not implemented") } -// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0. +// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecuteProgram method and will be removed in v0.11.0. func (*UnimplementedKclvmServiceServer) ExecArtifact(context.Context, *ExecArtifact_Args) (*ExecProgram_Result, error) { return nil, status.Errorf(codes.Unimplemented, "method ExecArtifact not implemented") } @@ -957,7 +957,7 @@ func _KclvmService_ExecProgram_Handler(srv interface{}, ctx context.Context, dec return interceptor(ctx, in, info, handler) } -// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0. +// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecuteProgram method and will be removed in v0.11.0. func _KclvmService_BuildProgram_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(BuildProgram_Args) if err := dec(in); err != nil { @@ -976,7 +976,7 @@ func _KclvmService_BuildProgram_Handler(srv interface{}, ctx context.Context, de return interceptor(ctx, in, info, handler) } -// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.11.0. +// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecuteProgram method and will be removed in v0.11.0. func _KclvmService_ExecArtifact_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(ExecArtifact_Args) if err := dec(in); err != nil {