From 9d1b8ca61958b91166dc64dd1ec6cb843d618780 Mon Sep 17 00:00:00 2001 From: sulinehk Date: Sat, 3 Feb 2024 20:59:21 +0800 Subject: [PATCH] use api instead grpc-gateway --- go.mod | 4 - go.sum | 14 -- .../user_center/api/etc/user_center-api.yaml | 9 + .../user_center/api/internal/config/config.go | 11 ++ .../internal/handler/create_user_handler.go | 28 +++ .../api/internal/handler/routes.go | 22 +++ .../api/internal/logic/create_user_logic.go | 37 ++++ .../api/internal/svc/service_context.go | 19 ++ .../user_center/api/internal/types/types.go | 14 ++ service/user_center/api/user_center.api | 24 +++ service/user_center/api/user_center.go | 31 ++++ service/user_center/rpc/gateway/gateway.go | 20 --- service/user_center/rpc/gateway/gateway.yaml | 14 -- .../user_center/rpc/gateway/user_center.pb | 9 - .../{ping_logic.go => create_user_logic.go} | 10 +- .../rpc/internal/server/user_center_server.go | 6 +- .../rpc/user_center/user_center.pb.go | 166 +++++++++++++----- .../rpc/user_center/user_center_grpc.pb.go | 30 ++-- .../rpc/user_center_client/user_center.go | 11 +- 19 files changed, 343 insertions(+), 136 deletions(-) create mode 100644 service/user_center/api/etc/user_center-api.yaml create mode 100644 service/user_center/api/internal/config/config.go create mode 100644 service/user_center/api/internal/handler/create_user_handler.go create mode 100644 service/user_center/api/internal/handler/routes.go create mode 100644 service/user_center/api/internal/logic/create_user_logic.go create mode 100644 service/user_center/api/internal/svc/service_context.go create mode 100644 service/user_center/api/internal/types/types.go create mode 100644 service/user_center/api/user_center.api create mode 100644 service/user_center/api/user_center.go delete mode 100644 service/user_center/rpc/gateway/gateway.go delete mode 100644 service/user_center/rpc/gateway/gateway.yaml delete mode 100644 service/user_center/rpc/gateway/user_center.pb rename service/user_center/rpc/internal/logic/{ping_logic.go => create_user_logic.go} (57%) diff --git a/go.mod b/go.mod index ebf1c9d..707df81 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,6 @@ require ( require ( github.com/beorn7/perks v1.0.1 // indirect - github.com/bufbuild/protocompile v0.7.1 // indirect github.com/cenkalti/backoff/v4 v4.2.1 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/coreos/go-semver v0.3.1 // indirect @@ -19,7 +18,6 @@ require ( github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/emicklei/go-restful/v3 v3.9.0 // indirect github.com/fatih/color v1.16.0 // indirect - github.com/fullstorydev/grpcurl v1.8.9 // indirect github.com/go-logr/logr v1.2.4 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-openapi/jsonpointer v0.19.6 // indirect @@ -36,7 +34,6 @@ require ( github.com/google/gofuzz v1.2.0 // indirect github.com/google/uuid v1.4.0 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.0 // indirect - github.com/jhump/protoreflect v1.15.4 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/mailru/easyjson v0.7.7 // indirect @@ -73,7 +70,6 @@ require ( go.uber.org/zap v1.24.0 // indirect golang.org/x/net v0.19.0 // indirect golang.org/x/oauth2 v0.14.0 // indirect - golang.org/x/sync v0.5.0 // indirect golang.org/x/sys v0.15.0 // indirect golang.org/x/term v0.15.0 // indirect golang.org/x/text v0.14.0 // indirect diff --git a/go.sum b/go.sum index c81c62b..d7ddc3c 100644 --- a/go.sum +++ b/go.sum @@ -8,8 +8,6 @@ github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLj github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= -github.com/bufbuild/protocompile v0.7.1 h1:Kd8fb6EshOHXNNRtYAmLAwy/PotlyFoN0iMbuwGNh0M= -github.com/bufbuild/protocompile v0.7.1/go.mod h1:+Etjg4guZoAqzVk2czwEQP12yaxLJ8DxuqCJ9qHdH94= github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM= github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= @@ -30,8 +28,6 @@ github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= -github.com/fullstorydev/grpcurl v1.8.9 h1:JMvZXK8lHDGyLmTQ0ZdGDnVVGuwjbpaumf8p42z0d+c= -github.com/fullstorydev/grpcurl v1.8.9/go.mod h1:PNNKevV5VNAV2loscyLISrEnWQI61eqR0F8l3bVadAA= github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= @@ -81,8 +77,6 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.0 h1:RtRsiaGvWxcwd8y3BiRZxsylPT8 github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.0/go.mod h1:TzP6duP4Py2pHLVPPQp42aoYI92+PCrVotyR5e8Vqlk= github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw= github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI= -github.com/jhump/protoreflect v1.15.4 h1:mrwJhfQGGljwvR/jPEocli8KA6G9afbQpH8NY2wORcI= -github.com/jhump/protoreflect v1.15.4/go.mod h1:2B+zwrnMY3TTIqEK01OG/d3pyUycQBfDf+bx8fE2DNg= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= @@ -103,8 +97,6 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= -github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -140,8 +132,6 @@ github.com/prometheus/common v0.44.0 h1:+5BrQJwiBB9xsMygAB3TNvpQKOwlkc25LbISbrdO github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO7x0VV9VvuY= github.com/prometheus/procfs v0.11.1 h1:xRC8Iq1yyca5ypa9n1EZnWZkt7dwcoRPQwX/5gwaUuI= github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/y0/p/ScXhY= -github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= -github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= @@ -229,8 +219,6 @@ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= -golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -285,8 +273,6 @@ google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHh gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= -gopkg.in/cheggaaa/pb.v1 v1.0.28 h1:n1tBJnnK2r7g9OW2btFH91V92STTUevLXYFb8gy9EMk= -gopkg.in/cheggaaa/pb.v1 v1.0.28/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw= gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY= gopkg.in/h2non/gock.v1 v1.1.2/go.mod h1:n7UGz/ckNChHiK05rDoiC4MYSunEC/lyaUm2WWaDva0= gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc= diff --git a/service/user_center/api/etc/user_center-api.yaml b/service/user_center/api/etc/user_center-api.yaml new file mode 100644 index 0000000..daf4499 --- /dev/null +++ b/service/user_center/api/etc/user_center-api.yaml @@ -0,0 +1,9 @@ +Name: user_center-api +Host: 0.0.0.0 +Port: 8888 + +user_center: + Etcd: + Hosts: + - localhost:2379 + Key: usercenter.rpc \ No newline at end of file diff --git a/service/user_center/api/internal/config/config.go b/service/user_center/api/internal/config/config.go new file mode 100644 index 0000000..fa5230d --- /dev/null +++ b/service/user_center/api/internal/config/config.go @@ -0,0 +1,11 @@ +package config + +import ( + "github.com/zeromicro/go-zero/rest" + "github.com/zeromicro/go-zero/zrpc" +) + +type Config struct { + rest.RestConf + UserCenter zrpc.RpcClientConf +} diff --git a/service/user_center/api/internal/handler/create_user_handler.go b/service/user_center/api/internal/handler/create_user_handler.go new file mode 100644 index 0000000..9ab5cbc --- /dev/null +++ b/service/user_center/api/internal/handler/create_user_handler.go @@ -0,0 +1,28 @@ +package handler + +import ( + "net/http" + + "github.com/linehk/go-blogger/service/user_center/api/internal/logic" + "github.com/linehk/go-blogger/service/user_center/api/internal/svc" + "github.com/linehk/go-blogger/service/user_center/api/internal/types" + "github.com/zeromicro/go-zero/rest/httpx" +) + +func CreateUserHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.CreateUserRequest + if err := httpx.Parse(r, &req); err != nil { + httpx.ErrorCtx(r.Context(), w, err) + return + } + + l := logic.NewCreateUserLogic(r.Context(), svcCtx) + err := l.CreateUser(&req) + if err != nil { + httpx.ErrorCtx(r.Context(), w, err) + } else { + httpx.Ok(w) + } + } +} diff --git a/service/user_center/api/internal/handler/routes.go b/service/user_center/api/internal/handler/routes.go new file mode 100644 index 0000000..a260d24 --- /dev/null +++ b/service/user_center/api/internal/handler/routes.go @@ -0,0 +1,22 @@ +// Code generated by goctl. DO NOT EDIT. +package handler + +import ( + "net/http" + + "github.com/linehk/go-blogger/service/user_center/api/internal/svc" + + "github.com/zeromicro/go-zero/rest" +) + +func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { + server.AddRoutes( + []rest.Route{ + { + Method: http.MethodPost, + Path: "/users/create", + Handler: CreateUserHandler(serverCtx), + }, + }, + ) +} diff --git a/service/user_center/api/internal/logic/create_user_logic.go b/service/user_center/api/internal/logic/create_user_logic.go new file mode 100644 index 0000000..740cf94 --- /dev/null +++ b/service/user_center/api/internal/logic/create_user_logic.go @@ -0,0 +1,37 @@ +package logic + +import ( + "context" + + "github.com/linehk/go-blogger/service/user_center/api/internal/svc" + "github.com/linehk/go-blogger/service/user_center/api/internal/types" + "github.com/linehk/go-blogger/service/user_center/rpc/user_center" + + "github.com/zeromicro/go-zero/core/logx" +) + +type CreateUserLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewCreateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateUserLogic { + return &CreateUserLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *CreateUserLogic) CreateUser(req *types.CreateUserRequest) error { + _, err := l.svcCtx.UserCenter.CreateUser(l.ctx, &user_center.CreateUserRequest{ + Username: req.Username, + Password: req.Password, + Email: req.Email, + }) + if err != nil { + return err + } + return nil +} diff --git a/service/user_center/api/internal/svc/service_context.go b/service/user_center/api/internal/svc/service_context.go new file mode 100644 index 0000000..415f307 --- /dev/null +++ b/service/user_center/api/internal/svc/service_context.go @@ -0,0 +1,19 @@ +package svc + +import ( + "github.com/linehk/go-blogger/service/user_center/api/internal/config" + "github.com/linehk/go-blogger/service/user_center/rpc/user_center_client" + "github.com/zeromicro/go-zero/zrpc" +) + +type ServiceContext struct { + Config config.Config + UserCenter user_center_client.UserCenter +} + +func NewServiceContext(c config.Config) *ServiceContext { + return &ServiceContext{ + Config: c, + UserCenter: user_center_client.NewUserCenter(zrpc.MustNewClient(c.UserCenter)), + } +} diff --git a/service/user_center/api/internal/types/types.go b/service/user_center/api/internal/types/types.go new file mode 100644 index 0000000..a00c5d8 --- /dev/null +++ b/service/user_center/api/internal/types/types.go @@ -0,0 +1,14 @@ +// Code generated by goctl. DO NOT EDIT. +package types + +type CreateUserRequest struct { + Username string `form:"username"` + Password string `form:"password"` + Email string `form:"email"` +} + +type EmptyRequest struct { +} + +type EmptyResponse struct { +} diff --git a/service/user_center/api/user_center.api b/service/user_center/api/user_center.api new file mode 100644 index 0000000..e0b7f93 --- /dev/null +++ b/service/user_center/api/user_center.api @@ -0,0 +1,24 @@ +syntax = "v1" + +info ( + title: "user_center" + desc: "user_center" + author: "sulinehk" + email: "sulinehk@gmail.com" +) + +type EmptyRequest {} + +type EmptyResponse {} + +type CreateUserRequest { + username string `form:"username"` + password string `form:"password"` + email string `form:"email"` +} + +service user_center-api { + @handler CreateUser + post /users/create (CreateUserRequest) +} + diff --git a/service/user_center/api/user_center.go b/service/user_center/api/user_center.go new file mode 100644 index 0000000..c688c7b --- /dev/null +++ b/service/user_center/api/user_center.go @@ -0,0 +1,31 @@ +package main + +import ( + "flag" + "fmt" + + "github.com/linehk/go-blogger/service/user_center/api/internal/config" + "github.com/linehk/go-blogger/service/user_center/api/internal/handler" + "github.com/linehk/go-blogger/service/user_center/api/internal/svc" + + "github.com/zeromicro/go-zero/core/conf" + "github.com/zeromicro/go-zero/rest" +) + +var configFile = flag.String("f", "etc/user_center-api.yaml", "the config file") + +func main() { + flag.Parse() + + var c config.Config + conf.MustLoad(*configFile, &c) + + server := rest.MustNewServer(c.RestConf) + defer server.Stop() + + ctx := svc.NewServiceContext(c) + handler.RegisterHandlers(server, ctx) + + fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port) + server.Start() +} diff --git a/service/user_center/rpc/gateway/gateway.go b/service/user_center/rpc/gateway/gateway.go deleted file mode 100644 index 6273451..0000000 --- a/service/user_center/rpc/gateway/gateway.go +++ /dev/null @@ -1,20 +0,0 @@ -package main - -import ( - "flag" - - "github.com/zeromicro/go-zero/core/conf" - "github.com/zeromicro/go-zero/gateway" -) - -var configFile = flag.String("f", "etc/gateway.yaml", "config file") - -func main() { - flag.Parse() - - var c gateway.GatewayConf - conf.MustLoad(*configFile, &c) - gw := gateway.MustNewServer(c) - defer gw.Stop() - gw.Start() -} diff --git a/service/user_center/rpc/gateway/gateway.yaml b/service/user_center/rpc/gateway/gateway.yaml deleted file mode 100644 index 9909f3d..0000000 --- a/service/user_center/rpc/gateway/gateway.yaml +++ /dev/null @@ -1,14 +0,0 @@ -Name: user_center-gateway -Host: localhost -Port: 8888 -Upstreams: - - Grpc: - Target: 0.0.0.0:8080 - # protoset mode - ProtoSets: - - user_center.pb - # Mappings can also be written in proto options - Mappings: - - Method: post - Path: /create-user - RpcPath: usercenter/CreateUser \ No newline at end of file diff --git a/service/user_center/rpc/gateway/user_center.pb b/service/user_center/rpc/gateway/user_center.pb deleted file mode 100644 index 2041e07..0000000 --- a/service/user_center/rpc/gateway/user_center.pb +++ /dev/null @@ -1,9 +0,0 @@ - -Ô -)service/user_center/rpc/user_center.proto user_center" -Request -ping ( Rping" -Response -pong ( Rpong2B - User_center3 -Ping.user_center.Request.user_center.ResponseBZ ./user_centerbproto3 \ No newline at end of file diff --git a/service/user_center/rpc/internal/logic/ping_logic.go b/service/user_center/rpc/internal/logic/create_user_logic.go similarity index 57% rename from service/user_center/rpc/internal/logic/ping_logic.go rename to service/user_center/rpc/internal/logic/create_user_logic.go index 8ff8596..dfddc9c 100644 --- a/service/user_center/rpc/internal/logic/ping_logic.go +++ b/service/user_center/rpc/internal/logic/create_user_logic.go @@ -9,22 +9,22 @@ import ( "github.com/zeromicro/go-zero/core/logx" ) -type PingLogic struct { +type CreateUserLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger } -func NewPingLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PingLogic { - return &PingLogic{ +func NewCreateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateUserLogic { + return &CreateUserLogic{ ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx), } } -func (l *PingLogic) Ping(in *user_center.Request) (*user_center.Response, error) { +func (l *CreateUserLogic) CreateUser(in *user_center.CreateUserRequest) (*user_center.EmptyResponse, error) { // todo: add your logic here and delete this line - return &user_center.Response{}, nil + return &user_center.EmptyResponse{}, nil } diff --git a/service/user_center/rpc/internal/server/user_center_server.go b/service/user_center/rpc/internal/server/user_center_server.go index 37c7ac1..ad53e6d 100644 --- a/service/user_center/rpc/internal/server/user_center_server.go +++ b/service/user_center/rpc/internal/server/user_center_server.go @@ -22,7 +22,7 @@ func NewUserCenterServer(svcCtx *svc.ServiceContext) *UserCenterServer { } } -func (s *UserCenterServer) Ping(ctx context.Context, in *user_center.Request) (*user_center.Response, error) { - l := logic.NewPingLogic(ctx, s.svcCtx) - return l.Ping(in) +func (s *UserCenterServer) CreateUser(ctx context.Context, in *user_center.CreateUserRequest) (*user_center.EmptyResponse, error) { + l := logic.NewCreateUserLogic(ctx, s.svcCtx) + return l.CreateUser(in) } diff --git a/service/user_center/rpc/user_center/user_center.pb.go b/service/user_center/rpc/user_center/user_center.pb.go index 21d2d17..1f2483c 100644 --- a/service/user_center/rpc/user_center/user_center.pb.go +++ b/service/user_center/rpc/user_center/user_center.pb.go @@ -20,16 +20,14 @@ const ( _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) -type Request struct { +type EmptyRequest struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - - Ping string `protobuf:"bytes,1,opt,name=ping,proto3" json:"ping,omitempty"` } -func (x *Request) Reset() { - *x = Request{} +func (x *EmptyRequest) Reset() { + *x = EmptyRequest{} if protoimpl.UnsafeEnabled { mi := &file_service_user_center_rpc_user_center_proto_msgTypes[0] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) @@ -37,13 +35,13 @@ func (x *Request) Reset() { } } -func (x *Request) String() string { +func (x *EmptyRequest) String() string { return protoimpl.X.MessageStringOf(x) } -func (*Request) ProtoMessage() {} +func (*EmptyRequest) ProtoMessage() {} -func (x *Request) ProtoReflect() protoreflect.Message { +func (x *EmptyRequest) ProtoReflect() protoreflect.Message { mi := &file_service_user_center_rpc_user_center_proto_msgTypes[0] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) @@ -55,43 +53,77 @@ func (x *Request) ProtoReflect() protoreflect.Message { return mi.MessageOf(x) } -// Deprecated: Use Request.ProtoReflect.Descriptor instead. -func (*Request) Descriptor() ([]byte, []int) { +// Deprecated: Use EmptyRequest.ProtoReflect.Descriptor instead. +func (*EmptyRequest) Descriptor() ([]byte, []int) { return file_service_user_center_rpc_user_center_proto_rawDescGZIP(), []int{0} } -func (x *Request) GetPing() string { - if x != nil { - return x.Ping +type EmptyResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields +} + +func (x *EmptyResponse) Reset() { + *x = EmptyResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_service_user_center_rpc_user_center_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) } - return "" } -type Response struct { +func (x *EmptyResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*EmptyResponse) ProtoMessage() {} + +func (x *EmptyResponse) ProtoReflect() protoreflect.Message { + mi := &file_service_user_center_rpc_user_center_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use EmptyResponse.ProtoReflect.Descriptor instead. +func (*EmptyResponse) Descriptor() ([]byte, []int) { + return file_service_user_center_rpc_user_center_proto_rawDescGZIP(), []int{1} +} + +type CreateUserRequest struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Pong string `protobuf:"bytes,1,opt,name=pong,proto3" json:"pong,omitempty"` + Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` + Username string `protobuf:"bytes,2,opt,name=username,proto3" json:"username,omitempty"` + Password string `protobuf:"bytes,3,opt,name=password,proto3" json:"password,omitempty"` + Email string `protobuf:"bytes,4,opt,name=email,proto3" json:"email,omitempty"` } -func (x *Response) Reset() { - *x = Response{} +func (x *CreateUserRequest) Reset() { + *x = CreateUserRequest{} if protoimpl.UnsafeEnabled { - mi := &file_service_user_center_rpc_user_center_proto_msgTypes[1] + mi := &file_service_user_center_rpc_user_center_proto_msgTypes[2] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } -func (x *Response) String() string { +func (x *CreateUserRequest) String() string { return protoimpl.X.MessageStringOf(x) } -func (*Response) ProtoMessage() {} +func (*CreateUserRequest) ProtoMessage() {} -func (x *Response) ProtoReflect() protoreflect.Message { - mi := &file_service_user_center_rpc_user_center_proto_msgTypes[1] +func (x *CreateUserRequest) ProtoReflect() protoreflect.Message { + mi := &file_service_user_center_rpc_user_center_proto_msgTypes[2] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -102,14 +134,35 @@ func (x *Response) ProtoReflect() protoreflect.Message { return mi.MessageOf(x) } -// Deprecated: Use Response.ProtoReflect.Descriptor instead. -func (*Response) Descriptor() ([]byte, []int) { - return file_service_user_center_rpc_user_center_proto_rawDescGZIP(), []int{1} +// Deprecated: Use CreateUserRequest.ProtoReflect.Descriptor instead. +func (*CreateUserRequest) Descriptor() ([]byte, []int) { + return file_service_user_center_rpc_user_center_proto_rawDescGZIP(), []int{2} +} + +func (x *CreateUserRequest) GetId() int64 { + if x != nil { + return x.Id + } + return 0 +} + +func (x *CreateUserRequest) GetUsername() string { + if x != nil { + return x.Username + } + return "" +} + +func (x *CreateUserRequest) GetPassword() string { + if x != nil { + return x.Password + } + return "" } -func (x *Response) GetPong() string { +func (x *CreateUserRequest) GetEmail() string { if x != nil { - return x.Pong + return x.Email } return "" } @@ -120,17 +173,23 @@ var file_service_user_center_rpc_user_center_proto_rawDesc = []byte{ 0x0a, 0x29, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2f, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x2f, 0x72, 0x70, 0x63, 0x2f, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0b, 0x75, 0x73, 0x65, - 0x72, 0x5f, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x22, 0x1d, 0x0a, 0x07, 0x52, 0x65, 0x71, 0x75, - 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x69, 0x6e, 0x67, 0x18, 0x01, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x04, 0x70, 0x69, 0x6e, 0x67, 0x22, 0x1e, 0x0a, 0x08, 0x52, 0x65, 0x73, 0x70, 0x6f, - 0x6e, 0x73, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x6f, 0x6e, 0x67, 0x18, 0x01, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x04, 0x70, 0x6f, 0x6e, 0x67, 0x32, 0x42, 0x0a, 0x0b, 0x55, 0x73, 0x65, 0x72, 0x5f, - 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x12, 0x33, 0x0a, 0x04, 0x50, 0x69, 0x6e, 0x67, 0x12, 0x14, - 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x2e, 0x52, 0x65, 0x71, - 0x75, 0x65, 0x73, 0x74, 0x1a, 0x15, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x63, 0x65, 0x6e, 0x74, - 0x65, 0x72, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x0f, 0x5a, 0x0d, 0x2e, - 0x2f, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x62, 0x06, 0x70, 0x72, - 0x6f, 0x74, 0x6f, 0x33, + 0x72, 0x5f, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x22, 0x0e, 0x0a, 0x0c, 0x45, 0x6d, 0x70, 0x74, + 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x0f, 0x0a, 0x0d, 0x45, 0x6d, 0x70, 0x74, + 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x71, 0x0a, 0x11, 0x43, 0x72, 0x65, + 0x61, 0x74, 0x65, 0x55, 0x73, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x0e, + 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x69, 0x64, 0x12, 0x1a, + 0x0a, 0x08, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x08, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x61, + 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x70, 0x61, + 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x65, 0x6d, 0x61, 0x69, 0x6c, 0x18, + 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x65, 0x6d, 0x61, 0x69, 0x6c, 0x32, 0x57, 0x0a, 0x0b, + 0x55, 0x73, 0x65, 0x72, 0x5f, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x12, 0x48, 0x0a, 0x0a, 0x43, + 0x72, 0x65, 0x61, 0x74, 0x65, 0x55, 0x73, 0x65, 0x72, 0x12, 0x1e, 0x2e, 0x75, 0x73, 0x65, 0x72, + 0x5f, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x55, 0x73, + 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1a, 0x2e, 0x75, 0x73, 0x65, 0x72, + 0x5f, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x52, 0x65, 0x73, + 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x0f, 0x5a, 0x0d, 0x2e, 0x2f, 0x75, 0x73, 0x65, 0x72, 0x5f, + 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -145,14 +204,15 @@ func file_service_user_center_rpc_user_center_proto_rawDescGZIP() []byte { return file_service_user_center_rpc_user_center_proto_rawDescData } -var file_service_user_center_rpc_user_center_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_service_user_center_rpc_user_center_proto_msgTypes = make([]protoimpl.MessageInfo, 3) var file_service_user_center_rpc_user_center_proto_goTypes = []interface{}{ - (*Request)(nil), // 0: user_center.Request - (*Response)(nil), // 1: user_center.Response + (*EmptyRequest)(nil), // 0: user_center.EmptyRequest + (*EmptyResponse)(nil), // 1: user_center.EmptyResponse + (*CreateUserRequest)(nil), // 2: user_center.CreateUserRequest } var file_service_user_center_rpc_user_center_proto_depIdxs = []int32{ - 0, // 0: user_center.User_center.Ping:input_type -> user_center.Request - 1, // 1: user_center.User_center.Ping:output_type -> user_center.Response + 2, // 0: user_center.User_center.CreateUser:input_type -> user_center.CreateUserRequest + 1, // 1: user_center.User_center.CreateUser:output_type -> user_center.EmptyResponse 1, // [1:2] is the sub-list for method output_type 0, // [0:1] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name @@ -167,7 +227,7 @@ func file_service_user_center_rpc_user_center_proto_init() { } if !protoimpl.UnsafeEnabled { file_service_user_center_rpc_user_center_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Request); i { + switch v := v.(*EmptyRequest); i { case 0: return &v.state case 1: @@ -179,7 +239,19 @@ func file_service_user_center_rpc_user_center_proto_init() { } } file_service_user_center_rpc_user_center_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Response); i { + switch v := v.(*EmptyResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_service_user_center_rpc_user_center_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*CreateUserRequest); i { case 0: return &v.state case 1: @@ -197,7 +269,7 @@ func file_service_user_center_rpc_user_center_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_service_user_center_rpc_user_center_proto_rawDesc, NumEnums: 0, - NumMessages: 2, + NumMessages: 3, NumExtensions: 0, NumServices: 1, }, diff --git a/service/user_center/rpc/user_center/user_center_grpc.pb.go b/service/user_center/rpc/user_center/user_center_grpc.pb.go index 362089e..17afdeb 100644 --- a/service/user_center/rpc/user_center/user_center_grpc.pb.go +++ b/service/user_center/rpc/user_center/user_center_grpc.pb.go @@ -19,14 +19,14 @@ import ( const _ = grpc.SupportPackageIsVersion7 const ( - UserCenter_Ping_FullMethodName = "/user_center.User_center/Ping" + UserCenter_CreateUser_FullMethodName = "/user_center.User_center/CreateUser" ) // UserCenterClient is the client API for UserCenter service. // // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. type UserCenterClient interface { - Ping(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) + CreateUser(ctx context.Context, in *CreateUserRequest, opts ...grpc.CallOption) (*EmptyResponse, error) } type userCenterClient struct { @@ -37,9 +37,9 @@ func NewUserCenterClient(cc grpc.ClientConnInterface) UserCenterClient { return &userCenterClient{cc} } -func (c *userCenterClient) Ping(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) { - out := new(Response) - err := c.cc.Invoke(ctx, UserCenter_Ping_FullMethodName, in, out, opts...) +func (c *userCenterClient) CreateUser(ctx context.Context, in *CreateUserRequest, opts ...grpc.CallOption) (*EmptyResponse, error) { + out := new(EmptyResponse) + err := c.cc.Invoke(ctx, UserCenter_CreateUser_FullMethodName, in, out, opts...) if err != nil { return nil, err } @@ -50,7 +50,7 @@ func (c *userCenterClient) Ping(ctx context.Context, in *Request, opts ...grpc.C // All implementations must embed UnimplementedUserCenterServer // for forward compatibility type UserCenterServer interface { - Ping(context.Context, *Request) (*Response, error) + CreateUser(context.Context, *CreateUserRequest) (*EmptyResponse, error) mustEmbedUnimplementedUserCenterServer() } @@ -58,8 +58,8 @@ type UserCenterServer interface { type UnimplementedUserCenterServer struct { } -func (UnimplementedUserCenterServer) Ping(context.Context, *Request) (*Response, error) { - return nil, status.Errorf(codes.Unimplemented, "method Ping not implemented") +func (UnimplementedUserCenterServer) CreateUser(context.Context, *CreateUserRequest) (*EmptyResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method CreateUser not implemented") } func (UnimplementedUserCenterServer) mustEmbedUnimplementedUserCenterServer() {} @@ -74,20 +74,20 @@ func RegisterUserCenterServer(s grpc.ServiceRegistrar, srv UserCenterServer) { s.RegisterService(&UserCenter_ServiceDesc, srv) } -func _UserCenter_Ping_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(Request) +func _UserCenter_CreateUser_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(CreateUserRequest) if err := dec(in); err != nil { return nil, err } if interceptor == nil { - return srv.(UserCenterServer).Ping(ctx, in) + return srv.(UserCenterServer).CreateUser(ctx, in) } info := &grpc.UnaryServerInfo{ Server: srv, - FullMethod: UserCenter_Ping_FullMethodName, + FullMethod: UserCenter_CreateUser_FullMethodName, } handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(UserCenterServer).Ping(ctx, req.(*Request)) + return srv.(UserCenterServer).CreateUser(ctx, req.(*CreateUserRequest)) } return interceptor(ctx, in, info, handler) } @@ -100,8 +100,8 @@ var UserCenter_ServiceDesc = grpc.ServiceDesc{ HandlerType: (*UserCenterServer)(nil), Methods: []grpc.MethodDesc{ { - MethodName: "Ping", - Handler: _UserCenter_Ping_Handler, + MethodName: "CreateUser", + Handler: _UserCenter_CreateUser_Handler, }, }, Streams: []grpc.StreamDesc{}, diff --git a/service/user_center/rpc/user_center_client/user_center.go b/service/user_center/rpc/user_center_client/user_center.go index 8efe768..0cc0779 100644 --- a/service/user_center/rpc/user_center_client/user_center.go +++ b/service/user_center/rpc/user_center_client/user_center.go @@ -13,11 +13,12 @@ import ( ) type ( - Request = user_center.Request - Response = user_center.Response + CreateUserRequest = user_center.CreateUserRequest + EmptyRequest = user_center.EmptyRequest + EmptyResponse = user_center.EmptyResponse UserCenter interface { - Ping(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) + CreateUser(ctx context.Context, in *CreateUserRequest, opts ...grpc.CallOption) (*EmptyResponse, error) } defaultUserCenter struct { @@ -31,7 +32,7 @@ func NewUserCenter(cli zrpc.Client) UserCenter { } } -func (m *defaultUserCenter) Ping(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) { +func (m *defaultUserCenter) CreateUser(ctx context.Context, in *CreateUserRequest, opts ...grpc.CallOption) (*EmptyResponse, error) { client := user_center.NewUserCenterClient(m.cli.Conn()) - return client.Ping(ctx, in, opts...) + return client.CreateUser(ctx, in, opts...) }