Skip to content

Commit

Permalink
refactor: service client (#383)
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy authored Sep 16, 2024
1 parent 51da7c9 commit 858430e
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 132 deletions.
77 changes: 30 additions & 47 deletions pkg/service/rest_server.go → pkg/server/rest_server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build rpc || !cgo
// +build rpc !cgo

// Copyright The KCL Authors. All rights reserved.

package service
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
})
}
4 changes: 2 additions & 2 deletions pkg/service/client_kclvm_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pkg/service/grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
23 changes: 0 additions & 23 deletions pkg/service/rest_client_test.go

This file was deleted.

46 changes: 0 additions & 46 deletions pkg/service/testmain_test.go

This file was deleted.

9 changes: 9 additions & 0 deletions pkg/service/types.go
Original file line number Diff line number Diff line change
@@ -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"`
}
Loading

0 comments on commit 858430e

Please sign in to comment.