diff --git a/cmd/server/main.go b/cmd/server/main.go index 6cbd82433235041f596492ed0f6b56093390b51d..04ac751c461f9e9c62fc733ebbca02616c86ebed 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -29,13 +29,12 @@ func init() { flag.StringVar(&flagconf, "conf", "", "config path, eg: -conf config.yaml") } -func newApp(logger log.Logger, hs *http.Server, gs *grpc.Server) *kratos.App { +func newApp(hs *http.Server, gs *grpc.Server) *kratos.App { return kratos.New( kratos.ID(id), kratos.Name(Name), kratos.Version(Version), kratos.Metadata(map[string]string{}), - kratos.Logger(logger), kratos.Server( hs, gs, @@ -54,6 +53,8 @@ func main() { "trace.id", tracing.TraceID(), "span.id", tracing.SpanID(), ) + log.SetLogger(logger) + c := config.New( config.WithSource( file.NewSource(flagconf), @@ -70,7 +71,7 @@ func main() { panic(err) } - app, cleanup, err := wireApp(bc.Server, bc.Data, logger) + app, cleanup, err := wireApp(bc.Server, bc.Data) if err != nil { panic(err) } diff --git a/cmd/server/wire.go b/cmd/server/wire.go index f86560226dafc9b86fd0bbb53844c9e4014201cb..50f3784c641899000ba40891822578fbd8c71dcb 100644 --- a/cmd/server/wire.go +++ b/cmd/server/wire.go @@ -1,3 +1,4 @@ +//go:build wireinject // +build wireinject // The build tag makes sure the stub is not built in the final build. @@ -11,11 +12,10 @@ import ( "github.com/go-kratos/kratos-layout/internal/server" "github.com/go-kratos/kratos-layout/internal/service" "github.com/go-kratos/kratos/v2" - "github.com/go-kratos/kratos/v2/log" "github.com/google/wire" ) // wireApp init kratos application. -func wireApp(*conf.Server, *conf.Data, log.Logger) (*kratos.App, func(), error) { +func wireApp(*conf.Server, *conf.Data) (*kratos.App, func(), error) { panic(wire.Build(server.ProviderSet, data.ProviderSet, biz.ProviderSet, service.ProviderSet, newApp)) } diff --git a/cmd/server/wire_gen.go b/cmd/server/wire_gen.go deleted file mode 100644 index f34089a4fe35e3453efe05b25636f23c0cced3fb..0000000000000000000000000000000000000000 --- a/cmd/server/wire_gen.go +++ /dev/null @@ -1,35 +0,0 @@ -// Code generated by Wire. DO NOT EDIT. - -//go:generate go run github.com/google/wire/cmd/wire -//+build !wireinject - -package main - -import ( - "github.com/go-kratos/kratos-layout/internal/biz" - "github.com/go-kratos/kratos-layout/internal/conf" - "github.com/go-kratos/kratos-layout/internal/data" - "github.com/go-kratos/kratos-layout/internal/server" - "github.com/go-kratos/kratos-layout/internal/service" - "github.com/go-kratos/kratos/v2" - "github.com/go-kratos/kratos/v2/log" -) - -// Injectors from wire.go: - -// wireApp init kratos application. -func wireApp(confServer *conf.Server, confData *conf.Data, logger log.Logger) (*kratos.App, func(), error) { - dataData, cleanup, err := data.NewData(confData, logger) - if err != nil { - return nil, nil, err - } - greeterRepo := data.NewGreeterRepo(dataData, logger) - greeterUsecase := biz.NewGreeterUsecase(greeterRepo, logger) - greeterService := service.NewGreeterService(greeterUsecase) - httpServer := server.NewHTTPServer(confServer, greeterService, logger) - grpcServer := server.NewGRPCServer(confServer, greeterService, logger) - app := newApp(logger, httpServer, grpcServer) - return app, func() { - cleanup() - }, nil -} diff --git a/go.mod b/go.mod index aaa83ec842c55c70fce8480251257a141e74f2e8..c4f06b2d25b4a43c752e32114abb777983352d9c 100644 --- a/go.mod +++ b/go.mod @@ -16,14 +16,17 @@ require ( github.com/go-logr/stdr v1.2.2 // indirect github.com/go-playground/form/v4 v4.2.0 // indirect github.com/golang/protobuf v1.5.2 // indirect + github.com/google/subcommands v1.0.1 // indirect github.com/google/uuid v1.3.0 // indirect github.com/gorilla/mux v1.8.0 // indirect github.com/imdario/mergo v0.3.12 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect go.opentelemetry.io/otel v1.7.0 // indirect go.opentelemetry.io/otel/trace v1.7.0 // indirect golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 // indirect golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect golang.org/x/text v0.3.7 // indirect + golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135 // indirect gopkg.in/yaml.v3 v3.0.0 // indirect ) diff --git a/go.sum b/go.sum index a2706a57bce0942c30fdd4063cfe724e3f50ccf0..e24fc0933d84a167ef08cdaa0ef0de8fb61b0864 100644 --- a/go.sum +++ b/go.sum @@ -62,6 +62,7 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= +github.com/google/subcommands v1.0.1 h1:/eqq+otEXm5vhfBrbREPCSVQbvofip6kIz+mX5TUH7k= github.com/google/subcommands v1.0.1/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= @@ -146,6 +147,7 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190422233926-fe54fb35175b/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135 h1:5Beo0mZN8dRzgrMMkDp0jc8YXQKx9DiJ2k1dkvGsn5A= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= diff --git a/internal/biz/greeter.go b/internal/biz/greeter.go index 0ee58cfc2985252099b7efaea82868b5b6500fcf..51aeaa68ad3dcc558e7810eabd1a6f7381017e1f 100644 --- a/internal/biz/greeter.go +++ b/internal/biz/greeter.go @@ -34,12 +34,12 @@ type GreeterUsecase struct { } // NewGreeterUsecase new a Greeter usecase. -func NewGreeterUsecase(repo GreeterRepo, logger log.Logger) *GreeterUsecase { - return &GreeterUsecase{repo: repo, log: log.NewHelper(logger)} +func NewGreeterUsecase(repo GreeterRepo) *GreeterUsecase { + return &GreeterUsecase{repo: repo} } // CreateGreeter creates a Greeter, and returns the new Greeter. func (uc *GreeterUsecase) CreateGreeter(ctx context.Context, g *Greeter) (*Greeter, error) { - uc.log.WithContext(ctx).Infof("CreateGreeter: %v", g.Hello) + log.Infof("CreateGreeter: %v", g.Hello) return uc.repo.Save(ctx, g) } diff --git a/internal/data/data.go b/internal/data/data.go index 16a8a42421bda5d3ffb84b841b9163ff5d084519..dff1f08c6a619cb522d1fb420dd19ab040a55c90 100644 --- a/internal/data/data.go +++ b/internal/data/data.go @@ -15,9 +15,9 @@ type Data struct { } // NewData . -func NewData(c *conf.Data, logger log.Logger) (*Data, func(), error) { +func NewData(c *conf.Data) (*Data, func(), error) { cleanup := func() { - log.NewHelper(logger).Info("closing the data resources") + log.Info("closing the data resources") } return &Data{}, cleanup, nil } diff --git a/internal/data/greeter.go b/internal/data/greeter.go index 139504a3ab022fd9715e14acfbd81b639a777853..7af3138e507fbc9d74b58a524d10c3a760d0e6b5 100644 --- a/internal/data/greeter.go +++ b/internal/data/greeter.go @@ -4,19 +4,16 @@ import ( "context" "github.com/go-kratos/kratos-layout/internal/biz" - "github.com/go-kratos/kratos/v2/log" ) type greeterRepo struct { data *Data - log *log.Helper } // NewGreeterRepo . -func NewGreeterRepo(data *Data, logger log.Logger) biz.GreeterRepo { +func NewGreeterRepo(data *Data) biz.GreeterRepo { return &greeterRepo{ data: data, - log: log.NewHelper(logger), } } diff --git a/internal/server/grpc.go b/internal/server/grpc.go index 535e60fcbe57961bafd9855224d3738fd3636bed..763f268de6fae56a4e2b4e5c7c60f5abf4a06daa 100644 --- a/internal/server/grpc.go +++ b/internal/server/grpc.go @@ -4,13 +4,12 @@ import ( v1 "github.com/go-kratos/kratos-layout/api/helloworld/v1" "github.com/go-kratos/kratos-layout/internal/conf" "github.com/go-kratos/kratos-layout/internal/service" - "github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/middleware/recovery" "github.com/go-kratos/kratos/v2/transport/grpc" ) // NewGRPCServer new a gRPC server. -func NewGRPCServer(c *conf.Server, greeter *service.GreeterService, logger log.Logger) *grpc.Server { +func NewGRPCServer(c *conf.Server, greeter *service.GreeterService) *grpc.Server { var opts = []grpc.ServerOption{ grpc.Middleware( recovery.Recovery(), diff --git a/internal/server/http.go b/internal/server/http.go index dc7b2547a77f2d2210c3953ada5723e18f1e16d2..ed1c7aa0958ed9a4cfd82fdd35cb38582500a3ef 100644 --- a/internal/server/http.go +++ b/internal/server/http.go @@ -4,13 +4,12 @@ import ( v1 "github.com/go-kratos/kratos-layout/api/helloworld/v1" "github.com/go-kratos/kratos-layout/internal/conf" "github.com/go-kratos/kratos-layout/internal/service" - "github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/middleware/recovery" "github.com/go-kratos/kratos/v2/transport/http" ) // NewHTTPServer new a HTTP server. -func NewHTTPServer(c *conf.Server, greeter *service.GreeterService, logger log.Logger) *http.Server { +func NewHTTPServer(c *conf.Server, greeter *service.GreeterService) *http.Server { var opts = []http.ServerOption{ http.Middleware( recovery.Recovery(),