From 9b0b958f43e99dfeaa398e83b5dd2699850ab269 Mon Sep 17 00:00:00 2001 From: kim Date: Tue, 11 Aug 2020 11:07:47 +0800 Subject: [PATCH 01/20] fix windows slash --- tools/goctl/api/gogen/util.go | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/goctl/api/gogen/util.go b/tools/goctl/api/gogen/util.go index 5b3b8274..55b65ff3 100644 --- a/tools/goctl/api/gogen/util.go +++ b/tools/goctl/api/gogen/util.go @@ -22,6 +22,7 @@ func getParentPackage(dir string) (string, error) { if err != nil { return "", err } + absDir = strings.ReplaceAll(absDir, `\`, `/`) var rootPath string var tempPath = absDir From f77c73eec1853f75068d94be1f66cebd283680bf Mon Sep 17 00:00:00 2001 From: kevin Date: Tue, 11 Aug 2020 12:39:09 +0800 Subject: [PATCH 02/20] remove unused method --- tools/goctl/api/gogen/util.go | 2 +- tools/goctl/util/path.go | 14 -------------- 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/tools/goctl/api/gogen/util.go b/tools/goctl/api/gogen/util.go index 55b65ff3..b89b787b 100644 --- a/tools/goctl/api/gogen/util.go +++ b/tools/goctl/api/gogen/util.go @@ -22,8 +22,8 @@ func getParentPackage(dir string) (string, error) { if err != nil { return "", err } - absDir = strings.ReplaceAll(absDir, `\`, `/`) + absDir = strings.ReplaceAll(absDir, `\`, `/`) var rootPath string var tempPath = absDir var hasGoMod = false diff --git a/tools/goctl/util/path.go b/tools/goctl/util/path.go index 8446b4bb..9c092688 100644 --- a/tools/goctl/util/path.go +++ b/tools/goctl/util/path.go @@ -4,7 +4,6 @@ import ( "fmt" "os" "path" - "path/filepath" "strings" "github.com/tal-tech/go-zero/tools/goctl/vars" @@ -44,16 +43,3 @@ func PathFromGoSrc() (string, error) { // skip slash return dir[len(parent)+1:], nil } - -func GetParentPackage(dir string) (string, error) { - absDir, err := filepath.Abs(dir) - if err != nil { - return "", err - } - pos := strings.Index(absDir, vars.ProjectName) - if pos < 0 { - return "", fmt.Errorf("error dir:[%s],please make sure that your project is in the %s directory", vars.ProjectName, dir) - } - - return absDir[pos:], nil -} From 4f59fd306a7e1e625f0be3c4ab77b1797fb752a0 Mon Sep 17 00:00:00 2001 From: kevin Date: Tue, 11 Aug 2020 12:44:21 +0800 Subject: [PATCH 03/20] use fmt.Println instead of println --- example/periodicalexecutor/pe.go | 3 ++- tools/goctl/api/gogen/util.go | 3 +-- tools/goctl/api/javagen/genpacket.go | 4 +++- tools/goctl/api/tsgen/genpacket.go | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/example/periodicalexecutor/pe.go b/example/periodicalexecutor/pe.go index 3be333c8..fbaa5a94 100644 --- a/example/periodicalexecutor/pe.go +++ b/example/periodicalexecutor/pe.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "time" "github.com/tal-tech/go-zero/core/executors" @@ -8,7 +9,7 @@ import ( func main() { exeutor := executors.NewBulkExecutor(func(items []interface{}) { - println(len(items)) + fmt.Println(len(items)) }, executors.WithBulkTasks(10)) for { exeutor.Add(1) diff --git a/tools/goctl/api/gogen/util.go b/tools/goctl/api/gogen/util.go index b89b787b..44d8a6fd 100644 --- a/tools/goctl/api/gogen/util.go +++ b/tools/goctl/api/gogen/util.go @@ -44,8 +44,7 @@ func getParentPackage(dir string) (string, error) { parent := path.Join(gopath, "src") pos := strings.Index(absDir, parent) if pos < 0 { - message := fmt.Sprintf("%s not in gomod project path, or not in GOPATH of %s directory", absDir, gopath) - println(message) + fmt.Printf("%s not in gomod project path, or not in GOPATH of %s directory\n", absDir, gopath) tempPath = filepath.Dir(absDir) rootPath = absDir[len(tempPath)+1:] } else { diff --git a/tools/goctl/api/javagen/genpacket.go b/tools/goctl/api/javagen/genpacket.go index 83497bc2..721eeddc 100644 --- a/tools/goctl/api/javagen/genpacket.go +++ b/tools/goctl/api/javagen/genpacket.go @@ -175,7 +175,7 @@ func formatFile(tmplBytes *bytes.Buffer, file *os.File) { builder.WriteString(scanner.Text() + "\n") } if err := scanner.Err(); err != nil { - println(err) + fmt.Println(err) } } @@ -268,10 +268,12 @@ func genType(writer io.Writer, tp spec.Type) error { return err } } + writeBreakline(writer) writeIndent(writer, 1) genGetSet(writer, tp, 2) writeIndent(writer, 1) fmt.Fprintln(writer, "}") + return nil } diff --git a/tools/goctl/api/tsgen/genpacket.go b/tools/goctl/api/tsgen/genpacket.go index 2f7e9cd8..159c8c6a 100644 --- a/tools/goctl/api/tsgen/genpacket.go +++ b/tools/goctl/api/tsgen/genpacket.go @@ -156,7 +156,7 @@ func paramsForRoute(route spec.Route, prefixForType func(string) string) string hasBody := hasRequestBody(route) rt, err := goTypeToTs(route.RequestType.Name, prefixForType) if err != nil { - println(err.Error()) + fmt.Println(err.Error()) return "" } if hasParams && hasBody { From d1529fced8863f3e2b3d2ffd6cfd1dc92086e0b1 Mon Sep 17 00:00:00 2001 From: kevin Date: Tue, 11 Aug 2020 13:47:22 +0800 Subject: [PATCH 04/20] move auth interceptor into serverinterceptors --- rpcx/internal/{ => serverinterceptors}/authinterceptor.go | 2 +- rpcx/server.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename rpcx/internal/{ => serverinterceptors}/authinterceptor.go (96%) diff --git a/rpcx/internal/authinterceptor.go b/rpcx/internal/serverinterceptors/authinterceptor.go similarity index 96% rename from rpcx/internal/authinterceptor.go rename to rpcx/internal/serverinterceptors/authinterceptor.go index d59e0158..7cea029f 100644 --- a/rpcx/internal/authinterceptor.go +++ b/rpcx/internal/serverinterceptors/authinterceptor.go @@ -1,4 +1,4 @@ -package internal +package serverinterceptors import ( "context" diff --git a/rpcx/server.go b/rpcx/server.go index 76dc086f..040808bb 100644 --- a/rpcx/server.go +++ b/rpcx/server.go @@ -117,8 +117,8 @@ func setupInterceptors(server internal.Server, c RpcServerConf, metrics *stat.Me return err } - server.AddStreamInterceptors(internal.StreamAuthorizeInterceptor(authenticator)) - server.AddUnaryInterceptors(internal.UnaryAuthorizeInterceptor(authenticator)) + server.AddStreamInterceptors(serverinterceptors.StreamAuthorizeInterceptor(authenticator)) + server.AddUnaryInterceptors(serverinterceptors.UnaryAuthorizeInterceptor(authenticator)) } return nil From 10cd6053bceca61517e0d34a1e5b9853ccbd870b Mon Sep 17 00:00:00 2001 From: kevin Date: Tue, 11 Aug 2020 14:33:10 +0800 Subject: [PATCH 05/20] refactor rpcx, export WithDialOption and WithTimeout --- .../graceful/dns/api/svc/servicecontext.go | 2 +- .../graceful/etcd/api/svc/servicecontext.go | 2 +- example/tracing/edge/main.go | 2 +- example/tracing/portal/server.go | 4 +-- rpcx/client.go | 29 +++++++++++++------ rpcx/internal/client.go | 4 --- rpcx/proxy.go | 4 +-- 7 files changed, 27 insertions(+), 20 deletions(-) diff --git a/example/graceful/dns/api/svc/servicecontext.go b/example/graceful/dns/api/svc/servicecontext.go index 7bd478e0..42d8d4b7 100644 --- a/example/graceful/dns/api/svc/servicecontext.go +++ b/example/graceful/dns/api/svc/servicecontext.go @@ -3,5 +3,5 @@ package svc import "github.com/tal-tech/go-zero/rpcx" type ServiceContext struct { - Client *rpcx.RpcClient + Client rpcx.Client } diff --git a/example/graceful/etcd/api/svc/servicecontext.go b/example/graceful/etcd/api/svc/servicecontext.go index 7bd478e0..42d8d4b7 100644 --- a/example/graceful/etcd/api/svc/servicecontext.go +++ b/example/graceful/etcd/api/svc/servicecontext.go @@ -3,5 +3,5 @@ package svc import "github.com/tal-tech/go-zero/rpcx" type ServiceContext struct { - Client *rpcx.RpcClient + Client rpcx.Client } diff --git a/example/tracing/edge/main.go b/example/tracing/edge/main.go index ce43a02a..558ba94b 100644 --- a/example/tracing/edge/main.go +++ b/example/tracing/edge/main.go @@ -15,7 +15,7 @@ import ( var ( configFile = flag.String("f", "config.json", "the config file") - client *rpcx.RpcClient + client rpcx.Client ) func handle(w http.ResponseWriter, r *http.Request) { diff --git a/example/tracing/portal/server.go b/example/tracing/portal/server.go index 47198344..6c5e0c4d 100644 --- a/example/tracing/portal/server.go +++ b/example/tracing/portal/server.go @@ -20,11 +20,11 @@ type ( } PortalServer struct { - userRpc *rpcx.RpcClient + userRpc rpcx.Client } ) -func NewPortalServer(client *rpcx.RpcClient) *PortalServer { +func NewPortalServer(client rpcx.Client) *PortalServer { return &PortalServer{ userRpc: client, } diff --git a/rpcx/client.go b/rpcx/client.go index cb523951..7f589d56 100644 --- a/rpcx/client.go +++ b/rpcx/client.go @@ -10,11 +10,22 @@ import ( "google.golang.org/grpc" ) -type RpcClient struct { - client internal.Client -} +var ( + WithDialOption = internal.WithDialOption + WithTimeout = internal.WithTimeout +) + +type ( + Client interface { + Conn() *grpc.ClientConn + } + + RpcClient struct { + client Client + } +) -func MustNewClient(c RpcClientConf, options ...internal.ClientOption) *RpcClient { +func MustNewClient(c RpcClientConf, options ...internal.ClientOption) Client { cli, err := NewClient(c, options...) if err != nil { log.Fatal(err) @@ -23,20 +34,20 @@ func MustNewClient(c RpcClientConf, options ...internal.ClientOption) *RpcClient return cli } -func NewClient(c RpcClientConf, options ...internal.ClientOption) (*RpcClient, error) { +func NewClient(c RpcClientConf, options ...internal.ClientOption) (Client, error) { var opts []internal.ClientOption if c.HasCredential() { - opts = append(opts, internal.WithDialOption(grpc.WithPerRPCCredentials(&auth.Credential{ + opts = append(opts, WithDialOption(grpc.WithPerRPCCredentials(&auth.Credential{ App: c.App, Token: c.Token, }))) } if c.Timeout > 0 { - opts = append(opts, internal.WithTimeout(time.Duration(c.Timeout)*time.Millisecond)) + opts = append(opts, WithTimeout(time.Duration(c.Timeout)*time.Millisecond)) } opts = append(opts, options...) - var client internal.Client + var client Client var err error if len(c.Server) > 0 { client, err = internal.NewDirectClient(c.Server, opts...) @@ -52,7 +63,7 @@ func NewClient(c RpcClientConf, options ...internal.ClientOption) (*RpcClient, e }, nil } -func NewClientNoAuth(c discov.EtcdConf) (*RpcClient, error) { +func NewClientNoAuth(c discov.EtcdConf) (Client, error) { client, err := internal.NewDiscovClient(c.Hosts, c.Key) if err != nil { return nil, err diff --git a/rpcx/internal/client.go b/rpcx/internal/client.go index 9576f261..1b799787 100644 --- a/rpcx/internal/client.go +++ b/rpcx/internal/client.go @@ -18,10 +18,6 @@ type ( } ClientOption func(options *ClientOptions) - - Client interface { - Conn() *grpc.ClientConn - } ) func WithDialOption(opt grpc.DialOption) ClientOption { diff --git a/rpcx/proxy.go b/rpcx/proxy.go index e4c90d0a..dfdff273 100644 --- a/rpcx/proxy.go +++ b/rpcx/proxy.go @@ -12,7 +12,7 @@ import ( type RpcProxy struct { backend string - clients map[string]*RpcClient + clients map[string]Client options []internal.ClientOption sharedCalls syncx.SharedCalls lock sync.Mutex @@ -21,7 +21,7 @@ type RpcProxy struct { func NewRpcProxy(backend string, opts ...internal.ClientOption) *RpcProxy { return &RpcProxy{ backend: backend, - clients: make(map[string]*RpcClient), + clients: make(map[string]Client), options: opts, sharedCalls: syncx.NewSharedCalls(), } From d117e31993549e078f3d0a79deb6ad350d285b38 Mon Sep 17 00:00:00 2001 From: kevin Date: Tue, 11 Aug 2020 17:18:51 +0800 Subject: [PATCH 06/20] use strings.Contains instead of strings.Index --- core/logx/logs_test.go | 2 +- rest/httpx/requests.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/logx/logs_test.go b/core/logx/logs_test.go index cb3e6d8a..d8cb7dbc 100644 --- a/core/logx/logs_test.go +++ b/core/logx/logs_test.go @@ -37,7 +37,7 @@ func (mw *mockWriter) Reset() { } func (mw *mockWriter) Contains(text string) bool { - return strings.Index(mw.builder.String(), text) > -1 + return strings.Contains(mw.builder.String(), text) } func TestFileLineFileMode(t *testing.T) { diff --git a/rest/httpx/requests.go b/rest/httpx/requests.go index e62f6842..115cd11f 100644 --- a/rest/httpx/requests.go +++ b/rest/httpx/requests.go @@ -39,7 +39,7 @@ func Parse(r *http.Request, v interface{}) error { // Parses the form request. func ParseForm(r *http.Request, v interface{}) error { - if strings.Index(r.Header.Get(ContentType), multipartFormData) != -1 { + if strings.Contains(r.Header.Get(ContentType), multipartFormData) { if err := r.ParseMultipartForm(maxMemory); err != nil { return err } @@ -107,5 +107,5 @@ func ParsePath(r *http.Request, v interface{}) error { } func withJsonBody(r *http.Request) bool { - return r.ContentLength > 0 && strings.Index(r.Header.Get(ContentType), ApplicationJson) != -1 + return r.ContentLength > 0 && strings.Contains(r.Header.Get(ContentType), ApplicationJson) } From 19888b7d110340c1d069649df59a6afbc9fbf7ca Mon Sep 17 00:00:00 2001 From: kingxt Date: Wed, 12 Aug 2020 10:23:49 +0800 Subject: [PATCH 07/20] fix windows bug --- tools/goctl/api/gogen/util.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/goctl/api/gogen/util.go b/tools/goctl/api/gogen/util.go index 44d8a6fd..1306d1b8 100644 --- a/tools/goctl/api/gogen/util.go +++ b/tools/goctl/api/gogen/util.go @@ -28,6 +28,9 @@ func getParentPackage(dir string) (string, error) { var tempPath = absDir var hasGoMod = false for { + if tempPath == filepath.Dir(tempPath) { + break + } tempPath = filepath.Dir(tempPath) if goctlutil.FileExists(filepath.Join(tempPath, goModeIdentifier)) { tempPath = filepath.Dir(tempPath) From 63c7f44a5fd43911997d384e9f70da95f3682943 Mon Sep 17 00:00:00 2001 From: kingxt Date: Wed, 12 Aug 2020 10:50:17 +0800 Subject: [PATCH 08/20] update readme.md --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 07102d79..b3a503ff 100644 --- a/readme.md +++ b/readme.md @@ -144,7 +144,7 @@ go-zero是一个集成了各种工程实践的包含web和rpc框架,有如下 8 directories, 9 files ``` - + 如果使用的是gomod依赖管理,需要在greet项目目录下面执行 `go mod init greet` 初始化gomod。 生成的代码可以直接运行: ```shell From 47440964cd0996d3ca598236f6ca14bd24dca15e Mon Sep 17 00:00:00 2001 From: kim Date: Tue, 11 Aug 2020 11:07:47 +0800 Subject: [PATCH 09/20] fix windows slash --- tools/goctl/api/gogen/util.go | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/goctl/api/gogen/util.go b/tools/goctl/api/gogen/util.go index 1306d1b8..8c513fa2 100644 --- a/tools/goctl/api/gogen/util.go +++ b/tools/goctl/api/gogen/util.go @@ -22,6 +22,7 @@ func getParentPackage(dir string) (string, error) { if err != nil { return "", err } + absDir = strings.ReplaceAll(absDir, `\`, `/`) absDir = strings.ReplaceAll(absDir, `\`, `/`) var rootPath string From 7da31921c783a22c674e0192faf5328228cf4641 Mon Sep 17 00:00:00 2001 From: kevin Date: Tue, 11 Aug 2020 12:39:09 +0800 Subject: [PATCH 10/20] remove unused method --- tools/goctl/api/gogen/util.go | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/goctl/api/gogen/util.go b/tools/goctl/api/gogen/util.go index 8c513fa2..1306d1b8 100644 --- a/tools/goctl/api/gogen/util.go +++ b/tools/goctl/api/gogen/util.go @@ -22,7 +22,6 @@ func getParentPackage(dir string) (string, error) { if err != nil { return "", err } - absDir = strings.ReplaceAll(absDir, `\`, `/`) absDir = strings.ReplaceAll(absDir, `\`, `/`) var rootPath string From d4097af627c79f200b574a6976884b04212d4487 Mon Sep 17 00:00:00 2001 From: kevin Date: Wed, 12 Aug 2020 12:12:31 +0800 Subject: [PATCH 11/20] export token parser for refresh token service --- rest/handler/authhandler.go | 4 ++-- rest/{internal => token}/tokenparser.go | 2 +- rest/{internal => token}/tokenparser_test.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) rename rest/{internal => token}/tokenparser.go (99%) rename rest/{internal => token}/tokenparser_test.go (99%) diff --git a/rest/handler/authhandler.go b/rest/handler/authhandler.go index 253dc503..5ed6caf1 100644 --- a/rest/handler/authhandler.go +++ b/rest/handler/authhandler.go @@ -8,7 +8,7 @@ import ( "github.com/dgrijalva/jwt-go" "github.com/tal-tech/go-zero/core/logx" - "github.com/tal-tech/go-zero/rest/internal" + "github.com/tal-tech/go-zero/rest/token" ) const ( @@ -43,7 +43,7 @@ func Authorize(secret string, opts ...AuthorizeOption) func(http.Handler) http.H opt(&authOpts) } - parser := internal.NewTokenParser() + parser := token.NewTokenParser() return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { token, err := parser.ParseToken(r, secret, authOpts.PrevSecret) diff --git a/rest/internal/tokenparser.go b/rest/token/tokenparser.go similarity index 99% rename from rest/internal/tokenparser.go rename to rest/token/tokenparser.go index d2eb9436..f2927b16 100644 --- a/rest/internal/tokenparser.go +++ b/rest/token/tokenparser.go @@ -1,4 +1,4 @@ -package internal +package token import ( "net/http" diff --git a/rest/internal/tokenparser_test.go b/rest/token/tokenparser_test.go similarity index 99% rename from rest/internal/tokenparser_test.go rename to rest/token/tokenparser_test.go index d486adcc..215b4db5 100644 --- a/rest/internal/tokenparser_test.go +++ b/rest/token/tokenparser_test.go @@ -1,4 +1,4 @@ -package internal +package token import ( "net/http" From c637f86817e07ecb33a5975fc75957c396170c22 Mon Sep 17 00:00:00 2001 From: kevin Date: Wed, 12 Aug 2020 12:16:36 +0800 Subject: [PATCH 12/20] export router --- rest/ngin_test.go | 2 +- rest/{internal => }/router/patrouter.go | 0 rest/{internal => }/router/patrouter_test.go | 0 rest/server.go | 2 +- 4 files changed, 2 insertions(+), 2 deletions(-) rename rest/{internal => }/router/patrouter.go (100%) rename rest/{internal => }/router/patrouter_test.go (100%) diff --git a/rest/ngin_test.go b/rest/ngin_test.go index a4591197..01929454 100644 --- a/rest/ngin_test.go +++ b/rest/ngin_test.go @@ -9,7 +9,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/tal-tech/go-zero/rest/httpx" - "github.com/tal-tech/go-zero/rest/internal/router" + "github.com/tal-tech/go-zero/rest/router" ) func TestWithMiddleware(t *testing.T) { diff --git a/rest/internal/router/patrouter.go b/rest/router/patrouter.go similarity index 100% rename from rest/internal/router/patrouter.go rename to rest/router/patrouter.go diff --git a/rest/internal/router/patrouter_test.go b/rest/router/patrouter_test.go similarity index 100% rename from rest/internal/router/patrouter_test.go rename to rest/router/patrouter_test.go diff --git a/rest/server.go b/rest/server.go index 005f3692..69496868 100644 --- a/rest/server.go +++ b/rest/server.go @@ -13,7 +13,7 @@ import ( "github.com/tal-tech/go-zero/rest/handler" "github.com/tal-tech/go-zero/rest/httpx" "github.com/tal-tech/go-zero/rest/internal" - "github.com/tal-tech/go-zero/rest/internal/router" + "github.com/tal-tech/go-zero/rest/router" ) // use 1000m to represent 100% From 9428fface2dccc882c0018457007acdb609a3673 Mon Sep 17 00:00:00 2001 From: kevin Date: Wed, 12 Aug 2020 12:25:52 +0800 Subject: [PATCH 13/20] export httpx.GetRemoteAddr --- rest/handler/breakerhandler.go | 4 ++-- rest/handler/loghandler.go | 5 +++-- rest/handler/sheddinghandler.go | 4 ++-- rest/{internal => httpx}/util.go | 2 +- rest/{internal => httpx}/util_test.go | 3 +-- rest/internal/log.go | 3 ++- 6 files changed, 11 insertions(+), 10 deletions(-) rename rest/{internal => httpx}/util.go (93%) rename rest/{internal => httpx}/util_test.go (94%) diff --git a/rest/handler/breakerhandler.go b/rest/handler/breakerhandler.go index 6e10a56d..846e68be 100644 --- a/rest/handler/breakerhandler.go +++ b/rest/handler/breakerhandler.go @@ -8,7 +8,7 @@ import ( "github.com/tal-tech/go-zero/core/breaker" "github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/core/stat" - "github.com/tal-tech/go-zero/rest/internal" + "github.com/tal-tech/go-zero/rest/httpx" "github.com/tal-tech/go-zero/rest/internal/security" ) @@ -22,7 +22,7 @@ func BreakerHandler(method, path string, metrics *stat.Metrics) func(http.Handle if err != nil { metrics.AddDrop() logx.Errorf("[http] dropped, %s - %s - %s", - r.RequestURI, internal.GetRemoteAddr(r), r.UserAgent()) + r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent()) w.WriteHeader(http.StatusServiceUnavailable) return } diff --git a/rest/handler/loghandler.go b/rest/handler/loghandler.go index 577c1c56..96157215 100644 --- a/rest/handler/loghandler.go +++ b/rest/handler/loghandler.go @@ -13,6 +13,7 @@ import ( "github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/core/timex" "github.com/tal-tech/go-zero/core/utils" + "github.com/tal-tech/go-zero/rest/httpx" "github.com/tal-tech/go-zero/rest/internal" ) @@ -112,10 +113,10 @@ func logBrief(r *http.Request, code int, timer *utils.ElapsedTimer, logs *intern var buf bytes.Buffer duration := timer.Duration() buf.WriteString(fmt.Sprintf("%d - %s - %s - %s - %s", - code, r.RequestURI, internal.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration))) + code, r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration))) if duration > slowThreshold { logx.Slowf("[HTTP] %d - %s - %s - %s - slowcall(%s)", - code, r.RequestURI, internal.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration)) + code, r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration)) } ok := isOkResponse(code) diff --git a/rest/handler/sheddinghandler.go b/rest/handler/sheddinghandler.go index fa70f797..568e7887 100644 --- a/rest/handler/sheddinghandler.go +++ b/rest/handler/sheddinghandler.go @@ -7,7 +7,7 @@ import ( "github.com/tal-tech/go-zero/core/load" "github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/core/stat" - "github.com/tal-tech/go-zero/rest/internal" + "github.com/tal-tech/go-zero/rest/httpx" "github.com/tal-tech/go-zero/rest/internal/security" ) @@ -35,7 +35,7 @@ func SheddingHandler(shedder load.Shedder, metrics *stat.Metrics) func(http.Hand metrics.AddDrop() sheddingStat.IncrementDrop() logx.Errorf("[http] dropped, %s - %s - %s", - r.RequestURI, internal.GetRemoteAddr(r), r.UserAgent()) + r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent()) w.WriteHeader(http.StatusServiceUnavailable) return } diff --git a/rest/internal/util.go b/rest/httpx/util.go similarity index 93% rename from rest/internal/util.go rename to rest/httpx/util.go index 5e7c2e2b..bdf6eaa8 100644 --- a/rest/internal/util.go +++ b/rest/httpx/util.go @@ -1,4 +1,4 @@ -package internal +package httpx import "net/http" diff --git a/rest/internal/util_test.go b/rest/httpx/util_test.go similarity index 94% rename from rest/internal/util_test.go rename to rest/httpx/util_test.go index 96594454..bcd9c416 100644 --- a/rest/internal/util_test.go +++ b/rest/httpx/util_test.go @@ -1,4 +1,4 @@ -package internal +package httpx import ( "net/http" @@ -16,4 +16,3 @@ func TestGetRemoteAddr(t *testing.T) { r.Header.Set(xForwardFor, host) assert.Equal(t, host, GetRemoteAddr(r)) } - diff --git a/rest/internal/log.go b/rest/internal/log.go index 78013835..80b19355 100644 --- a/rest/internal/log.go +++ b/rest/internal/log.go @@ -7,6 +7,7 @@ import ( "sync" "github.com/tal-tech/go-zero/core/logx" + "github.com/tal-tech/go-zero/rest/httpx" ) const LogContext = "request_logs" @@ -79,5 +80,5 @@ func formatf(r *http.Request, format string, v ...interface{}) string { } func formatWithReq(r *http.Request, v string) string { - return fmt.Sprintf("(%s - %s) %s", r.RequestURI, GetRemoteAddr(r), v) + return fmt.Sprintf("(%s - %s) %s", r.RequestURI, httpx.GetRemoteAddr(r), v) } From 05c8dd0b9c91eb4567b05d49a2de9485e588efc8 Mon Sep 17 00:00:00 2001 From: kevin Date: Wed, 12 Aug 2020 14:31:11 +0800 Subject: [PATCH 14/20] return ErrBodylessRequest on get method etc. --- rest/httpx/requests.go | 7 ++++++- rest/httpx/requests_test.go | 20 ++++++++++++++++++++ rest/httpx/{constants.go => vars.go} | 4 ++++ 3 files changed, 30 insertions(+), 1 deletion(-) rename rest/httpx/{constants.go => vars.go} (81%) diff --git a/rest/httpx/requests.go b/rest/httpx/requests.go index 115cd11f..e5c4be4a 100644 --- a/rest/httpx/requests.go +++ b/rest/httpx/requests.go @@ -83,8 +83,13 @@ func ParseHeader(headerValue string) map[string]string { // Parses the post request which contains json in body. func ParseJsonBody(r *http.Request, v interface{}) error { - var reader io.Reader + switch r.Method { + case http.MethodDelete, http.MethodPatch, http.MethodPost, http.MethodPut: + default: + return ErrBodylessRequest + } + var reader io.Reader if withJsonBody(r) { reader = io.LimitReader(r.Body, maxBodyLen) } else { diff --git a/rest/httpx/requests_test.go b/rest/httpx/requests_test.go index 49ecaa7e..a6411479 100644 --- a/rest/httpx/requests_test.go +++ b/rest/httpx/requests_test.go @@ -134,6 +134,26 @@ func BenchmarkParseRaw(b *testing.B) { } } +func TestParseJsonBodyless(t *testing.T) { + methods := []string{ + http.MethodConnect, + http.MethodGet, + http.MethodHead, + http.MethodOptions, + http.MethodTrace, + } + + for _, method := range methods { + t.Run(method, func(t *testing.T) { + r, err := http.NewRequest(http.MethodGet, "http://hello.com", nil) + if err != nil { + t.Fatal(err) + } + assert.Equal(t, ErrBodylessRequest, ParseJsonBody(r, nil)) + }) + } +} + func BenchmarkParseAuto(b *testing.B) { r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?name=hello&age=18&percent=3.4", nil) if err != nil { diff --git a/rest/httpx/constants.go b/rest/httpx/vars.go similarity index 81% rename from rest/httpx/constants.go rename to rest/httpx/vars.go index 3df99c8d..f4a6d6d9 100644 --- a/rest/httpx/constants.go +++ b/rest/httpx/vars.go @@ -1,5 +1,7 @@ package httpx +import "errors" + const ( ApplicationJson = "application/json" ContentEncoding = "Content-Encoding" @@ -17,3 +19,5 @@ const ( CodeSignatureWrongTime CodeSignatureInvalidToken ) + +var ErrBodylessRequest = errors.New("not a POST|PUT|PATCH request") From a26fc2b672d9df0b32c9763a84019f213377e114 Mon Sep 17 00:00:00 2001 From: kevin Date: Wed, 12 Aug 2020 14:37:34 +0800 Subject: [PATCH 15/20] parse body only if content length > 0 --- rest/httpx/requests.go | 6 +++++- rest/httpx/vars.go | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/rest/httpx/requests.go b/rest/httpx/requests.go index e5c4be4a..7f8d52cc 100644 --- a/rest/httpx/requests.go +++ b/rest/httpx/requests.go @@ -34,7 +34,11 @@ func Parse(r *http.Request, v interface{}) error { return err } - return ParseJsonBody(r, v) + if r.ContentLength > 0 { + return ParseJsonBody(r, v) + } + + return nil } // Parses the form request. diff --git a/rest/httpx/vars.go b/rest/httpx/vars.go index f4a6d6d9..001d2d6d 100644 --- a/rest/httpx/vars.go +++ b/rest/httpx/vars.go @@ -20,4 +20,4 @@ const ( CodeSignatureInvalidToken ) -var ErrBodylessRequest = errors.New("not a POST|PUT|PATCH request") +var ErrBodylessRequest = errors.New("not a POST|PUT|PATCH|DELETE request") From fcaebd73fbe4e6375a2ce989eed7178b763987fe Mon Sep 17 00:00:00 2001 From: kevin Date: Wed, 12 Aug 2020 14:44:09 +0800 Subject: [PATCH 16/20] remove bodyless check --- rest/httpx/requests.go | 12 +----------- rest/httpx/requests_test.go | 20 -------------------- rest/httpx/vars.go | 4 ---- 3 files changed, 1 insertion(+), 35 deletions(-) diff --git a/rest/httpx/requests.go b/rest/httpx/requests.go index 7f8d52cc..bf217da5 100644 --- a/rest/httpx/requests.go +++ b/rest/httpx/requests.go @@ -34,11 +34,7 @@ func Parse(r *http.Request, v interface{}) error { return err } - if r.ContentLength > 0 { - return ParseJsonBody(r, v) - } - - return nil + return ParseJsonBody(r, v) } // Parses the form request. @@ -87,12 +83,6 @@ func ParseHeader(headerValue string) map[string]string { // Parses the post request which contains json in body. func ParseJsonBody(r *http.Request, v interface{}) error { - switch r.Method { - case http.MethodDelete, http.MethodPatch, http.MethodPost, http.MethodPut: - default: - return ErrBodylessRequest - } - var reader io.Reader if withJsonBody(r) { reader = io.LimitReader(r.Body, maxBodyLen) diff --git a/rest/httpx/requests_test.go b/rest/httpx/requests_test.go index a6411479..49ecaa7e 100644 --- a/rest/httpx/requests_test.go +++ b/rest/httpx/requests_test.go @@ -134,26 +134,6 @@ func BenchmarkParseRaw(b *testing.B) { } } -func TestParseJsonBodyless(t *testing.T) { - methods := []string{ - http.MethodConnect, - http.MethodGet, - http.MethodHead, - http.MethodOptions, - http.MethodTrace, - } - - for _, method := range methods { - t.Run(method, func(t *testing.T) { - r, err := http.NewRequest(http.MethodGet, "http://hello.com", nil) - if err != nil { - t.Fatal(err) - } - assert.Equal(t, ErrBodylessRequest, ParseJsonBody(r, nil)) - }) - } -} - func BenchmarkParseAuto(b *testing.B) { r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?name=hello&age=18&percent=3.4", nil) if err != nil { diff --git a/rest/httpx/vars.go b/rest/httpx/vars.go index 001d2d6d..3df99c8d 100644 --- a/rest/httpx/vars.go +++ b/rest/httpx/vars.go @@ -1,7 +1,5 @@ package httpx -import "errors" - const ( ApplicationJson = "application/json" ContentEncoding = "Content-Encoding" @@ -19,5 +17,3 @@ const ( CodeSignatureWrongTime CodeSignatureInvalidToken ) - -var ErrBodylessRequest = errors.New("not a POST|PUT|PATCH|DELETE request") From cfa6644b0c1f141b02dd9170c3ae48f06a4cb30b Mon Sep 17 00:00:00 2001 From: kingxt Date: Wed, 12 Aug 2020 15:02:56 +0800 Subject: [PATCH 17/20] auto generate go mod if need --- tools/goctl/api/gogen/gen.go | 44 +++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/tools/goctl/api/gogen/gen.go b/tools/goctl/api/gogen/gen.go index 0468585c..0b9c553a 100644 --- a/tools/goctl/api/gogen/gen.go +++ b/tools/goctl/api/gogen/gen.go @@ -1,6 +1,7 @@ package gogen import ( + "bytes" "errors" "fmt" "os" @@ -55,6 +56,7 @@ func GoCommand(c *cli.Context) error { lang.Must(genLogic(dir, api)) // it does not work format(dir) + createGoModFileIfNeed(dir) if err := backupAndSweep(apiFile); err != nil { return err @@ -98,7 +100,7 @@ func format(dir string) { cmd := exec.Command("go", "fmt", "./"+dir+"...") _, err := cmd.CombinedOutput() if err != nil { - print(err.Error()) + fmt.Println(err.Error()) } } @@ -131,3 +133,43 @@ func sweep() error { return nil }) } + +func createGoModFileIfNeed(dir string) { + absDir, err := filepath.Abs(dir) + if err != nil { + panic(err) + } + + var tempPath = absDir + var hasGoMod = false + for { + if tempPath == filepath.Dir(tempPath) { + break + } + tempPath = filepath.Dir(tempPath) + if util.FileExists(filepath.Join(tempPath, goModeIdentifier)) { + tempPath = filepath.Dir(tempPath) + hasGoMod = true + break + } + } + if !hasGoMod { + gopath := os.Getenv("GOPATH") + parent := path.Join(gopath, "src") + pos := strings.Index(absDir, parent) + if pos < 0 { + moduleName := absDir[len(filepath.Dir(absDir))+1:] + cmd := exec.Command("go", "mod", "init", moduleName) + cmd.Dir = dir + var stdout, stderr bytes.Buffer + cmd.Stdout = &stdout + cmd.Stderr = &stderr + err := cmd.Run() + if err != nil { + fmt.Println(err.Error()) + } + outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes()) + fmt.Printf(outStr + "\n" + errStr) + } + } +} From 03ac41438fd079be2c4776a6aab2a1cfe7478299 Mon Sep 17 00:00:00 2001 From: kevin Date: Wed, 12 Aug 2020 15:03:07 +0800 Subject: [PATCH 18/20] rename files --- core/search/{searchtree.go => tree.go} | 0 core/search/{searchtree_debug.go => tree_debug.go} | 0 core/search/{searchtree_test.go => tree_test.go} | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename core/search/{searchtree.go => tree.go} (100%) rename core/search/{searchtree_debug.go => tree_debug.go} (100%) rename core/search/{searchtree_test.go => tree_test.go} (100%) diff --git a/core/search/searchtree.go b/core/search/tree.go similarity index 100% rename from core/search/searchtree.go rename to core/search/tree.go diff --git a/core/search/searchtree_debug.go b/core/search/tree_debug.go similarity index 100% rename from core/search/searchtree_debug.go rename to core/search/tree_debug.go diff --git a/core/search/searchtree_test.go b/core/search/tree_test.go similarity index 100% rename from core/search/searchtree_test.go rename to core/search/tree_test.go From 6d8dc4630f9fcb710beb87c3fd9c6e4e23579f08 Mon Sep 17 00:00:00 2001 From: kevin Date: Wed, 12 Aug 2020 15:21:47 +0800 Subject: [PATCH 19/20] update readme --- readme.md | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/readme.md b/readme.md index b3a503ff..f9d9b61c 100644 --- a/readme.md +++ b/readme.md @@ -144,27 +144,26 @@ go-zero是一个集成了各种工程实践的包含web和rpc框架,有如下 8 directories, 9 files ``` - 如果使用的是gomod依赖管理,需要在greet项目目录下面执行 `go mod init greet` 初始化gomod。 生成的代码可以直接运行: - - ```shell + +```shell cd greet go run greet.go -f etc/greet-api.json ``` - - 默认侦听在8888端口(可以在配置文件里修改),可以通过curl请求: - - ```shell + +默认侦听在8888端口(可以在配置文件里修改),可以通过curl请求: + +```shell ➜ go-zero git:(master) curl -w "\ncode: %{http_code}\n" http://localhost:8888/greet/from/kevin {"code":0} code: 200 ``` - - 编写业务代码: - - * 可以在servicecontext.go里面传递依赖给logic,比如mysql, redis等 + +编写业务代码: + +* 可以在servicecontext.go里面传递依赖给logic,比如mysql, redis等 * 在api定义的get/post/put/delete等请求对应的logic里增加业务处理逻辑 - + 4. 可以根据api文件生成前端需要的Java, TypeScript, Dart, JavaScript代码 ```shell From 78f5e7df87d5ab2217d739c289fda28d097a87df Mon Sep 17 00:00:00 2001 From: kevin Date: Wed, 12 Aug 2020 16:08:15 +0800 Subject: [PATCH 20/20] update workflow --- .github/workflows/go.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 0e7c1003..2ecbf4c5 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -25,10 +25,6 @@ jobs: - name: Get dependencies run: | go get -v -t -d ./... - if [ -f Gopkg.toml ]; then - curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh - dep ensure - fi - name: Test run: go test -v -race ./...