diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 502f1f92ff..69cba21154 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -6,7 +6,7 @@ jobs: unit-benchmark-test: strategy: matrix: - go: [ 1.17, 1.18, 1.19 ] + go: [ 1.15, 1.17, 1.18, 1.19 ] os: [ X64, ARM64 ] runs-on: ${{ matrix.os }} steps: @@ -34,7 +34,7 @@ jobs: scenario-test: strategy: matrix: - go: [ 1.17, 1.18 ] + go: [ 1.15, 1.18 ] runs-on: [ self-hosted, X64 ] steps: - uses: actions/checkout@v3 diff --git a/.licenserc.yaml b/.licenserc.yaml index 487bc8f3aa..99498ea88d 100644 --- a/.licenserc.yaml +++ b/.licenserc.yaml @@ -4,11 +4,14 @@ header: copyright-owner: CloudWeGo Authors paths: - - '**/*.go' - - '**/*.s' + - "**/*.go" + - "**/*.s" paths-ignore: - internal/mocks/thrift/test.go + - pkg/remote/codec/thrift/thrift_frugal.go + - pkg/remote/codec/thrift/thrift_frugal_test.go + - pkg/remote/codec/thrift/thrift_others.go - pkg/remote/trans/nphttp2/codes/codes.go - pkg/remote/trans/nphttp2/grpc/grpcframe/ - pkg/remote/trans/nphttp2/grpc/bdp_estimator.go diff --git a/go.mod b/go.mod index b1147f9b43..859280e193 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/cloudwego/kitex -go 1.16 +go 1.13 require ( github.com/apache/thrift v0.13.0 @@ -9,7 +9,7 @@ require ( github.com/cloudwego/fastpb v0.0.2 github.com/cloudwego/frugal v0.1.3 github.com/cloudwego/netpoll v0.2.6 - github.com/cloudwego/thriftgo v0.2.0 + github.com/cloudwego/thriftgo v0.2.1 github.com/golang/mock v1.6.0 github.com/jhump/protoreflect v1.8.2 github.com/json-iterator/go v1.1.12 diff --git a/go.sum b/go.sum index f827665689..b084f54895 100644 --- a/go.sum +++ b/go.sum @@ -23,8 +23,8 @@ github.com/cloudwego/netpoll v0.2.4/go.mod h1:1T2WVuQ+MQw6h6DpE45MohSvDTKdy2DlzC github.com/cloudwego/netpoll v0.2.6 h1:vzN8cyayoa9RdCOG87tqkYO/j2hA4SMLC+vkcNUq6uI= github.com/cloudwego/netpoll v0.2.6/go.mod h1:1T2WVuQ+MQw6h6DpE45MohSvDTKdy2DlzCx2KsnPI4E= github.com/cloudwego/thriftgo v0.1.2/go.mod h1:LzeafuLSiHA9JTiWC8TIMIq64iadeObgRUhmVG1OC/w= -github.com/cloudwego/thriftgo v0.2.0 h1:gDNxU1CmNkwwe/I9UfuCTIc1DXu5FVFa2cMWwT61e50= -github.com/cloudwego/thriftgo v0.2.0/go.mod h1:aZxiVX7KJdonZwwnapZEMDAAGUGk9WCIkNU3YoVjbpg= +github.com/cloudwego/thriftgo v0.2.1 h1:pKEZDDND14+kG4ILPCq0sqv3gidjxOnf/wLjOwxzBks= +github.com/cloudwego/thriftgo v0.2.1/go.mod h1:8i9AF5uDdWHGqzUhXDlubCjx4MEfKvWXGQlMWyR0tM4= github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= diff --git a/pkg/generic/httppbthrift_codec.go b/pkg/generic/httppbthrift_codec.go index f598c3e1de..919b548b4f 100644 --- a/pkg/generic/httppbthrift_codec.go +++ b/pkg/generic/httppbthrift_codec.go @@ -21,6 +21,7 @@ import ( "errors" "fmt" "io" + "io/ioutil" "net/http" "strings" "sync/atomic" @@ -168,7 +169,7 @@ func FromHTTPPbRequest(req *http.Request) (*HTTPRequest, error) { // body == nil if from Get request return customReq, nil } - if customReq.RawBody, err = io.ReadAll(b); err != nil { + if customReq.RawBody, err = ioutil.ReadAll(b); err != nil { return nil, err } if len(customReq.RawBody) == 0 { diff --git a/pkg/generic/httpthrift_codec.go b/pkg/generic/httpthrift_codec.go index c704e8f69f..593c341bba 100644 --- a/pkg/generic/httpthrift_codec.go +++ b/pkg/generic/httpthrift_codec.go @@ -21,6 +21,7 @@ import ( "errors" "fmt" "io" + "io/ioutil" "net/http" "sync/atomic" @@ -154,7 +155,7 @@ func FromHTTPRequest(req *http.Request) (*HTTPRequest, error) { // body == nil if from Get request return customReq, nil } - if customReq.RawBody, err = io.ReadAll(b); err != nil { + if customReq.RawBody, err = ioutil.ReadAll(b); err != nil { return nil, err } if len(customReq.RawBody) == 0 { diff --git a/pkg/remote/codec/thrift/thrift.go b/pkg/remote/codec/thrift/thrift.go index 3ee6aa08fe..38c8275b08 100644 --- a/pkg/remote/codec/thrift/thrift.go +++ b/pkg/remote/codec/thrift/thrift.go @@ -172,7 +172,7 @@ func (c thriftCodec) Unmarshal(ctx context.Context, message remote.Message, in r data := message.Data() // decode with hyper unmarshal - if c.hyperMarshalEnabled() && hyperMessageUnmarshalAvailable(data, message) { + if c.hyperMessageUnmarshalEnabled() && hyperMessageUnmarshalAvailable(data, message) { msgBeginLen := bthrift.Binary.MessageBeginLength(methodName, msgType, seqID) ri := message.RPCInfo() internal_stats.Record(ctx, ri, stats.WaitReadStart, nil) diff --git a/pkg/remote/codec/thrift/thrift_frugal.go b/pkg/remote/codec/thrift/thrift_frugal.go index e3964776e4..c9c114baee 100644 --- a/pkg/remote/codec/thrift/thrift_frugal.go +++ b/pkg/remote/codec/thrift/thrift_frugal.go @@ -1,5 +1,5 @@ -//go:build amd64 && !windows -// +build amd64,!windows +//go:build amd64 && !windows && !go1.13 && !go.14 +// +build amd64,!windows,!go1.13,!go.14 /* * Copyright 2021 CloudWeGo Authors diff --git a/pkg/remote/codec/thrift/thrift_frugal_test.go b/pkg/remote/codec/thrift/thrift_frugal_test.go index 92c73e14cf..a5794bf0fa 100644 --- a/pkg/remote/codec/thrift/thrift_frugal_test.go +++ b/pkg/remote/codec/thrift/thrift_frugal_test.go @@ -1,5 +1,5 @@ -//go:build amd64 && !windows -// +build amd64,!windows +//go:build amd64 && !windows && !go1.13 && !go.14 +// +build amd64,!windows,!go1.13,!go.14 // Copyright 2022 ByteDance Inc. // diff --git a/pkg/remote/codec/thrift/thrift_others.go b/pkg/remote/codec/thrift/thrift_others.go index 487995b83d..8309aad6b6 100644 --- a/pkg/remote/codec/thrift/thrift_others.go +++ b/pkg/remote/codec/thrift/thrift_others.go @@ -1,5 +1,5 @@ -//go:build !amd64 || windows -// +build !amd64 windows +//go:build !amd64 || windows || go1.13 || go1.14 +// +build !amd64 windows go1.13 go1.14 /* * Copyright 2021 CloudWeGo Authors diff --git a/pkg/remote/trans/gonet/conn_extension.go b/pkg/remote/trans/gonet/conn_extension.go index b239b3dbab..db8ffd338e 100644 --- a/pkg/remote/trans/gonet/conn_extension.go +++ b/pkg/remote/trans/gonet/conn_extension.go @@ -21,6 +21,7 @@ import ( "errors" "io" "net" + "strings" "syscall" "github.com/cloudwego/netpoll" @@ -75,8 +76,10 @@ func (e *gonetConnExtension) IsRemoteClosedErr(err error) bool { if err == nil { return false } - return errors.Is(err, net.ErrClosed) || - errors.Is(err, netpoll.ErrConnClosed) || + // strings.Contains(err.Error(), "closed network connection") change to errors.Is(err, net.ErrClosed) + // when support go version >= 1.16 + return errors.Is(err, netpoll.ErrConnClosed) || errors.Is(err, io.EOF) || - errors.Is(err, syscall.EPIPE) + errors.Is(err, syscall.EPIPE) || + strings.Contains(err.Error(), "closed network connection") } diff --git a/pkg/remote/trans/netpoll/http_client_handler.go b/pkg/remote/trans/netpoll/http_client_handler.go index 6fc6712df8..593b81c334 100644 --- a/pkg/remote/trans/netpoll/http_client_handler.go +++ b/pkg/remote/trans/netpoll/http_client_handler.go @@ -21,7 +21,7 @@ import ( "context" "errors" "fmt" - "io" + "io/ioutil" "net" "net/http" "path" @@ -279,7 +279,7 @@ func getBodyBufReader(buf remote.ByteBuffer) (remote.ByteBuffer, error) { if hr.StatusCode != http.StatusOK { return nil, fmt.Errorf("http response not OK, StatusCode: %d", hr.StatusCode) } - b, err := io.ReadAll(hr.Body) + b, err := ioutil.ReadAll(hr.Body) hr.Body.Close() if err != nil { return nil, fmt.Errorf("read http response body error:%w", err) diff --git a/pkg/utils/yaml.go b/pkg/utils/yaml.go index 16f8b80deb..61946128b1 100644 --- a/pkg/utils/yaml.go +++ b/pkg/utils/yaml.go @@ -17,7 +17,7 @@ package utils import ( - "io" + "io/ioutil" "os" "time" @@ -45,7 +45,7 @@ func ReadYamlConfigFile(yamlFile string) (*YamlConfig, error) { } defer fd.Close() - b, err := io.ReadAll(fd) + b, err := ioutil.ReadAll(fd) if err != nil { return nil, err } diff --git a/tool/internal_pkg/pluginmode/protoc/protoc.go b/tool/internal_pkg/pluginmode/protoc/protoc.go index da031940e0..f729cf0553 100644 --- a/tool/internal_pkg/pluginmode/protoc/protoc.go +++ b/tool/internal_pkg/pluginmode/protoc/protoc.go @@ -17,7 +17,7 @@ package protoc import ( "errors" "fmt" - "io" + "io/ioutil" "os" "path/filepath" "strings" @@ -44,7 +44,7 @@ func Run() int { func run(opts protogen.Options) error { // unmarshal request from stdin - in, err := io.ReadAll(os.Stdin) + in, err := ioutil.ReadAll(os.Stdin) if err != nil { return err } diff --git a/tool/internal_pkg/pluginmode/thriftgo/convertor.go b/tool/internal_pkg/pluginmode/thriftgo/convertor.go index 66543233ef..5e42fa7448 100644 --- a/tool/internal_pkg/pluginmode/thriftgo/convertor.go +++ b/tool/internal_pkg/pluginmode/thriftgo/convertor.go @@ -18,6 +18,7 @@ import ( "fmt" "go/format" "io" + "io/ioutil" "os" "path/filepath" "regexp" @@ -467,7 +468,7 @@ func (c *converter) persist(res *plugin.Response) error { if err := os.MkdirAll(path, 0o755); err != nil && !os.IsExist(err) { return fmt.Errorf("failed to create path '%s': %w", path, err) } - if err := os.WriteFile(full, content, 0o644); err != nil { + if err := ioutil.WriteFile(full, content, 0o644); err != nil { return fmt.Errorf("failed to write file '%s': %w", full, err) } } diff --git a/tool/internal_pkg/pluginmode/thriftgo/patcher.go b/tool/internal_pkg/pluginmode/thriftgo/patcher.go index e49ad95f7a..b7102a4151 100644 --- a/tool/internal_pkg/pluginmode/thriftgo/patcher.go +++ b/tool/internal_pkg/pluginmode/thriftgo/patcher.go @@ -16,7 +16,7 @@ package thriftgo import ( "fmt" - "os" + "io/ioutil" "path/filepath" "reflect" "sort" @@ -205,7 +205,7 @@ func (p *patcher) patch(req *plugin.Request) (patches []*plugin.Generated, err e }) if p.copyIDL { - content, err := os.ReadFile(ast.Filename) + content, err := ioutil.ReadFile(ast.Filename) if err != nil { return nil, fmt.Errorf("read %q: %w", ast.Filename, err) } diff --git a/tool/internal_pkg/pluginmode/thriftgo/plugin.go b/tool/internal_pkg/pluginmode/thriftgo/plugin.go index 965579ddae..653a157a3f 100644 --- a/tool/internal_pkg/pluginmode/thriftgo/plugin.go +++ b/tool/internal_pkg/pluginmode/thriftgo/plugin.go @@ -17,7 +17,7 @@ package thriftgo import ( "errors" "fmt" - "io" + "io/ioutil" "os" "github.com/cloudwego/thriftgo/plugin" @@ -34,7 +34,7 @@ const TheUseOptionMessage = "kitex_gen is not generated due to the -use option" // Run is an entry of the plugin mode of kitex for thriftgo. // It reads a plugin request from the standard input and writes out a response. func Run() int { - data, err := io.ReadAll(os.Stdin) + data, err := ioutil.ReadAll(os.Stdin) if err != nil { println("Failed to get input:", err.Error()) return 1 diff --git a/tool/internal_pkg/tpl/bootstrap.sh.go b/tool/internal_pkg/tpl/bootstrap.sh.go new file mode 100644 index 0000000000..1a8f2d2667 --- /dev/null +++ b/tool/internal_pkg/tpl/bootstrap.sh.go @@ -0,0 +1,39 @@ +// Copyright 2022 CloudWeGo Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tpl + +// BootstrapTpl is the template for generating bootstrap.sh. +var BootstrapTpl string = `#! /usr/bin/env bash +CURDIR=$(cd $(dirname $0); pwd) + +if [ "X$1" != "X" ]; then + RUNTIME_ROOT=$1 +else + RUNTIME_ROOT=${CURDIR} +fi + +export KITEX_RUNTIME_ROOT=$RUNTIME_ROOT +export KITEX_LOG_DIR="$RUNTIME_ROOT/log" + +if [ ! -d "$KITEX_LOG_DIR/app" ]; then + mkdir -p "$KITEX_LOG_DIR/app" +fi + +if [ ! -d "$KITEX_LOG_DIR/rpc" ]; then + mkdir -p "$KITEX_LOG_DIR/rpc" +fi + +exec "$CURDIR/bin/{{.RealServiceName}}" +` diff --git a/tool/internal_pkg/tpl/bootstrap.sh.tmpl b/tool/internal_pkg/tpl/bootstrap.sh.tmpl deleted file mode 100644 index a915fa84b0..0000000000 --- a/tool/internal_pkg/tpl/bootstrap.sh.tmpl +++ /dev/null @@ -1,21 +0,0 @@ -#! /usr/bin/env bash -CURDIR=$(cd $(dirname $0); pwd) - -if [ "X$1" != "X" ]; then - RUNTIME_ROOT=$1 -else - RUNTIME_ROOT=${CURDIR} -fi - -export KITEX_RUNTIME_ROOT=$RUNTIME_ROOT -export KITEX_LOG_DIR="$RUNTIME_ROOT/log" - -if [ ! -d "$KITEX_LOG_DIR/app" ]; then - mkdir -p "$KITEX_LOG_DIR/app" -fi - -if [ ! -d "$KITEX_LOG_DIR/rpc" ]; then - mkdir -p "$KITEX_LOG_DIR/rpc" -fi - -exec "$CURDIR/bin/{{.RealServiceName}}" diff --git a/tool/internal_pkg/tpl/build.sh.go b/tool/internal_pkg/tpl/build.sh.go new file mode 100644 index 0000000000..83804e8084 --- /dev/null +++ b/tool/internal_pkg/tpl/build.sh.go @@ -0,0 +1,30 @@ +// Copyright 2022 CloudWeGo Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tpl + +// BuildTpl is the template for generating build.sh. +var BuildTpl string = `#!/usr/bin/env bash +RUN_NAME="{{.RealServiceName}}" + +mkdir -p output/bin +cp script/* output/ +chmod +x output/bootstrap.sh + +if [ "$IS_SYSTEM_TEST_ENV" != "1" ]; then + go build -o output/bin/${RUN_NAME} +else + go test -c -covermode=set -o output/bin/${RUN_NAME} -coverpkg=./... +fi +` diff --git a/tool/internal_pkg/tpl/build.sh.tmpl b/tool/internal_pkg/tpl/build.sh.tmpl deleted file mode 100644 index 26ab9f2a1a..0000000000 --- a/tool/internal_pkg/tpl/build.sh.tmpl +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env bash -RUN_NAME="{{.RealServiceName}}" - -mkdir -p output/bin -cp script/* output/ -chmod +x output/bootstrap.sh - -if [ "$IS_SYSTEM_TEST_ENV" != "1" ]; then - go build -o output/bin/${RUN_NAME} -else - go test -c -covermode=set -o output/bin/${RUN_NAME} -coverpkg=./... -fi diff --git a/tool/internal_pkg/tpl/client.go.tmpl b/tool/internal_pkg/tpl/client.go similarity index 79% rename from tool/internal_pkg/tpl/client.go.tmpl rename to tool/internal_pkg/tpl/client.go index 7661e317c2..9d86784761 100644 --- a/tool/internal_pkg/tpl/client.go.tmpl +++ b/tool/internal_pkg/tpl/client.go @@ -1,4 +1,21 @@ -// Code generated by Kitex {{.Version}}. DO NOT EDIT. +// Copyright 2022 CloudWeGo Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tpl + +// ClientTpl is the template for generating client.go. +var ClientTpl string = `// Code generated by Kitex {{.Version}}. DO NOT EDIT. package {{ToLower .ServiceName}} import ( @@ -77,3 +94,4 @@ func (p *k{{$.ServiceName}}Client) {{.Name}}(ctx context.Context {{range .Args}} {{- end}} {{end}} {{template "@client.go-EOF" .}} +` diff --git a/tool/internal_pkg/tpl/handler.go b/tool/internal_pkg/tpl/handler.go new file mode 100644 index 0000000000..7227e301f3 --- /dev/null +++ b/tool/internal_pkg/tpl/handler.go @@ -0,0 +1,29 @@ +// Copyright 2022 CloudWeGo Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tpl + +// HandlerTpl is the template for generating handler.go. +var HandlerTpl string = `package main + +import ( + {{- range $path, $alias := .Imports}} + {{$alias }}"{{$path}}" + {{- end}} +) + +// {{.ServiceName}}Impl implements the last service interface defined in the IDL. +type {{.ServiceName}}Impl struct{} +{{template "HandlerMethod" .}} +` diff --git a/tool/internal_pkg/tpl/handler.go.tmpl b/tool/internal_pkg/tpl/handler.go.tmpl deleted file mode 100644 index 03c439f151..0000000000 --- a/tool/internal_pkg/tpl/handler.go.tmpl +++ /dev/null @@ -1,11 +0,0 @@ -package main - -import ( - {{- range $path, $alias := .Imports}} - {{$alias }}"{{$path}}" - {{- end}} -) - -// {{.ServiceName}}Impl implements the last service interface defined in the IDL. -type {{.ServiceName}}Impl struct{} -{{template "HandlerMethod" .}} diff --git a/tool/internal_pkg/tpl/handler.method.tmpl b/tool/internal_pkg/tpl/handler.method.go similarity index 54% rename from tool/internal_pkg/tpl/handler.method.tmpl rename to tool/internal_pkg/tpl/handler.method.go index cddb51f0f2..8cf009de9a 100644 --- a/tool/internal_pkg/tpl/handler.method.tmpl +++ b/tool/internal_pkg/tpl/handler.method.go @@ -1,4 +1,21 @@ -{{define "HandlerMethod"}} +// Copyright 2022 CloudWeGo Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tpl + +// HandlerMethodTpl is the template for generating methods in handler.go. +var HandlerMethodsTpl string = `{{define "HandlerMethod"}} {{range .AllMethods}} {{- if or .ClientStreaming .ServerStreaming}} func (s *{{$.ServiceName}}Impl) {{.Name}}({{if not .ClientStreaming}}{{range .Args}}{{LowerFirst .Name}} {{.Type}}, {{end}}{{end}}stream {{.PkgRefName}}.{{.ServiceName}}_{{.RawName}}Server) (err error) { @@ -22,3 +39,4 @@ func (s *{{$.ServiceName}}Impl) {{.Name}}(ctx context.Context {{range .Args}}, { {{end}} {{end}} {{end}}{{/* define "HandlerMethod" */}} +` diff --git a/tool/internal_pkg/tpl/invoker.go b/tool/internal_pkg/tpl/invoker.go new file mode 100644 index 0000000000..6cd9b2ccba --- /dev/null +++ b/tool/internal_pkg/tpl/invoker.go @@ -0,0 +1,44 @@ +// Copyright 2022 CloudWeGo Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tpl + +// InvokerTpl is the template for generating invoker.go. +var InvokerTpl string = `// Code generated by Kitex {{.Version}}. DO NOT EDIT. + +package {{ToLower .ServiceName}} + +import ( + {{- range $path, $alias := .Imports}} + {{$alias }}"{{$path}}" + {{- end}} +) + +// NewInvoker creates a server.Invoker with the given handler and options. +func NewInvoker(handler {{call .ServiceTypeName}}, opts ...server.Option) server.Invoker { + var options []server.Option + {{template "@invoker.go-NewInvoker-option" .}} + options = append(options, opts...) + + s := server.NewInvoker(options...) + if err := s.RegisterService(serviceInfo(), handler); err != nil { + panic(err) + } + if err := s.Init(); err != nil { + panic(err) + } + return s +} +{{template "@invoker.go-EOF" .}} +` diff --git a/tool/internal_pkg/tpl/invoker.go.tmpl b/tool/internal_pkg/tpl/invoker.go.tmpl deleted file mode 100644 index 37f262c86d..0000000000 --- a/tool/internal_pkg/tpl/invoker.go.tmpl +++ /dev/null @@ -1,26 +0,0 @@ -// Code generated by Kitex {{.Version}}. DO NOT EDIT. - -package {{ToLower .ServiceName}} - -import ( - {{- range $path, $alias := .Imports}} - {{$alias }}"{{$path}}" - {{- end}} -) - -// NewInvoker creates a server.Invoker with the given handler and options. -func NewInvoker(handler {{call .ServiceTypeName}}, opts ...server.Option) server.Invoker { - var options []server.Option - {{template "@invoker.go-NewInvoker-option" .}} - options = append(options, opts...) - - s := server.NewInvoker(options...) - if err := s.RegisterService(serviceInfo(), handler); err != nil { - panic(err) - } - if err := s.Init(); err != nil { - panic(err) - } - return s -} -{{template "@invoker.go-EOF" .}} diff --git a/tool/internal_pkg/tpl/main.go b/tool/internal_pkg/tpl/main.go new file mode 100644 index 0000000000..347eef630d --- /dev/null +++ b/tool/internal_pkg/tpl/main.go @@ -0,0 +1,35 @@ +// Copyright 2022 CloudWeGo Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tpl + +// MainTpl is the template for generating main.go +var MainTpl string = `package main + +import ( + {{- range $path, $alias := .Imports}} + {{$alias }}"{{$path}}" + {{- end}} +) + +func main() { + svr := {{.PkgRefName}}.NewServer(new({{.ServiceName}}Impl)) + + err := svr.Run() + + if err != nil { + log.Println(err.Error()) + } +} +` diff --git a/tool/internal_pkg/tpl/main.go.tmpl b/tool/internal_pkg/tpl/main.go.tmpl deleted file mode 100644 index c8203daec9..0000000000 --- a/tool/internal_pkg/tpl/main.go.tmpl +++ /dev/null @@ -1,17 +0,0 @@ -package main - -import ( - {{- range $path, $alias := .Imports}} - {{$alias }}"{{$path}}" - {{- end}} -) - -func main() { - svr := {{.PkgRefName}}.NewServer(new({{.ServiceName}}Impl)) - - err := svr.Run() - - if err != nil { - log.Println(err.Error()) - } -} diff --git a/tool/internal_pkg/tpl/server.go b/tool/internal_pkg/tpl/server.go new file mode 100644 index 0000000000..6f1a5474ef --- /dev/null +++ b/tool/internal_pkg/tpl/server.go @@ -0,0 +1,40 @@ +// Copyright 2022 CloudWeGo Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tpl + +// ServerTpl is the template for generating server.go. +var ServerTpl string = `// Code generated by Kitex {{.Version}}. DO NOT EDIT. +package {{ToLower .ServiceName}} + +import ( + {{- range $path, $alias := .Imports}} + {{$alias }}"{{$path}}" + {{- end}} +) + +// NewServer creates a server.Server with the given handler and options. +func NewServer(handler {{call .ServiceTypeName}}, opts ...server.Option) server.Server { + var options []server.Option + {{template "@server.go-NewServer-option" .}} + options = append(options, opts...) + + svr := server.NewServer(options...) + if err := svr.RegisterService(serviceInfo(), handler); err != nil { + panic(err) + } + return svr +} +{{template "@server.go-EOF" .}} +` diff --git a/tool/internal_pkg/tpl/server.go.tmpl b/tool/internal_pkg/tpl/server.go.tmpl deleted file mode 100644 index 398d355e40..0000000000 --- a/tool/internal_pkg/tpl/server.go.tmpl +++ /dev/null @@ -1,22 +0,0 @@ -// Code generated by Kitex {{.Version}}. DO NOT EDIT. -package {{ToLower .ServiceName}} - -import ( - {{- range $path, $alias := .Imports}} - {{$alias }}"{{$path}}" - {{- end}} -) - -// NewServer creates a server.Server with the given handler and options. -func NewServer(handler {{call .ServiceTypeName}}, opts ...server.Option) server.Server { - var options []server.Option - {{template "@server.go-NewServer-option" .}} - options = append(options, opts...) - - svr := server.NewServer(options...) - if err := svr.RegisterService(serviceInfo(), handler); err != nil { - panic(err) - } - return svr -} -{{template "@server.go-EOF" .}} diff --git a/tool/internal_pkg/tpl/service.go.tmpl b/tool/internal_pkg/tpl/service.go similarity index 93% rename from tool/internal_pkg/tpl/service.go.tmpl rename to tool/internal_pkg/tpl/service.go index 5db87095d9..652c50a891 100644 --- a/tool/internal_pkg/tpl/service.go.tmpl +++ b/tool/internal_pkg/tpl/service.go @@ -1,4 +1,21 @@ -// Code generated by Kitex {{.Version}}. DO NOT EDIT. +// Copyright 2022 CloudWeGo Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tpl + +// ServiceTpl is the template for generating the servicename.go source. +var ServiceTpl string = `// Code generated by Kitex {{.Version}}. DO NOT EDIT. package {{ToLower .ServiceName}} @@ -386,3 +403,4 @@ func (p *kClient) {{.Name}}(ctx context.Context {{range .Args}}, {{.RawName}} {{ } {{- end}} {{end}} +` diff --git a/tool/internal_pkg/tpl/templates.go b/tool/internal_pkg/tpl/templates.go deleted file mode 100644 index fbe7a2c565..0000000000 --- a/tool/internal_pkg/tpl/templates.go +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright 2022 CloudWeGo Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package tpl - -import _ "embed" - -// BootstrapTpl is the template for generating bootstrap.sh. -// -//go:embed bootstrap.sh.tmpl -var BootstrapTpl string - -// BuildTpl is the template for generating build.sh. -// -//go:embed build.sh.tmpl -var BuildTpl string - -// ClientTpl is the template for generating client.go. -// -//go:embed client.go.tmpl -var ClientTpl string - -// HandlerTpl is the template for generating handler.go. -// -//go:embed handler.go.tmpl -var HandlerTpl string - -// HandlerMethodTpl is the template for generating methods in handler.go. -// -//go:embed handler.method.tmpl -var HandlerMethodsTpl string - -// InvokerTpl is the template for generating invoker.go. -// -//go:embed invoker.go.tmpl -var InvokerTpl string - -// MainTpl is the template for generating main.go. -// -//go:embed main.go.tmpl -var MainTpl string - -// ServerTpl is the template for generating server.go. -// -//go:embed server.go.tmpl -var ServerTpl string - -// ServiceTpl is the template for generating the servicename.go source. -// -//go:embed service.go.tmpl -var ServiceTpl string diff --git a/tool/internal_pkg/util/util.go b/tool/internal_pkg/util/util.go index e034cb1e74..c4da49667c 100644 --- a/tool/internal_pkg/util/util.go +++ b/tool/internal_pkg/util/util.go @@ -18,6 +18,7 @@ import ( "fmt" "go/build" "go/format" + "io/ioutil" "os" "os/exec" "path/filepath" @@ -110,7 +111,7 @@ func NotPtr(s string) string { func SearchGoMod(cwd string) (moduleName, path string, found bool) { for { path = filepath.Join(cwd, "go.mod") - data, err := os.ReadFile(path) + data, err := ioutil.ReadFile(path) if err == nil { re := regexp.MustCompile(`^\s*module\s+(\S+)\s*`) for _, line := range strings.Split(string(data), "\n") { diff --git a/version.go b/version.go index 5dd5e99900..82252ac06b 100644 --- a/version.go +++ b/version.go @@ -19,5 +19,5 @@ package kitex // Name and Version info of this framework, used for statistics and debug const ( Name = "Kitex" - Version = "v0.4.1" + Version = "v0.4.2" )