├── .gitignore ├── LICENSE ├── README.md ├── authentication ├── Makefile ├── cmd │ ├── auth-api.go │ └── login-grpc.go ├── go.mod ├── go.sum ├── handlers │ ├── commonFunctions.go │ └── loginHandler.go ├── help.txt ├── models │ ├── JwtClaims.go │ ├── flags.go │ ├── genricModels.go │ ├── loginRequest.go │ └── user.go ├── repository │ └── loginRepository.go ├── routers │ └── routers.go ├── rpc │ ├── login.proto │ └── tokenValidate.proto ├── services │ ├── LoginRpcServer.go │ ├── ValidateRpcServer.go │ └── loginService.go └── token │ └── token.go ├── employee ├── add │ ├── add.employee.handler.go │ ├── add.employee.router.go │ └── add.employee.service.go ├── cmd │ └── employee-api.go ├── common │ └── commonFunctions.handler.go ├── data │ ├── employee.data.go │ ├── employee.model.go │ ├── flags.go │ └── genricModels.go ├── delete │ ├── delete.employee.handler.go │ ├── delete.employee.router.go │ └── delete.employee.service.go ├── get │ ├── get.employee.handler.go │ ├── get.employee.router.go │ └── get.employee.service.go ├── go.mod ├── go.sum ├── persistance │ ├── contract.go │ └── employee.mongodb.go └── update │ ├── update.employee.handler.go │ ├── update.employee.router.go │ └── update.employee.service.go ├── rpc ├── go.mod └── rpc_auth │ ├── login.pb.go │ ├── login_grpc.pb.go │ ├── tokenValidate.pb.go │ └── tokenValidate_grpc.pb.go ├── task ├── add │ ├── add.task.handler.go │ ├── add.task.router.go │ └── add.task.service.go ├── cmd │ └── task-api.go ├── common │ └── commonFunctions.handler.go ├── data │ ├── flags.go │ ├── genricModels.go │ ├── task.data.go │ └── task.model.go ├── delete │ ├── delete.task.handler.go │ ├── delete.task.router.go │ └── delete.task.service.go ├── get │ ├── get.task.handler.go │ ├── get.task.router.go │ └── get.task.service.go ├── go.mod ├── go.sum ├── persistance │ ├── contract.go │ └── task.mongodb.go └── update │ ├── update.task.handler.go │ ├── update.task.router.go │ └── update.task.service.go └── testGrpc ├── go.mod ├── go.sum └── main.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, built with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | # Dependency directories (remove the comment below to include it) 15 | # vendor/ 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 architagr 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # golang-microservice-tutorial 2 | I will place all code related to each video I make, on my channel (The Exception Handler). This repository includes step by step how to make a microservice in golang 3 | -------------------------------------------------------------------------------- /authentication/Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: generate-client 2 | 3 | generate-client: 4 | protoc -I rpc/ rpc/login.proto --go_out=../rpc/ --go-grpc_out=../rpc/ 5 | protoc -I rpc/ rpc/tokenValidate.proto --go_out=../rpc/ --go-grpc_out=../rpc/ -------------------------------------------------------------------------------- /authentication/cmd/auth-api.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/authentication/models" 5 | "github.com/architagr/golang-microservice-tutorial/authentication/routers" 6 | "flag" 7 | "fmt" 8 | "log" 9 | "os" 10 | ) 11 | 12 | var ( 13 | port = flag.String("port", "8080", "port to be used") 14 | ip = flag.String("ip", "localhost", "ip to be used") 15 | ) 16 | 17 | func main() { 18 | flag.Parse() 19 | flags := models.NewFlags(*ip, *port) 20 | 21 | fmt.Println("Starting Api") 22 | 23 | logger := log.New(os.Stdout, "auth", 1) 24 | route := routers.NewRoute(logger, flags) 25 | engine := route.RegisterRoutes() 26 | 27 | url, _ := flags.GetApplicationUrl() 28 | engine.Run(*url) 29 | } 30 | -------------------------------------------------------------------------------- /authentication/cmd/login-grpc.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "log" 7 | "net" 8 | 9 | "github.com/architagr/golang-microservice-tutorial/authentication/models" 10 | "github.com/architagr/golang-microservice-tutorial/authentication/services" 11 | "github.com/architagr/golang-microservice-tutorial/authentication/token" 12 | rpc_auth "github.com/architagr/golang-microservice-tutorial/rpc/rpc_auth" 13 | 14 | "google.golang.org/grpc" 15 | ) 16 | 17 | var ( 18 | port = flag.String("port", "8080", "port to be used") 19 | ip = flag.String("ip", "localhost", "ip to be used") 20 | ) 21 | var flags *models.Flags 22 | 23 | func main() { 24 | flag.Parse() 25 | flags = models.NewFlags(*ip, *port) 26 | token.Init() 27 | url, _ := flags.GetApplicationUrl() 28 | 29 | lis, err := net.Listen("tcp", *url) 30 | if err != nil { 31 | log.Fatalf("failed to listen: %v", err) 32 | } 33 | var opts []grpc.ServerOption 34 | 35 | grpcServer := grpc.NewServer(opts...) 36 | rpc_auth.RegisterLoginServiceServer(grpcServer, &services.LoginRpcServer{}) 37 | rpc_auth.RegisterValidateTokenServiceServer(grpcServer,&services.ValidateRpcServer{}) 38 | fmt.Println("starting grpc server on", *url) 39 | err1 := grpcServer.Serve(lis) 40 | if err1 == nil { 41 | fmt.Println("grpc server running on", *url) 42 | }else{ 43 | fmt.Println("grpc server running error on", err1) 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /authentication/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/architagr/golang-microservice-tutorial/authentication 2 | 3 | go 1.16 4 | 5 | require ( 6 | github.com/architagr/golang-microservice-tutorial/rpc v0.0.0-00010101000000-000000000000 7 | github.com/dgrijalva/jwt-go v3.2.0+incompatible 8 | github.com/gin-gonic/gin v1.7.2 9 | google.golang.org/grpc v1.39.0 10 | google.golang.org/protobuf v1.27.1 // indirect 11 | ) 12 | 13 | replace github.com/architagr/golang-microservice-tutorial/rpc => ../rpc 14 | -------------------------------------------------------------------------------- /authentication/go.sum: -------------------------------------------------------------------------------- 1 | cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= 2 | cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= 3 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 4 | github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= 5 | github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= 6 | github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= 7 | github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= 8 | github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= 9 | github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= 10 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 11 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 12 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 13 | github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= 14 | github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= 15 | github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= 16 | github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= 17 | github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= 18 | github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= 19 | github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.mod h1:hliV/p42l8fGbc6Y9bQ70uLwIvmJyVE5k4iMKlh8wCQ= 20 | github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= 21 | github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= 22 | github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= 23 | github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= 24 | github.com/gin-gonic/gin v1.7.2 h1:Tg03T9yM2xa8j6I3Z3oqLaQRSmKvxPd6g/2HJ6zICFA= 25 | github.com/gin-gonic/gin v1.7.2/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY= 26 | github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= 27 | github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= 28 | github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= 29 | github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= 30 | github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= 31 | github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= 32 | github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE= 33 | github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= 34 | github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= 35 | github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= 36 | github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 37 | github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 38 | github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= 39 | github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= 40 | github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= 41 | github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= 42 | github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= 43 | github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= 44 | github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= 45 | github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= 46 | github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= 47 | github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4= 48 | github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= 49 | github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= 50 | github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= 51 | github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= 52 | github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 53 | github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 54 | github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= 55 | github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 56 | github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= 57 | github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 58 | github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= 59 | github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= 60 | github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= 61 | github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= 62 | github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= 63 | github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= 64 | github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= 65 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= 66 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 67 | github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= 68 | github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= 69 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 70 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 71 | github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= 72 | github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= 73 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 74 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 75 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 76 | github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= 77 | github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= 78 | github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= 79 | github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= 80 | github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= 81 | github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= 82 | go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= 83 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 84 | golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI= 85 | golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 86 | golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= 87 | golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= 88 | golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= 89 | golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= 90 | golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 91 | golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 92 | golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 93 | golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 94 | golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 95 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 96 | golang.org/x/net v0.0.0-20200822124328-c89045814202 h1:VvcQYSHwXgi7W+TpUR6A9g6Up98WAHf3f/ulnJ62IyA= 97 | golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= 98 | golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= 99 | golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= 100 | golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 101 | golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 102 | golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 103 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 104 | golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 105 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 106 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 107 | golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 108 | golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884= 109 | golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 110 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 111 | golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= 112 | golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= 113 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 114 | golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 115 | golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= 116 | golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= 117 | golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= 118 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 119 | golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= 120 | golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 121 | google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= 122 | google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= 123 | google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= 124 | google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= 125 | google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= 126 | google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 h1:+kGHl1aib/qcwaRi1CbqBZ1rk19r85MNUf8HaBghugY= 127 | google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= 128 | google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= 129 | google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= 130 | google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= 131 | google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= 132 | google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0= 133 | google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= 134 | google.golang.org/grpc v1.39.0 h1:Klz8I9kdtkIN6EpHHUOMLCYhTn/2WAe5a0s1hcBkdTI= 135 | google.golang.org/grpc v1.39.0/go.mod h1:PImNr+rS9TWYb2O4/emRugxiyHZ5JyHW5F+RPnDzfrE= 136 | google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= 137 | google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= 138 | google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= 139 | google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= 140 | google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= 141 | google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= 142 | google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= 143 | google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= 144 | google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= 145 | google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= 146 | google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= 147 | google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= 148 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 149 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 150 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 151 | gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 152 | gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= 153 | gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 154 | honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= 155 | honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= 156 | -------------------------------------------------------------------------------- /authentication/handlers/commonFunctions.go: -------------------------------------------------------------------------------- 1 | package handlers 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/authentication/models" 5 | "net/http" 6 | 7 | "github.com/gin-gonic/gin" 8 | ) 9 | 10 | func ok(context *gin.Context, status int, message string, data interface{}) { 11 | context.AbortWithStatusJSON(status, models.Response{ 12 | Data: data, 13 | Status: status, 14 | Message: message, 15 | }) 16 | } 17 | func badRequest(context *gin.Context, status int, message string, errors []models.ErrorDetail) { 18 | context.AbortWithStatusJSON(status, models.Response{ 19 | Error: errors, 20 | Status: status, 21 | Message: message, 22 | }) 23 | } 24 | 25 | func returnUnauthorized(context *gin.Context) { 26 | context.AbortWithStatusJSON(http.StatusUnauthorized, models.Response{ 27 | Error: []models.ErrorDetail{ 28 | { 29 | ErrorType: models.ErrorTypeUnauthorized, 30 | ErrorMessage: "You are not authorized to access this path", 31 | }, 32 | }, 33 | Status: http.StatusUnauthorized, 34 | Message: "Unauthorized access", 35 | }) 36 | } 37 | -------------------------------------------------------------------------------- /authentication/handlers/loginHandler.go: -------------------------------------------------------------------------------- 1 | package handlers 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/authentication/models" 5 | "github.com/architagr/golang-microservice-tutorial/authentication/services" 6 | "fmt" 7 | "log" 8 | "net/http" 9 | 10 | "github.com/gin-gonic/gin" 11 | ) 12 | 13 | type Login struct { 14 | logger *log.Logger 15 | flags *models.Flags 16 | loginService *services.Login 17 | } 18 | 19 | func NewLogin(l *log.Logger, f *models.Flags) *Login { 20 | loginService := services.NewLogin(l, f) 21 | return &Login{ 22 | logger: l, 23 | flags: f, 24 | loginService: loginService, 25 | } 26 | } 27 | 28 | func (l *Login) Login(context *gin.Context) { 29 | 30 | var loginObj models.LoginRequest 31 | if err := context.ShouldBindJSON(&loginObj); err != nil { 32 | var errors []models.ErrorDetail = make([]models.ErrorDetail, 0, 1) 33 | errors = append(errors, models.ErrorDetail{ 34 | ErrorType: models.ErrorTypeValidation, 35 | ErrorMessage: fmt.Sprintf("%v", err), 36 | }) 37 | badRequest(context, http.StatusBadRequest, "invalid request", errors) 38 | return 39 | } 40 | tokeString, err:=l.loginService.GetToken(loginObj, context.Request.Header.Get("Referer")) 41 | 42 | if err != nil { 43 | badRequest(context, http.StatusBadRequest, "error in gerating token", []models.ErrorDetail{ 44 | *err, 45 | }) 46 | return 47 | } 48 | 49 | ok(context, http.StatusOK, "token created", tokeString) 50 | } 51 | 52 | func (l *Login) VerifyToken(context *gin.Context) { 53 | tokenString := context.Request.Header.Get("apikey") 54 | referer := context.Request.Header.Get("Referer") 55 | 56 | valid, claims := l.loginService.VerifyToken(tokenString, referer) 57 | if !valid { 58 | returnUnauthorized(context) 59 | return 60 | } 61 | ok(context, http.StatusOK, "token is valid", claims) 62 | } 63 | -------------------------------------------------------------------------------- /authentication/help.txt: -------------------------------------------------------------------------------- 1 | ginEngine.POST("/verifyToken", r.loginHandler.VerifyToken) 2 | 3 | func (l *Login) VerifyToken(context *gin.Context) { 4 | tokenString := context.Request.Header.Get("apikey") 5 | referer := context.Request.Header.Get("Referer") 6 | 7 | valid, claims := l.loginService.VerifyToken(tokenString, referer) 8 | if !valid { 9 | returnUnauthorized(context) 10 | return 11 | } 12 | ok(context, http.StatusOK, "token is valid", claims) 13 | } 14 | 15 | -------------------------------------------------------------------------------- /authentication/models/JwtClaims.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | "time" 7 | 8 | jwt "github.com/dgrijalva/jwt-go" 9 | ) 10 | 11 | type JwtClaims struct { 12 | ComapnyId string `json:"comapnyId,omitempty"` 13 | Username string `json:"username,omitempty"` 14 | Roles []int `json:"roles,omitempty"` 15 | jwt.StandardClaims 16 | } 17 | 18 | func (claims JwtClaims) Valid() error { 19 | var now = time.Now().UTC().Unix() 20 | flags, _ := GetFlags() 21 | url, _ := flags.GetApplicationUrl() 22 | if claims.VerifyExpiresAt(now, true) && claims.VerifyIssuer(*url, true) { 23 | return nil 24 | } 25 | return fmt.Errorf("Token is invalid") 26 | } 27 | 28 | func (claims JwtClaims) VerifyAudience(origin string) bool { 29 | return strings.Compare(claims.Audience, origin) == 0 30 | } 31 | -------------------------------------------------------------------------------- /authentication/models/flags.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | import "fmt" 4 | 5 | type Flags struct { 6 | Ip string 7 | Port string 8 | } 9 | 10 | func (f *Flags) GetApplicationUrl() (*string, *ErrorDetail) { 11 | f, err := GetFlags() 12 | if err != nil { 13 | return nil, err 14 | } 15 | url := fmt.Sprintf("%s:%s", f.Ip, f.Port) 16 | return &url, nil 17 | } 18 | 19 | var flagsObj *Flags 20 | 21 | func NewFlags(ip, port string) *Flags { 22 | if flagsObj == nil { 23 | flagsObj = &Flags{ 24 | Ip: ip, 25 | Port: port, 26 | } 27 | } 28 | return flagsObj 29 | } 30 | 31 | func GetFlags() (*Flags, *ErrorDetail) { 32 | if flagsObj == nil { 33 | return nil, &ErrorDetail{ 34 | ErrorType: ErrorTypeFatal, 35 | ErrorMessage: "Flags not set", 36 | } 37 | } 38 | 39 | return flagsObj, nil 40 | } 41 | -------------------------------------------------------------------------------- /authentication/models/genricModels.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | import "fmt" 4 | 5 | const ( 6 | ErrorTypeFatal = "Fatal" 7 | ErrorTypeError = "Error" 8 | ErrorTypeValidation = "Validation Error" 9 | ErrorTypeInfo = "Info" 10 | ErrorTypeDebug = "Debug" 11 | ErrorTypeUnauthorized = "Unauthorized" 12 | ) 13 | 14 | type ErrorDetail struct { 15 | ErrorType string 16 | ErrorMessage string 17 | } 18 | 19 | func (err *ErrorDetail) Error() string { 20 | return fmt.Sprintf("ErrorType: %s, Error Message: %s", err.ErrorType, err.ErrorMessage) 21 | } 22 | 23 | type Response struct { 24 | Data interface{} 25 | Status int 26 | Error []ErrorDetail 27 | Message string 28 | } -------------------------------------------------------------------------------- /authentication/models/loginRequest.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | type LoginRequest struct { 4 | UserName string `json:"UserName" form:"UserName" binding:"required"` 5 | Password string `json:"Password" form:"Password" binding:"required"` 6 | RememberMe bool `json:"RememberMe" form:"RememberMe"` 7 | } -------------------------------------------------------------------------------- /authentication/models/user.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | type User struct { 4 | Id int 5 | Name string 6 | UserName string 7 | Password string 8 | Roles []int 9 | } 10 | -------------------------------------------------------------------------------- /authentication/repository/loginRepository.go: -------------------------------------------------------------------------------- 1 | package repository 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/authentication/models" 5 | ) 6 | 7 | type Login struct{ 8 | 9 | } 10 | 11 | var user []models.User 12 | 13 | func Init() *Login{ 14 | 15 | user = make([]models.User, 0,2) 16 | 17 | user = append(user, 18 | models.User{ 19 | Id: 1, 20 | Name: "Steve", 21 | UserName: "steve@yopmail.com", 22 | Password: "steve123", 23 | Roles: []int{1,2,3}, 24 | }, 25 | models.User{ 26 | Id: 2, 27 | Name: "Mark", 28 | UserName: "mark@yopmail.com", 29 | Password: "mark@123", 30 | Roles: []int{4}, 31 | }, 32 | ) 33 | 34 | return &Login{} 35 | } 36 | 37 | func (l *Login) GetUserByUserName(userName, password string) (models.User, *models.ErrorDetail){ 38 | for _, value:= range user{ 39 | if value.UserName == userName && value.Password == password{ 40 | return value, nil 41 | } 42 | } 43 | 44 | return models.User{}, &models.ErrorDetail{ 45 | ErrorType: models.ErrorTypeError, 46 | ErrorMessage: "UserName and password not valid", 47 | 48 | } 49 | } 50 | 51 | 52 | -------------------------------------------------------------------------------- /authentication/routers/routers.go: -------------------------------------------------------------------------------- 1 | package routers 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/authentication/handlers" 5 | "github.com/architagr/golang-microservice-tutorial/authentication/models" 6 | "github.com/architagr/golang-microservice-tutorial/authentication/token" 7 | "log" 8 | 9 | "github.com/gin-gonic/gin" 10 | ) 11 | 12 | type Login struct { 13 | logger *log.Logger 14 | loginHandler *handlers.Login 15 | flags *models.Flags 16 | } 17 | 18 | func NewRoute(l *log.Logger, f *models.Flags) *Login { 19 | loginHandler := handlers.NewLogin(l, f) 20 | token.Init() 21 | 22 | return &Login{ 23 | logger: l, 24 | loginHandler: loginHandler, 25 | flags: f, 26 | } 27 | } 28 | 29 | func (r *Login) RegisterRoutes() *gin.Engine { 30 | ginEngine := gin.Default() 31 | ginEngine.POST("/login", r.loginHandler.Login) 32 | ginEngine.POST("/verifyToken", r.loginHandler.VerifyToken) 33 | return ginEngine 34 | } 35 | -------------------------------------------------------------------------------- /authentication/rpc/login.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package rpc_auth; 3 | option go_package = "./rpc_auth"; 4 | 5 | message LoginRequest{ 6 | string Username = 1; 7 | string Password = 2; 8 | bool RememberMe = 3; 9 | } 10 | 11 | message LoginResponse{ 12 | LoginRequest loginDetails = 1; 13 | string token = 2; 14 | } 15 | message LoginRequestList{ 16 | repeated LoginRequest data =1; 17 | } 18 | 19 | message LoginResponseList{ 20 | repeated LoginResponse data =1; 21 | } 22 | 23 | service LoginService{ 24 | rpc LoginSimpleRPC(LoginRequest) returns (LoginResponse){} 25 | rpc LoginServerStreamRPC(LoginRequestList) returns (stream LoginResponse){} 26 | rpc LoginClientStreamRPC(stream LoginRequest) returns (LoginResponseList){} 27 | rpc LoginBiDirectionalRPC(stream LoginRequest) returns (stream LoginResponse){} 28 | } 29 | 30 | 31 | -------------------------------------------------------------------------------- /authentication/rpc/tokenValidate.proto: -------------------------------------------------------------------------------- 1 | syntax="proto3"; 2 | 3 | package rpc_auth; 4 | option go_package = "./rpc_auth"; 5 | 6 | message ValidateTokenRequest { 7 | string Token = 1; 8 | } 9 | 10 | message ValidateTokenResponse { 11 | bool IsValid = 1; 12 | string ComapnyId =2; 13 | string Username = 3; 14 | repeated int32 Roles = 4; 15 | } 16 | 17 | service ValidateTokenService{ 18 | rpc Validate(stream ValidateTokenRequest) returns (stream ValidateTokenResponse){} 19 | } -------------------------------------------------------------------------------- /authentication/services/LoginRpcServer.go: -------------------------------------------------------------------------------- 1 | package services 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "io" 7 | "log" 8 | "os" 9 | 10 | "github.com/architagr/golang-microservice-tutorial/authentication/models" 11 | rpc_auth "github.com/architagr/golang-microservice-tutorial/rpc/rpc_auth" 12 | ) 13 | 14 | type LoginRpcServer struct { 15 | rpc_auth.UnimplementedLoginServiceServer 16 | } 17 | 18 | func (LoginRpcServer) LoginSimpleRPC(ctx context.Context, in *rpc_auth.LoginRequest) (*rpc_auth.LoginResponse, error) { 19 | logger := log.New(os.Stdout, "loginRpc", 1) 20 | flags, err := models.GetFlags() 21 | if err != nil { 22 | return nil, err 23 | } 24 | service := NewLogin(logger, flags) 25 | loginModel := models.LoginRequest{ 26 | UserName: in.Username, 27 | Password: in.Password, 28 | RememberMe: in.RememberMe, 29 | } 30 | token, err := service.GetToken(loginModel, "") 31 | 32 | if err != nil { 33 | return nil, err 34 | } 35 | return &rpc_auth.LoginResponse{ 36 | Token: token, 37 | }, nil 38 | } 39 | 40 | func (LoginRpcServer) LoginServerStreamRPC(in *rpc_auth.LoginRequestList, stream rpc_auth.LoginService_LoginServerStreamRPCServer) error { 41 | for _, loginCred := range in.Data { 42 | logger := log.New(os.Stdout, "loginRpc", 1) 43 | flags, err := models.GetFlags() 44 | if err != nil { 45 | return err 46 | } 47 | 48 | service := NewLogin(logger, flags) 49 | loginModel := models.LoginRequest{ 50 | UserName: loginCred.Username, 51 | Password: loginCred.Password, 52 | RememberMe: loginCred.RememberMe, 53 | } 54 | token, err := service.GetToken(loginModel, "") 55 | 56 | if err != nil { 57 | return err 58 | } 59 | if err := stream.Send(&rpc_auth.LoginResponse{ 60 | LoginDetails: loginCred, 61 | Token: token, 62 | }); err != nil { 63 | return err 64 | } 65 | 66 | } 67 | return nil 68 | } 69 | 70 | func (LoginRpcServer) LoginClientStreamRPC(stream rpc_auth.LoginService_LoginClientStreamRPCServer) error { 71 | result := &rpc_auth.LoginResponseList{ 72 | Data: make([]*rpc_auth.LoginResponse, 0, 10), 73 | } 74 | logger := log.New(os.Stdout, "loginRpc", 1) 75 | flags, err := models.GetFlags() 76 | if err != nil { 77 | return err 78 | } 79 | 80 | service := NewLogin(logger, flags) 81 | for { 82 | loginCred, err := stream.Recv() 83 | if err == io.EOF { 84 | return stream.SendAndClose(result) 85 | } 86 | loginModel := models.LoginRequest{ 87 | UserName: loginCred.Username, 88 | Password: loginCred.Password, 89 | RememberMe: loginCred.RememberMe, 90 | } 91 | 92 | token, err1 := service.GetToken(loginModel, "") 93 | if err1 != nil { 94 | return err1 95 | } 96 | 97 | result.Data = append(result.Data, &rpc_auth.LoginResponse{ 98 | LoginDetails: loginCred, 99 | Token: token, 100 | }) 101 | if err != nil { 102 | return err 103 | } 104 | } 105 | return nil 106 | } 107 | 108 | func (LoginRpcServer) LoginBiDirectionalRPC(stream rpc_auth.LoginService_LoginBiDirectionalRPCServer) error { 109 | for { 110 | in, err := stream.Recv() 111 | if err == io.EOF { 112 | return nil 113 | } 114 | if err != nil { 115 | fmt.Println("error in reading data", err) 116 | return err 117 | } 118 | logger := log.New(os.Stdout, "loginRpc", 1) 119 | flags, err := models.GetFlags() 120 | if err != nil { 121 | return err 122 | } 123 | 124 | service := NewLogin(logger, flags) 125 | loginModel := models.LoginRequest{ 126 | UserName: in.Username, 127 | Password: in.Password, 128 | RememberMe: in.RememberMe, 129 | } 130 | token, err := service.GetToken(loginModel, "") 131 | 132 | if err != nil { 133 | return err 134 | } 135 | if err := stream.Send(&rpc_auth.LoginResponse{ 136 | LoginDetails: in, 137 | Token: token, 138 | }); err != nil { 139 | return err 140 | } 141 | } 142 | return nil 143 | } 144 | -------------------------------------------------------------------------------- /authentication/services/ValidateRpcServer.go: -------------------------------------------------------------------------------- 1 | package services 2 | 3 | import ( 4 | "io" 5 | "log" 6 | "os" 7 | 8 | "github.com/architagr/golang-microservice-tutorial/authentication/models" 9 | rpc_auth "github.com/architagr/golang-microservice-tutorial/rpc/rpc_auth" 10 | ) 11 | 12 | type ValidateRpcServer struct { 13 | rpc_auth.UnimplementedValidateTokenServiceServer 14 | } 15 | 16 | func (ValidateRpcServer) Validate(stream rpc_auth.ValidateTokenService_ValidateServer) error { 17 | for { 18 | in, err := stream.Recv() 19 | if err == io.EOF { 20 | return nil 21 | } 22 | if err != nil { 23 | return err 24 | } 25 | logger := log.New(os.Stdout, "validateRpc", 1) 26 | flags, errF := models.GetFlags() 27 | if errF != nil { 28 | return errF 29 | } 30 | service := NewLogin(logger, flags) 31 | 32 | valid, claims := service.VerifyToken(in.Token, "") 33 | 34 | if !valid { 35 | if err := stream.Send(&rpc_auth.ValidateTokenResponse{ 36 | IsValid: valid, 37 | ComapnyId: "", 38 | Username: "", 39 | Roles: nil, 40 | }); err != nil { 41 | return err 42 | } 43 | } else { 44 | if err := stream.Send(&rpc_auth.ValidateTokenResponse{ 45 | IsValid: valid, 46 | ComapnyId: claims.ComapnyId, 47 | Username: claims.Username, 48 | Roles: nil, 49 | }); err != nil { 50 | return err 51 | } 52 | } 53 | } 54 | return nil 55 | } 56 | -------------------------------------------------------------------------------- /authentication/services/loginService.go: -------------------------------------------------------------------------------- 1 | package services 2 | 3 | import ( 4 | "log" 5 | "strconv" 6 | "time" 7 | 8 | "github.com/architagr/golang-microservice-tutorial/authentication/models" 9 | "github.com/architagr/golang-microservice-tutorial/authentication/repository" 10 | "github.com/architagr/golang-microservice-tutorial/authentication/token" 11 | "github.com/dgrijalva/jwt-go" 12 | ) 13 | 14 | type Login struct { 15 | logger *log.Logger 16 | flags *models.Flags 17 | loginRepository *repository.Login 18 | } 19 | 20 | func NewLogin(l *log.Logger, f *models.Flags) *Login { 21 | return &Login{ 22 | logger: l, 23 | flags: f, 24 | loginRepository: repository.Init(), 25 | } 26 | } 27 | 28 | func (l *Login) GetToken(loginModel models.LoginRequest, origin string) (string, *models.ErrorDetail) { 29 | user, err := l.loginRepository.GetUserByUserName(loginModel.UserName, loginModel.Password) 30 | if err !=nil{ 31 | return "", err 32 | } 33 | var claims = &models.JwtClaims{ 34 | ComapnyId: strconv.Itoa(user.Id), 35 | Username: user.Name, 36 | Roles: user.Roles, 37 | StandardClaims: jwt.StandardClaims{ 38 | Audience: origin, 39 | }, 40 | } 41 | var tokenCreationTime = time.Now().UTC() 42 | var expirationTime = tokenCreationTime.Add(time.Duration(2) * time.Hour) 43 | return token.GenrateToken(claims, expirationTime) 44 | 45 | } 46 | 47 | func (*Login) VerifyToken(tokenString, referer string) (bool, *models.JwtClaims) { 48 | return token.VerifyToken(tokenString, referer) 49 | } 50 | -------------------------------------------------------------------------------- /authentication/token/token.go: -------------------------------------------------------------------------------- 1 | package token 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | 7 | "github.com/architagr/golang-microservice-tutorial/authentication/models" 8 | 9 | "github.com/dgrijalva/jwt-go" 10 | ) 11 | 12 | func Init() *models.ErrorDetail { 13 | flags, err := models.GetFlags() 14 | if err != nil { 15 | return err 16 | } 17 | 18 | ip, _ = flags.GetApplicationUrl() 19 | return nil 20 | } 21 | 22 | var ip *string 23 | 24 | const ( 25 | jWTPrivateToken = "SecrteTokenSecrteToken" 26 | ) 27 | 28 | func GenrateToken(claims *models.JwtClaims, expirationTime time.Time) (string, *models.ErrorDetail) { 29 | 30 | 31 | claims.ExpiresAt = expirationTime.Unix() 32 | claims.IssuedAt = time.Now().UTC().Unix() 33 | claims.Issuer = *ip 34 | 35 | token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) 36 | 37 | // Sign and get the complete encoded token as a string using the secret 38 | tokenString, err := token.SignedString([]byte(jWTPrivateToken)) 39 | if err != nil { 40 | return "", &models.ErrorDetail{ 41 | ErrorType: models.ErrorTypeError, 42 | ErrorMessage: err.Error(), 43 | } 44 | } 45 | return tokenString, nil 46 | } 47 | 48 | func VerifyToken(tokenString, origin string) (bool, *models.JwtClaims) { 49 | claims := &models.JwtClaims{} 50 | token, _ := getTokenFromString(tokenString, claims) 51 | 52 | if token.Valid { 53 | if claims.VerifyAudience(origin) { 54 | return true, claims 55 | } 56 | } 57 | return false, nil 58 | } 59 | 60 | func GetClaims(tokenString string) models.JwtClaims { 61 | claims := &models.JwtClaims{} 62 | 63 | _, err := getTokenFromString(tokenString, claims) 64 | if err == nil { 65 | return *claims 66 | } 67 | return *claims 68 | } 69 | func getTokenFromString(tokenString string, claims *models.JwtClaims) (*jwt.Token, error) { 70 | return jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) { 71 | // Don't forget to validate the alg is what you expect: 72 | if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { 73 | return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"]) 74 | } 75 | 76 | // hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key") 77 | return []byte(jWTPrivateToken), nil 78 | }) 79 | } 80 | -------------------------------------------------------------------------------- /employee/add/add.employee.handler.go: -------------------------------------------------------------------------------- 1 | package add 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/employee/common" 5 | "github.com/architagr/golang-microservice-tutorial/employee/data" 6 | "fmt" 7 | "net/http" 8 | 9 | "github.com/gin-gonic/gin" 10 | ) 11 | 12 | type Handler struct{ 13 | service *Service 14 | } 15 | 16 | func InitHandler(service *Service) *Handler { 17 | return &Handler{ 18 | service: service, 19 | } 20 | } 21 | 22 | 23 | func (handler * Handler) Add(c *gin.Context){ 24 | var addObj data.Employee 25 | if err := c.ShouldBindJSON(&addObj); err != nil { 26 | common.BadRequest(c, http.StatusBadRequest, fmt.Sprintf("Request has invalid body"), 27 | []data.ErrorDetail{ 28 | data.ErrorDetail{ 29 | ErrorType: data.ErrorTypeError, 30 | ErrorMessage: fmt.Sprintf("Request has invalid body"), 31 | }, 32 | data.ErrorDetail{ 33 | ErrorType: data.ErrorTypeValidation, 34 | ErrorMessage: err.Error(), 35 | }, 36 | }) 37 | return 38 | } 39 | result, errorResponse := handler.service.Add(&addObj) 40 | if errorResponse != nil { 41 | common.BadRequest(c, http.StatusBadRequest, fmt.Sprintf("Error in Adding employee by name %s", addObj.Name), 42 | []data.ErrorDetail{ 43 | *errorResponse, 44 | }) 45 | return 46 | } 47 | 48 | common.Ok(c, http.StatusOK, fmt.Sprintf("successfully Added employees with name %s", addObj.Name), result) 49 | return 50 | } 51 | -------------------------------------------------------------------------------- /employee/add/add.employee.router.go: -------------------------------------------------------------------------------- 1 | package add 2 | 3 | import "github.com/gin-gonic/gin" 4 | 5 | type Router struct { 6 | addHandler *Handler 7 | } 8 | func InitRouter(h *Handler) *Router{ 9 | return &Router{ 10 | addHandler: h, 11 | } 12 | } 13 | func (r *Router) RegisterRoutes(group *gin.RouterGroup) { 14 | group.POST("", r.addHandler.Add) 15 | } 16 | -------------------------------------------------------------------------------- /employee/add/add.employee.service.go: -------------------------------------------------------------------------------- 1 | package add 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/employee/data" 5 | "github.com/architagr/golang-microservice-tutorial/employee/persistance" 6 | ) 7 | 8 | type Service struct { 9 | repository persistance.IEmployeeDbContext 10 | } 11 | 12 | func InitService(repo persistance.IEmployeeDbContext) *Service { 13 | return &Service{ 14 | repository: repo, 15 | } 16 | } 17 | 18 | func (service *Service) Add(emp *data.Employee) (*data.Employee, *data.ErrorDetail) { 19 | return service.repository.Add(emp) 20 | } 21 | -------------------------------------------------------------------------------- /employee/cmd/employee-api.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/employee/add" 5 | "github.com/architagr/golang-microservice-tutorial/employee/data" 6 | "github.com/architagr/golang-microservice-tutorial/employee/delete" 7 | "github.com/architagr/golang-microservice-tutorial/employee/get" 8 | "github.com/architagr/golang-microservice-tutorial/employee/persistance" 9 | "github.com/architagr/golang-microservice-tutorial/employee/update" 10 | 11 | "flag" 12 | 13 | "github.com/gin-gonic/gin" 14 | ) 15 | 16 | var ( 17 | port = flag.String("port", "8080", "port to be used") 18 | ip = flag.String("ip", "localhost", "ip to be used") 19 | ) 20 | 21 | func main() { 22 | flag.Parse() 23 | flags := data.NewFlags(*ip, *port) 24 | url, _ := flags.GetApplicationUrl() 25 | ginR := gin.Default() 26 | ginR.Use(CORSMiddleware()) 27 | group := ginR.Group("api/employees") 28 | 29 | repo := getPersistanceObj() 30 | registerGetRoutes(group, repo) 31 | registerPutRoutes(group, repo) 32 | registerAddRoutes(group, repo) 33 | registerDeleteRoutes(group, repo) 34 | 35 | ginR.Run(*url) 36 | } 37 | func CORSMiddleware() gin.HandlerFunc { 38 | return func(c *gin.Context) { 39 | 40 | c.Header("Access-Control-Allow-Origin", "*") 41 | c.Header("Access-Control-Allow-Credentials", "true") 42 | c.Header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With") 43 | c.Header("Access-Control-Allow-Methods", "POST,HEAD,PATCH, OPTIONS, GET, PUT") 44 | 45 | if c.Request.Method == "OPTIONS" { 46 | c.AbortWithStatus(204) 47 | return 48 | } 49 | 50 | c.Next() 51 | } 52 | } 53 | func getPersistanceObj() persistance.IEmployeeDbContext { 54 | return persistance.InitMongoDb("", "") 55 | } 56 | 57 | func registerGetRoutes(group *gin.RouterGroup, repo persistance.IEmployeeDbContext) { 58 | service := get.InitService(repo) 59 | handler := get.InitHandler(service) 60 | getRouter := get.InitRouter(handler) 61 | getRouter.RegisterRoutes(group) 62 | } 63 | 64 | func registerPutRoutes(group *gin.RouterGroup, repo persistance.IEmployeeDbContext) { 65 | service := update.InitService(repo) 66 | handler := update.InitHandler(service) 67 | putRouter := update.InitRouter(handler) 68 | putRouter.RegisterRoutes(group) 69 | } 70 | 71 | func registerAddRoutes(group *gin.RouterGroup, repo persistance.IEmployeeDbContext) { 72 | service := add.InitService(repo) 73 | handler := add.InitHandler(service) 74 | addRouter := add.InitRouter(handler) 75 | addRouter.RegisterRoutes(group) 76 | } 77 | 78 | func registerDeleteRoutes(group *gin.RouterGroup, repo persistance.IEmployeeDbContext) { 79 | service := delete.InitService(repo) 80 | handler := delete.InitHandler(service) 81 | deleteRouter := delete.InitRouter(handler) 82 | deleteRouter.RegisterRoutes(group) 83 | } 84 | -------------------------------------------------------------------------------- /employee/common/commonFunctions.handler.go: -------------------------------------------------------------------------------- 1 | package common 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/employee/data" 5 | "net/http" 6 | 7 | "github.com/gin-gonic/gin" 8 | ) 9 | 10 | func Ok(context *gin.Context, status int, message string, result interface{}) { 11 | context.AbortWithStatusJSON(status, data.Response{ 12 | Data: result, 13 | Status: status, 14 | Message: message, 15 | }) 16 | } 17 | func BadRequest(context *gin.Context, status int, message string, errors []data.ErrorDetail) { 18 | context.AbortWithStatusJSON(status, data.Response{ 19 | Error: errors, 20 | Status: status, 21 | Message: message, 22 | }) 23 | } 24 | 25 | func ReturnUnauthorized(context *gin.Context) { 26 | context.AbortWithStatusJSON(http.StatusUnauthorized, data.Response{ 27 | Error: []data.ErrorDetail{ 28 | { 29 | ErrorType: data.ErrorTypeUnauthorized, 30 | ErrorMessage: "You are not authorized to access this path", 31 | }, 32 | }, 33 | Status: http.StatusUnauthorized, 34 | Message: "Unauthorized access", 35 | }) 36 | } 37 | -------------------------------------------------------------------------------- /employee/data/employee.data.go: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | var Emp []Employee 4 | 5 | func InitEmpData() { 6 | Emp = []Employee{ 7 | Employee{ 8 | ID: 1, 9 | Name: "Employee 1", 10 | Role: []int{1, 2, 3}, 11 | Department: "Accounts", 12 | }, 13 | Employee{ 14 | ID: 2, 15 | Name: "Employee 2", 16 | Role: []int{4}, 17 | Department: "Sales", 18 | }, 19 | Employee{ 20 | ID: 3, 21 | Name: "Employee 3", 22 | Role: []int{1, 2, 4}, 23 | Department: "Marketing", 24 | }, 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /employee/data/employee.model.go: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | type Employee struct { 4 | ID int `json:"id" uri:"id" form:"id"` 5 | Name string `json:"name" form:"name"` 6 | Role []int `json:"roles" form:"roles"` 7 | Department string `json:"department" form:"department"` 8 | } 9 | -------------------------------------------------------------------------------- /employee/data/flags.go: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | import "fmt" 4 | 5 | type Flags struct { 6 | Ip string 7 | Port string 8 | } 9 | 10 | func (f *Flags) GetApplicationUrl() (*string, *ErrorDetail) { 11 | f, err := GetFlags() 12 | if err != nil { 13 | return nil, err 14 | } 15 | url := fmt.Sprintf("%s:%s", f.Ip, f.Port) 16 | return &url, nil 17 | } 18 | 19 | var flagsObj *Flags 20 | 21 | func NewFlags(ip, port string) *Flags { 22 | if flagsObj == nil { 23 | flagsObj = &Flags{ 24 | Ip: ip, 25 | Port: port, 26 | } 27 | } 28 | return flagsObj 29 | } 30 | 31 | func GetFlags() (*Flags, *ErrorDetail) { 32 | if flagsObj == nil { 33 | return nil, &ErrorDetail{ 34 | ErrorType: ErrorTypeFatal, 35 | ErrorMessage: "Flags not set", 36 | } 37 | } 38 | 39 | return flagsObj, nil 40 | } 41 | -------------------------------------------------------------------------------- /employee/data/genricModels.go: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | import "fmt" 4 | 5 | const ( 6 | ErrorTypeFatal = "Fatal" 7 | ErrorTypeError = "Error" 8 | ErrorTypeValidation = "Validation Error" 9 | ErrorTypeInfo = "Info" 10 | ErrorTypeDebug = "Debug" 11 | ErrorTypeUnauthorized = "Unauthorized" 12 | ) 13 | 14 | type ErrorDetail struct { 15 | ErrorType string `json:"errorType,omitempty"` 16 | ErrorMessage string `json:"errorMessage,omitempty"` 17 | } 18 | 19 | func (err *ErrorDetail) Error() string { 20 | return fmt.Sprintf("ErrorType: %s, Error Message: %s", err.ErrorType, err.ErrorMessage) 21 | } 22 | 23 | type Response struct { 24 | Data interface{} `json:"data,omitempty"` 25 | Status int `json:"status,omitempty"` 26 | Error []ErrorDetail `json:"errors,omitempty"` 27 | Message string `json:"message,omitempty"` 28 | } 29 | -------------------------------------------------------------------------------- /employee/delete/delete.employee.handler.go: -------------------------------------------------------------------------------- 1 | package delete 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/employee/common" 5 | "github.com/architagr/golang-microservice-tutorial/employee/data" 6 | "fmt" 7 | "net/http" 8 | "strconv" 9 | 10 | "github.com/gin-gonic/gin" 11 | ) 12 | 13 | type Handler struct{ 14 | service *Service 15 | } 16 | 17 | func InitHandler(service *Service) *Handler { 18 | return &Handler{ 19 | service: service, 20 | } 21 | } 22 | 23 | 24 | func (handler * Handler) Delete(c *gin.Context){ 25 | id, err := strconv.Atoi(c.Param("id")) 26 | if err != nil { 27 | common.BadRequest(c, http.StatusBadRequest, fmt.Sprintf("Request has invalid employee id"), 28 | []data.ErrorDetail{ 29 | data.ErrorDetail{ 30 | ErrorType: data.ErrorTypeError, 31 | ErrorMessage: fmt.Sprintf("Request has invalid employee id"), 32 | }, 33 | }) 34 | return 35 | } 36 | result, errorResponse := handler.service.Delete(id) 37 | if errorResponse != nil { 38 | common.BadRequest(c, http.StatusBadRequest, fmt.Sprintf("Error in deleting employee by id %d", id), 39 | []data.ErrorDetail{ 40 | *errorResponse, 41 | }) 42 | return 43 | } 44 | 45 | common.Ok(c, http.StatusOK, fmt.Sprintf("successfully deleted employees with id: %d", id), result) 46 | return 47 | } -------------------------------------------------------------------------------- /employee/delete/delete.employee.router.go: -------------------------------------------------------------------------------- 1 | package delete 2 | 3 | import "github.com/gin-gonic/gin" 4 | 5 | type Router struct { 6 | deleteHandler *Handler 7 | } 8 | func InitRouter(h *Handler) *Router{ 9 | return &Router{ 10 | deleteHandler: h, 11 | } 12 | } 13 | func (r *Router) RegisterRoutes(group *gin.RouterGroup) { 14 | group.DELETE("/:id", r.deleteHandler.Delete) 15 | } 16 | -------------------------------------------------------------------------------- /employee/delete/delete.employee.service.go: -------------------------------------------------------------------------------- 1 | package delete 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/employee/data" 5 | "github.com/architagr/golang-microservice-tutorial/employee/persistance" 6 | ) 7 | 8 | type Service struct { 9 | repository persistance.IEmployeeDbContext 10 | } 11 | 12 | func InitService(repo persistance.IEmployeeDbContext) *Service { 13 | return &Service{ 14 | repository: repo, 15 | } 16 | } 17 | 18 | func (service *Service) Delete(id int) (*data.Employee, *data.ErrorDetail) { 19 | return service.repository.Delete(id) 20 | } 21 | -------------------------------------------------------------------------------- /employee/get/get.employee.handler.go: -------------------------------------------------------------------------------- 1 | package get 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | "strconv" 7 | 8 | "github.com/architagr/golang-microservice-tutorial/employee/common" 9 | "github.com/architagr/golang-microservice-tutorial/employee/data" 10 | 11 | "github.com/gin-gonic/gin" 12 | ) 13 | 14 | type Handler struct { 15 | service *Service 16 | } 17 | 18 | func InitHandler(service *Service) *Handler { 19 | return &Handler{ 20 | service: service, 21 | } 22 | } 23 | 24 | func (handler *Handler) GetAll(c *gin.Context) { 25 | 26 | result, err := handler.service.GetAll() 27 | 28 | if err != nil { 29 | common.BadRequest(c, http.StatusBadRequest, fmt.Sprintf("Error in getting all employees"), 30 | []data.ErrorDetail{ 31 | *err, 32 | }) 33 | return 34 | } 35 | 36 | common.Ok(c, http.StatusOK, fmt.Sprintf("successfully got list of employees"), result) 37 | return 38 | } 39 | 40 | func (handler *Handler) GetById(c *gin.Context) { 41 | id, err := strconv.Atoi(c.Param("id")) 42 | if err != nil { 43 | common.BadRequest(c, http.StatusBadRequest, fmt.Sprintf("Request has invalid employee id"), 44 | []data.ErrorDetail{ 45 | data.ErrorDetail{ 46 | ErrorType: data.ErrorTypeError, 47 | ErrorMessage: fmt.Sprintf("Request has invalid employee id"), 48 | }, 49 | }) 50 | return 51 | } 52 | result, errorResponse := handler.service.GetById(id) 53 | if errorResponse != nil { 54 | common.BadRequest(c, http.StatusBadRequest, fmt.Sprintf("Error in getting employee by id %d", id), 55 | []data.ErrorDetail{ 56 | *errorResponse, 57 | }) 58 | return 59 | } 60 | 61 | common.Ok(c, http.StatusOK, fmt.Sprintf("successfully got list of employees"), result) 62 | return 63 | } 64 | -------------------------------------------------------------------------------- /employee/get/get.employee.router.go: -------------------------------------------------------------------------------- 1 | package get 2 | 3 | import ( 4 | "github.com/gin-gonic/gin" 5 | ) 6 | 7 | type Router struct { 8 | getHandler *Handler 9 | } 10 | 11 | func InitRouter(h *Handler) *Router{ 12 | return &Router{ 13 | getHandler: h, 14 | } 15 | } 16 | 17 | func (r *Router) RegisterRoutes(group *gin.RouterGroup) { 18 | group.GET("", r.getHandler.GetAll) 19 | group.GET("/:id", r.getHandler.GetById) 20 | } 21 | -------------------------------------------------------------------------------- /employee/get/get.employee.service.go: -------------------------------------------------------------------------------- 1 | package get 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/employee/data" 5 | "github.com/architagr/golang-microservice-tutorial/employee/persistance" 6 | ) 7 | 8 | type Service struct { 9 | repository persistance.IEmployeeDbContext 10 | } 11 | 12 | func InitService(repo persistance.IEmployeeDbContext) *Service { 13 | return &Service{ 14 | repository: repo, 15 | } 16 | } 17 | 18 | func (service *Service) GetAll() ([]data.Employee, *data.ErrorDetail) { 19 | return service.repository.GetAll() 20 | } 21 | 22 | func (service *Service) GetById(id int) (*data.Employee, *data.ErrorDetail) { 23 | return service.repository.GetById(id) 24 | } 25 | -------------------------------------------------------------------------------- /employee/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/architagr/golang-microservice-tutorial/employee 2 | 3 | go 1.16 4 | 5 | require github.com/gin-gonic/gin v1.7.2 // indirect 6 | -------------------------------------------------------------------------------- /employee/go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 2 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 3 | github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= 4 | github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= 5 | github.com/gin-gonic/gin v1.7.2 h1:Tg03T9yM2xa8j6I3Z3oqLaQRSmKvxPd6g/2HJ6zICFA= 6 | github.com/gin-gonic/gin v1.7.2/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY= 7 | github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= 8 | github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= 9 | github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= 10 | github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= 11 | github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= 12 | github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE= 13 | github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= 14 | github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= 15 | github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= 16 | github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= 17 | github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= 18 | github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= 19 | github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= 20 | github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= 21 | github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= 22 | github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= 23 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= 24 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 25 | github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= 26 | github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= 27 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 28 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 29 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 30 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 31 | github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= 32 | github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= 33 | github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= 34 | github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= 35 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 36 | golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI= 37 | golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 38 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 39 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 40 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 41 | golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg= 42 | golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 43 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 44 | golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= 45 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 46 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 47 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 48 | gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= 49 | gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 50 | -------------------------------------------------------------------------------- /employee/persistance/contract.go: -------------------------------------------------------------------------------- 1 | package persistance 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/employee/data" 5 | ) 6 | type IEmployeeDbContext interface{ 7 | GetAll() ([]data.Employee, *data.ErrorDetail) 8 | GetById(id int) (*data.Employee, *data.ErrorDetail) 9 | Update(emp *data.Employee) (*data.Employee, *data.ErrorDetail) 10 | Add(emp *data.Employee) (*data.Employee, *data.ErrorDetail) 11 | Delete(id int) (*data.Employee, *data.ErrorDetail) 12 | } 13 | 14 | -------------------------------------------------------------------------------- /employee/persistance/employee.mongodb.go: -------------------------------------------------------------------------------- 1 | package persistance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/architagr/golang-microservice-tutorial/employee/data" 7 | ) 8 | 9 | type EmployeeMongoDb struct { 10 | connctionString string 11 | dbname string 12 | } 13 | 14 | func InitMongoDb(connctionString, dbname string) EmployeeMongoDb { 15 | data.InitEmpData() 16 | return EmployeeMongoDb{ 17 | connctionString: connctionString, 18 | dbname: dbname, 19 | } 20 | } 21 | 22 | func (dbContext EmployeeMongoDb) GetAll() ([]data.Employee, *data.ErrorDetail) { 23 | return data.Emp, nil 24 | } 25 | 26 | func (dbContext EmployeeMongoDb) GetById(id int) (*data.Employee, *data.ErrorDetail) { 27 | for _, e := range data.Emp { 28 | if e.ID == id { 29 | return &e, nil 30 | } 31 | } 32 | 33 | return nil, &data.ErrorDetail{ 34 | ErrorType: data.ErrorTypeError, 35 | ErrorMessage: fmt.Sprintf("Employee with id %d not found", id), 36 | } 37 | } 38 | 39 | func (dbContext EmployeeMongoDb) Update(emp *data.Employee) (*data.Employee, *data.ErrorDetail) { 40 | var result []data.Employee 41 | for _, e := range data.Emp { 42 | if e.ID != emp.ID { 43 | result = append(result, e) 44 | } 45 | } 46 | result = append(result, *emp) 47 | data.Emp = result 48 | return emp, nil 49 | } 50 | func (dbContext EmployeeMongoDb) Add(emp *data.Employee) (*data.Employee, *data.ErrorDetail) { 51 | emp.ID = len(data.Emp) + 1 52 | result := data.Emp 53 | 54 | result = append(result, *emp) 55 | data.Emp = result 56 | 57 | return emp, nil 58 | } 59 | func (dbContext EmployeeMongoDb) Delete(id int) (*data.Employee, *data.ErrorDetail) { 60 | var result []data.Employee 61 | found := false 62 | var returnData data.Employee 63 | for _, e := range data.Emp { 64 | if e.ID == id { 65 | found = true 66 | returnData = e 67 | } else { 68 | result = append(result, e) 69 | } 70 | } 71 | if found { 72 | data.Emp = result 73 | return &returnData, nil 74 | } else { 75 | return nil, &data.ErrorDetail{ 76 | ErrorType:data.ErrorTypeError, 77 | ErrorMessage: fmt.Sprintf("Employee with id %d not found", id), 78 | } 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /employee/update/update.employee.handler.go: -------------------------------------------------------------------------------- 1 | package update 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/employee/common" 5 | "github.com/architagr/golang-microservice-tutorial/employee/data" 6 | 7 | "fmt" 8 | "net/http" 9 | "strconv" 10 | 11 | "github.com/gin-gonic/gin" 12 | ) 13 | 14 | type Handler struct { 15 | service *Service 16 | } 17 | 18 | func InitHandler(service *Service) *Handler { 19 | return &Handler{ 20 | service: service, 21 | } 22 | } 23 | 24 | func (h *Handler) Update(c *gin.Context) { 25 | id, err := strconv.Atoi(c.Param("id")) 26 | if err != nil { 27 | common.BadRequest(c, http.StatusBadRequest, fmt.Sprintf("Request has invalid employee id"), 28 | []data.ErrorDetail{ 29 | data.ErrorDetail{ 30 | ErrorType: data.ErrorTypeError, 31 | ErrorMessage: fmt.Sprintf("Request has invalid employee id"), 32 | }, 33 | }) 34 | return 35 | } 36 | var updateObj data.Employee 37 | if err := c.ShouldBindJSON(&updateObj); err != nil { 38 | common.BadRequest(c, http.StatusBadRequest, fmt.Sprintf("Request has invalid body"), 39 | []data.ErrorDetail{ 40 | data.ErrorDetail{ 41 | ErrorType: data.ErrorTypeError, 42 | ErrorMessage: fmt.Sprintf("Request has invalid body"), 43 | }, 44 | data.ErrorDetail{ 45 | ErrorType: data.ErrorTypeValidation, 46 | ErrorMessage: err.Error(), 47 | }, 48 | }) 49 | return 50 | } 51 | updateObj.ID = id 52 | 53 | result, errorResponse := h.service.Update(&updateObj) 54 | if errorResponse != nil { 55 | common.BadRequest(c, http.StatusBadRequest, fmt.Sprintf("Error in updating employee by id %d", id), 56 | []data.ErrorDetail{ 57 | *errorResponse, 58 | }) 59 | return 60 | } 61 | 62 | common.Ok(c, http.StatusOK, fmt.Sprintf("successfully updated employees with id: %d", id), result) 63 | return 64 | } 65 | -------------------------------------------------------------------------------- /employee/update/update.employee.router.go: -------------------------------------------------------------------------------- 1 | package update 2 | 3 | import "github.com/gin-gonic/gin" 4 | 5 | type Router struct { 6 | updateHandler *Handler 7 | } 8 | func InitRouter(h *Handler) *Router{ 9 | return &Router{ 10 | updateHandler: h, 11 | } 12 | } 13 | func (r *Router) RegisterRoutes(group *gin.RouterGroup) { 14 | group.PUT("/:id", r.updateHandler.Update) 15 | } 16 | -------------------------------------------------------------------------------- /employee/update/update.employee.service.go: -------------------------------------------------------------------------------- 1 | package update 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/employee/data" 5 | "github.com/architagr/golang-microservice-tutorial/employee/persistance" 6 | ) 7 | 8 | type Service struct { 9 | repository persistance.IEmployeeDbContext 10 | } 11 | 12 | func InitService(repo persistance.IEmployeeDbContext) *Service { 13 | return &Service{ 14 | repository: repo, 15 | } 16 | } 17 | 18 | func (service *Service) Update(emp *data.Employee) (*data.Employee, *data.ErrorDetail) { 19 | return service.repository.Update(emp) 20 | } 21 | -------------------------------------------------------------------------------- /rpc/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/architagr/golang-microservice-tutorial/rpc 2 | 3 | go 1.16 4 | -------------------------------------------------------------------------------- /rpc/rpc_auth/login.pb.go: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-go. DO NOT EDIT. 2 | // versions: 3 | // protoc-gen-go v1.26.0 4 | // protoc v3.6.1 5 | // source: login.proto 6 | 7 | package rpc_auth 8 | 9 | import ( 10 | protoreflect "google.golang.org/protobuf/reflect/protoreflect" 11 | protoimpl "google.golang.org/protobuf/runtime/protoimpl" 12 | reflect "reflect" 13 | sync "sync" 14 | ) 15 | 16 | const ( 17 | // Verify that this generated code is sufficiently up-to-date. 18 | _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) 19 | // Verify that runtime/protoimpl is sufficiently up-to-date. 20 | _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) 21 | ) 22 | 23 | type LoginRequest struct { 24 | state protoimpl.MessageState 25 | sizeCache protoimpl.SizeCache 26 | unknownFields protoimpl.UnknownFields 27 | 28 | Username string `protobuf:"bytes,1,opt,name=Username,proto3" json:"Username,omitempty"` 29 | Password string `protobuf:"bytes,2,opt,name=Password,proto3" json:"Password,omitempty"` 30 | RememberMe bool `protobuf:"varint,3,opt,name=RememberMe,proto3" json:"RememberMe,omitempty"` 31 | } 32 | 33 | func (x *LoginRequest) Reset() { 34 | *x = LoginRequest{} 35 | if protoimpl.UnsafeEnabled { 36 | mi := &file_login_proto_msgTypes[0] 37 | ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) 38 | ms.StoreMessageInfo(mi) 39 | } 40 | } 41 | 42 | func (x *LoginRequest) String() string { 43 | return protoimpl.X.MessageStringOf(x) 44 | } 45 | 46 | func (*LoginRequest) ProtoMessage() {} 47 | 48 | func (x *LoginRequest) ProtoReflect() protoreflect.Message { 49 | mi := &file_login_proto_msgTypes[0] 50 | if protoimpl.UnsafeEnabled && x != nil { 51 | ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) 52 | if ms.LoadMessageInfo() == nil { 53 | ms.StoreMessageInfo(mi) 54 | } 55 | return ms 56 | } 57 | return mi.MessageOf(x) 58 | } 59 | 60 | // Deprecated: Use LoginRequest.ProtoReflect.Descriptor instead. 61 | func (*LoginRequest) Descriptor() ([]byte, []int) { 62 | return file_login_proto_rawDescGZIP(), []int{0} 63 | } 64 | 65 | func (x *LoginRequest) GetUsername() string { 66 | if x != nil { 67 | return x.Username 68 | } 69 | return "" 70 | } 71 | 72 | func (x *LoginRequest) GetPassword() string { 73 | if x != nil { 74 | return x.Password 75 | } 76 | return "" 77 | } 78 | 79 | func (x *LoginRequest) GetRememberMe() bool { 80 | if x != nil { 81 | return x.RememberMe 82 | } 83 | return false 84 | } 85 | 86 | type LoginResponse struct { 87 | state protoimpl.MessageState 88 | sizeCache protoimpl.SizeCache 89 | unknownFields protoimpl.UnknownFields 90 | 91 | LoginDetails *LoginRequest `protobuf:"bytes,1,opt,name=loginDetails,proto3" json:"loginDetails,omitempty"` 92 | Token string `protobuf:"bytes,2,opt,name=token,proto3" json:"token,omitempty"` 93 | } 94 | 95 | func (x *LoginResponse) Reset() { 96 | *x = LoginResponse{} 97 | if protoimpl.UnsafeEnabled { 98 | mi := &file_login_proto_msgTypes[1] 99 | ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) 100 | ms.StoreMessageInfo(mi) 101 | } 102 | } 103 | 104 | func (x *LoginResponse) String() string { 105 | return protoimpl.X.MessageStringOf(x) 106 | } 107 | 108 | func (*LoginResponse) ProtoMessage() {} 109 | 110 | func (x *LoginResponse) ProtoReflect() protoreflect.Message { 111 | mi := &file_login_proto_msgTypes[1] 112 | if protoimpl.UnsafeEnabled && x != nil { 113 | ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) 114 | if ms.LoadMessageInfo() == nil { 115 | ms.StoreMessageInfo(mi) 116 | } 117 | return ms 118 | } 119 | return mi.MessageOf(x) 120 | } 121 | 122 | // Deprecated: Use LoginResponse.ProtoReflect.Descriptor instead. 123 | func (*LoginResponse) Descriptor() ([]byte, []int) { 124 | return file_login_proto_rawDescGZIP(), []int{1} 125 | } 126 | 127 | func (x *LoginResponse) GetLoginDetails() *LoginRequest { 128 | if x != nil { 129 | return x.LoginDetails 130 | } 131 | return nil 132 | } 133 | 134 | func (x *LoginResponse) GetToken() string { 135 | if x != nil { 136 | return x.Token 137 | } 138 | return "" 139 | } 140 | 141 | type LoginRequestList struct { 142 | state protoimpl.MessageState 143 | sizeCache protoimpl.SizeCache 144 | unknownFields protoimpl.UnknownFields 145 | 146 | Data []*LoginRequest `protobuf:"bytes,1,rep,name=data,proto3" json:"data,omitempty"` 147 | } 148 | 149 | func (x *LoginRequestList) Reset() { 150 | *x = LoginRequestList{} 151 | if protoimpl.UnsafeEnabled { 152 | mi := &file_login_proto_msgTypes[2] 153 | ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) 154 | ms.StoreMessageInfo(mi) 155 | } 156 | } 157 | 158 | func (x *LoginRequestList) String() string { 159 | return protoimpl.X.MessageStringOf(x) 160 | } 161 | 162 | func (*LoginRequestList) ProtoMessage() {} 163 | 164 | func (x *LoginRequestList) ProtoReflect() protoreflect.Message { 165 | mi := &file_login_proto_msgTypes[2] 166 | if protoimpl.UnsafeEnabled && x != nil { 167 | ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) 168 | if ms.LoadMessageInfo() == nil { 169 | ms.StoreMessageInfo(mi) 170 | } 171 | return ms 172 | } 173 | return mi.MessageOf(x) 174 | } 175 | 176 | // Deprecated: Use LoginRequestList.ProtoReflect.Descriptor instead. 177 | func (*LoginRequestList) Descriptor() ([]byte, []int) { 178 | return file_login_proto_rawDescGZIP(), []int{2} 179 | } 180 | 181 | func (x *LoginRequestList) GetData() []*LoginRequest { 182 | if x != nil { 183 | return x.Data 184 | } 185 | return nil 186 | } 187 | 188 | type LoginResponseList struct { 189 | state protoimpl.MessageState 190 | sizeCache protoimpl.SizeCache 191 | unknownFields protoimpl.UnknownFields 192 | 193 | Data []*LoginResponse `protobuf:"bytes,1,rep,name=data,proto3" json:"data,omitempty"` 194 | } 195 | 196 | func (x *LoginResponseList) Reset() { 197 | *x = LoginResponseList{} 198 | if protoimpl.UnsafeEnabled { 199 | mi := &file_login_proto_msgTypes[3] 200 | ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) 201 | ms.StoreMessageInfo(mi) 202 | } 203 | } 204 | 205 | func (x *LoginResponseList) String() string { 206 | return protoimpl.X.MessageStringOf(x) 207 | } 208 | 209 | func (*LoginResponseList) ProtoMessage() {} 210 | 211 | func (x *LoginResponseList) ProtoReflect() protoreflect.Message { 212 | mi := &file_login_proto_msgTypes[3] 213 | if protoimpl.UnsafeEnabled && x != nil { 214 | ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) 215 | if ms.LoadMessageInfo() == nil { 216 | ms.StoreMessageInfo(mi) 217 | } 218 | return ms 219 | } 220 | return mi.MessageOf(x) 221 | } 222 | 223 | // Deprecated: Use LoginResponseList.ProtoReflect.Descriptor instead. 224 | func (*LoginResponseList) Descriptor() ([]byte, []int) { 225 | return file_login_proto_rawDescGZIP(), []int{3} 226 | } 227 | 228 | func (x *LoginResponseList) GetData() []*LoginResponse { 229 | if x != nil { 230 | return x.Data 231 | } 232 | return nil 233 | } 234 | 235 | var File_login_proto protoreflect.FileDescriptor 236 | 237 | var file_login_proto_rawDesc = []byte{ 238 | 0x0a, 0x0b, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x08, 0x72, 239 | 0x70, 0x63, 0x5f, 0x61, 0x75, 0x74, 0x68, 0x22, 0x66, 0x0a, 0x0c, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 240 | 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x55, 0x73, 0x65, 0x72, 0x6e, 241 | 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x55, 0x73, 0x65, 0x72, 0x6e, 242 | 0x61, 0x6d, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x50, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x18, 243 | 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x50, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x12, 244 | 0x1e, 0x0a, 0x0a, 0x52, 0x65, 0x6d, 0x65, 0x6d, 0x62, 0x65, 0x72, 0x4d, 0x65, 0x18, 0x03, 0x20, 245 | 0x01, 0x28, 0x08, 0x52, 0x0a, 0x52, 0x65, 0x6d, 0x65, 0x6d, 0x62, 0x65, 0x72, 0x4d, 0x65, 0x22, 246 | 0x61, 0x0a, 0x0d, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 247 | 0x12, 0x3a, 0x0a, 0x0c, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x73, 248 | 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x72, 0x70, 0x63, 0x5f, 0x61, 0x75, 0x74, 249 | 0x68, 0x2e, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x52, 0x0c, 250 | 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x73, 0x12, 0x14, 0x0a, 0x05, 251 | 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x6b, 252 | 0x65, 0x6e, 0x22, 0x3e, 0x0a, 0x10, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 253 | 0x73, 0x74, 0x4c, 0x69, 0x73, 0x74, 0x12, 0x2a, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x01, 254 | 0x20, 0x03, 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x72, 0x70, 0x63, 0x5f, 0x61, 0x75, 0x74, 0x68, 0x2e, 255 | 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x52, 0x04, 0x64, 0x61, 256 | 0x74, 0x61, 0x22, 0x40, 0x0a, 0x11, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 257 | 0x6e, 0x73, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x12, 0x2b, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 258 | 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x17, 0x2e, 0x72, 0x70, 0x63, 0x5f, 0x61, 0x75, 0x74, 0x68, 259 | 0x2e, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x52, 0x04, 260 | 0x64, 0x61, 0x74, 0x61, 0x32, 0xc5, 0x02, 0x0a, 0x0c, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x53, 0x65, 261 | 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x43, 0x0a, 0x0e, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x53, 0x69, 262 | 0x6d, 0x70, 0x6c, 0x65, 0x52, 0x50, 0x43, 0x12, 0x16, 0x2e, 0x72, 0x70, 0x63, 0x5f, 0x61, 0x75, 263 | 0x74, 0x68, 0x2e, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 264 | 0x17, 0x2e, 0x72, 0x70, 0x63, 0x5f, 0x61, 0x75, 0x74, 0x68, 0x2e, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 265 | 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x4f, 0x0a, 0x14, 0x4c, 0x6f, 266 | 0x67, 0x69, 0x6e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x52, 267 | 0x50, 0x43, 0x12, 0x1a, 0x2e, 0x72, 0x70, 0x63, 0x5f, 0x61, 0x75, 0x74, 0x68, 0x2e, 0x4c, 0x6f, 268 | 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x4c, 0x69, 0x73, 0x74, 0x1a, 0x17, 269 | 0x2e, 0x72, 0x70, 0x63, 0x5f, 0x61, 0x75, 0x74, 0x68, 0x2e, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 270 | 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x30, 0x01, 0x12, 0x4f, 0x0a, 0x14, 0x4c, 271 | 0x6f, 0x67, 0x69, 0x6e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 272 | 0x52, 0x50, 0x43, 0x12, 0x16, 0x2e, 0x72, 0x70, 0x63, 0x5f, 0x61, 0x75, 0x74, 0x68, 0x2e, 0x4c, 273 | 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1b, 0x2e, 0x72, 0x70, 274 | 0x63, 0x5f, 0x61, 0x75, 0x74, 0x68, 0x2e, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x73, 0x70, 275 | 0x6f, 0x6e, 0x73, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x22, 0x00, 0x28, 0x01, 0x12, 0x4e, 0x0a, 0x15, 276 | 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x42, 0x69, 0x44, 0x69, 0x72, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 277 | 0x61, 0x6c, 0x52, 0x50, 0x43, 0x12, 0x16, 0x2e, 0x72, 0x70, 0x63, 0x5f, 0x61, 0x75, 0x74, 0x68, 278 | 0x2e, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 279 | 0x72, 0x70, 0x63, 0x5f, 0x61, 0x75, 0x74, 0x68, 0x2e, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 280 | 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x28, 0x01, 0x30, 0x01, 0x42, 0x0c, 0x5a, 0x0a, 281 | 0x2e, 0x2f, 0x72, 0x70, 0x63, 0x5f, 0x61, 0x75, 0x74, 0x68, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 282 | 0x6f, 0x33, 283 | } 284 | 285 | var ( 286 | file_login_proto_rawDescOnce sync.Once 287 | file_login_proto_rawDescData = file_login_proto_rawDesc 288 | ) 289 | 290 | func file_login_proto_rawDescGZIP() []byte { 291 | file_login_proto_rawDescOnce.Do(func() { 292 | file_login_proto_rawDescData = protoimpl.X.CompressGZIP(file_login_proto_rawDescData) 293 | }) 294 | return file_login_proto_rawDescData 295 | } 296 | 297 | var file_login_proto_msgTypes = make([]protoimpl.MessageInfo, 4) 298 | var file_login_proto_goTypes = []interface{}{ 299 | (*LoginRequest)(nil), // 0: rpc_auth.LoginRequest 300 | (*LoginResponse)(nil), // 1: rpc_auth.LoginResponse 301 | (*LoginRequestList)(nil), // 2: rpc_auth.LoginRequestList 302 | (*LoginResponseList)(nil), // 3: rpc_auth.LoginResponseList 303 | } 304 | var file_login_proto_depIdxs = []int32{ 305 | 0, // 0: rpc_auth.LoginResponse.loginDetails:type_name -> rpc_auth.LoginRequest 306 | 0, // 1: rpc_auth.LoginRequestList.data:type_name -> rpc_auth.LoginRequest 307 | 1, // 2: rpc_auth.LoginResponseList.data:type_name -> rpc_auth.LoginResponse 308 | 0, // 3: rpc_auth.LoginService.LoginSimpleRPC:input_type -> rpc_auth.LoginRequest 309 | 2, // 4: rpc_auth.LoginService.LoginServerStreamRPC:input_type -> rpc_auth.LoginRequestList 310 | 0, // 5: rpc_auth.LoginService.LoginClientStreamRPC:input_type -> rpc_auth.LoginRequest 311 | 0, // 6: rpc_auth.LoginService.LoginBiDirectionalRPC:input_type -> rpc_auth.LoginRequest 312 | 1, // 7: rpc_auth.LoginService.LoginSimpleRPC:output_type -> rpc_auth.LoginResponse 313 | 1, // 8: rpc_auth.LoginService.LoginServerStreamRPC:output_type -> rpc_auth.LoginResponse 314 | 3, // 9: rpc_auth.LoginService.LoginClientStreamRPC:output_type -> rpc_auth.LoginResponseList 315 | 1, // 10: rpc_auth.LoginService.LoginBiDirectionalRPC:output_type -> rpc_auth.LoginResponse 316 | 7, // [7:11] is the sub-list for method output_type 317 | 3, // [3:7] is the sub-list for method input_type 318 | 3, // [3:3] is the sub-list for extension type_name 319 | 3, // [3:3] is the sub-list for extension extendee 320 | 0, // [0:3] is the sub-list for field type_name 321 | } 322 | 323 | func init() { file_login_proto_init() } 324 | func file_login_proto_init() { 325 | if File_login_proto != nil { 326 | return 327 | } 328 | if !protoimpl.UnsafeEnabled { 329 | file_login_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { 330 | switch v := v.(*LoginRequest); i { 331 | case 0: 332 | return &v.state 333 | case 1: 334 | return &v.sizeCache 335 | case 2: 336 | return &v.unknownFields 337 | default: 338 | return nil 339 | } 340 | } 341 | file_login_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { 342 | switch v := v.(*LoginResponse); i { 343 | case 0: 344 | return &v.state 345 | case 1: 346 | return &v.sizeCache 347 | case 2: 348 | return &v.unknownFields 349 | default: 350 | return nil 351 | } 352 | } 353 | file_login_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { 354 | switch v := v.(*LoginRequestList); i { 355 | case 0: 356 | return &v.state 357 | case 1: 358 | return &v.sizeCache 359 | case 2: 360 | return &v.unknownFields 361 | default: 362 | return nil 363 | } 364 | } 365 | file_login_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { 366 | switch v := v.(*LoginResponseList); i { 367 | case 0: 368 | return &v.state 369 | case 1: 370 | return &v.sizeCache 371 | case 2: 372 | return &v.unknownFields 373 | default: 374 | return nil 375 | } 376 | } 377 | } 378 | type x struct{} 379 | out := protoimpl.TypeBuilder{ 380 | File: protoimpl.DescBuilder{ 381 | GoPackagePath: reflect.TypeOf(x{}).PkgPath(), 382 | RawDescriptor: file_login_proto_rawDesc, 383 | NumEnums: 0, 384 | NumMessages: 4, 385 | NumExtensions: 0, 386 | NumServices: 1, 387 | }, 388 | GoTypes: file_login_proto_goTypes, 389 | DependencyIndexes: file_login_proto_depIdxs, 390 | MessageInfos: file_login_proto_msgTypes, 391 | }.Build() 392 | File_login_proto = out.File 393 | file_login_proto_rawDesc = nil 394 | file_login_proto_goTypes = nil 395 | file_login_proto_depIdxs = nil 396 | } 397 | -------------------------------------------------------------------------------- /rpc/rpc_auth/login_grpc.pb.go: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-go-grpc. DO NOT EDIT. 2 | 3 | package rpc_auth 4 | 5 | import ( 6 | context "context" 7 | grpc "google.golang.org/grpc" 8 | codes "google.golang.org/grpc/codes" 9 | status "google.golang.org/grpc/status" 10 | ) 11 | 12 | // This is a compile-time assertion to ensure that this generated file 13 | // is compatible with the grpc package it is being compiled against. 14 | // Requires gRPC-Go v1.32.0 or later. 15 | const _ = grpc.SupportPackageIsVersion7 16 | 17 | // LoginServiceClient is the client API for LoginService service. 18 | // 19 | // 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. 20 | type LoginServiceClient interface { 21 | LoginSimpleRPC(ctx context.Context, in *LoginRequest, opts ...grpc.CallOption) (*LoginResponse, error) 22 | LoginServerStreamRPC(ctx context.Context, in *LoginRequestList, opts ...grpc.CallOption) (LoginService_LoginServerStreamRPCClient, error) 23 | LoginClientStreamRPC(ctx context.Context, opts ...grpc.CallOption) (LoginService_LoginClientStreamRPCClient, error) 24 | LoginBiDirectionalRPC(ctx context.Context, opts ...grpc.CallOption) (LoginService_LoginBiDirectionalRPCClient, error) 25 | } 26 | 27 | type loginServiceClient struct { 28 | cc grpc.ClientConnInterface 29 | } 30 | 31 | func NewLoginServiceClient(cc grpc.ClientConnInterface) LoginServiceClient { 32 | return &loginServiceClient{cc} 33 | } 34 | 35 | func (c *loginServiceClient) LoginSimpleRPC(ctx context.Context, in *LoginRequest, opts ...grpc.CallOption) (*LoginResponse, error) { 36 | out := new(LoginResponse) 37 | err := c.cc.Invoke(ctx, "/rpc_auth.LoginService/LoginSimpleRPC", in, out, opts...) 38 | if err != nil { 39 | return nil, err 40 | } 41 | return out, nil 42 | } 43 | 44 | func (c *loginServiceClient) LoginServerStreamRPC(ctx context.Context, in *LoginRequestList, opts ...grpc.CallOption) (LoginService_LoginServerStreamRPCClient, error) { 45 | stream, err := c.cc.NewStream(ctx, &LoginService_ServiceDesc.Streams[0], "/rpc_auth.LoginService/LoginServerStreamRPC", opts...) 46 | if err != nil { 47 | return nil, err 48 | } 49 | x := &loginServiceLoginServerStreamRPCClient{stream} 50 | if err := x.ClientStream.SendMsg(in); err != nil { 51 | return nil, err 52 | } 53 | if err := x.ClientStream.CloseSend(); err != nil { 54 | return nil, err 55 | } 56 | return x, nil 57 | } 58 | 59 | type LoginService_LoginServerStreamRPCClient interface { 60 | Recv() (*LoginResponse, error) 61 | grpc.ClientStream 62 | } 63 | 64 | type loginServiceLoginServerStreamRPCClient struct { 65 | grpc.ClientStream 66 | } 67 | 68 | func (x *loginServiceLoginServerStreamRPCClient) Recv() (*LoginResponse, error) { 69 | m := new(LoginResponse) 70 | if err := x.ClientStream.RecvMsg(m); err != nil { 71 | return nil, err 72 | } 73 | return m, nil 74 | } 75 | 76 | func (c *loginServiceClient) LoginClientStreamRPC(ctx context.Context, opts ...grpc.CallOption) (LoginService_LoginClientStreamRPCClient, error) { 77 | stream, err := c.cc.NewStream(ctx, &LoginService_ServiceDesc.Streams[1], "/rpc_auth.LoginService/LoginClientStreamRPC", opts...) 78 | if err != nil { 79 | return nil, err 80 | } 81 | x := &loginServiceLoginClientStreamRPCClient{stream} 82 | return x, nil 83 | } 84 | 85 | type LoginService_LoginClientStreamRPCClient interface { 86 | Send(*LoginRequest) error 87 | CloseAndRecv() (*LoginResponseList, error) 88 | grpc.ClientStream 89 | } 90 | 91 | type loginServiceLoginClientStreamRPCClient struct { 92 | grpc.ClientStream 93 | } 94 | 95 | func (x *loginServiceLoginClientStreamRPCClient) Send(m *LoginRequest) error { 96 | return x.ClientStream.SendMsg(m) 97 | } 98 | 99 | func (x *loginServiceLoginClientStreamRPCClient) CloseAndRecv() (*LoginResponseList, error) { 100 | if err := x.ClientStream.CloseSend(); err != nil { 101 | return nil, err 102 | } 103 | m := new(LoginResponseList) 104 | if err := x.ClientStream.RecvMsg(m); err != nil { 105 | return nil, err 106 | } 107 | return m, nil 108 | } 109 | 110 | func (c *loginServiceClient) LoginBiDirectionalRPC(ctx context.Context, opts ...grpc.CallOption) (LoginService_LoginBiDirectionalRPCClient, error) { 111 | stream, err := c.cc.NewStream(ctx, &LoginService_ServiceDesc.Streams[2], "/rpc_auth.LoginService/LoginBiDirectionalRPC", opts...) 112 | if err != nil { 113 | return nil, err 114 | } 115 | x := &loginServiceLoginBiDirectionalRPCClient{stream} 116 | return x, nil 117 | } 118 | 119 | type LoginService_LoginBiDirectionalRPCClient interface { 120 | Send(*LoginRequest) error 121 | Recv() (*LoginResponse, error) 122 | grpc.ClientStream 123 | } 124 | 125 | type loginServiceLoginBiDirectionalRPCClient struct { 126 | grpc.ClientStream 127 | } 128 | 129 | func (x *loginServiceLoginBiDirectionalRPCClient) Send(m *LoginRequest) error { 130 | return x.ClientStream.SendMsg(m) 131 | } 132 | 133 | func (x *loginServiceLoginBiDirectionalRPCClient) Recv() (*LoginResponse, error) { 134 | m := new(LoginResponse) 135 | if err := x.ClientStream.RecvMsg(m); err != nil { 136 | return nil, err 137 | } 138 | return m, nil 139 | } 140 | 141 | // LoginServiceServer is the server API for LoginService service. 142 | // All implementations must embed UnimplementedLoginServiceServer 143 | // for forward compatibility 144 | type LoginServiceServer interface { 145 | LoginSimpleRPC(context.Context, *LoginRequest) (*LoginResponse, error) 146 | LoginServerStreamRPC(*LoginRequestList, LoginService_LoginServerStreamRPCServer) error 147 | LoginClientStreamRPC(LoginService_LoginClientStreamRPCServer) error 148 | LoginBiDirectionalRPC(LoginService_LoginBiDirectionalRPCServer) error 149 | mustEmbedUnimplementedLoginServiceServer() 150 | } 151 | 152 | // UnimplementedLoginServiceServer must be embedded to have forward compatible implementations. 153 | type UnimplementedLoginServiceServer struct { 154 | } 155 | 156 | func (UnimplementedLoginServiceServer) LoginSimpleRPC(context.Context, *LoginRequest) (*LoginResponse, error) { 157 | return nil, status.Errorf(codes.Unimplemented, "method LoginSimpleRPC not implemented") 158 | } 159 | func (UnimplementedLoginServiceServer) LoginServerStreamRPC(*LoginRequestList, LoginService_LoginServerStreamRPCServer) error { 160 | return status.Errorf(codes.Unimplemented, "method LoginServerStreamRPC not implemented") 161 | } 162 | func (UnimplementedLoginServiceServer) LoginClientStreamRPC(LoginService_LoginClientStreamRPCServer) error { 163 | return status.Errorf(codes.Unimplemented, "method LoginClientStreamRPC not implemented") 164 | } 165 | func (UnimplementedLoginServiceServer) LoginBiDirectionalRPC(LoginService_LoginBiDirectionalRPCServer) error { 166 | return status.Errorf(codes.Unimplemented, "method LoginBiDirectionalRPC not implemented") 167 | } 168 | func (UnimplementedLoginServiceServer) mustEmbedUnimplementedLoginServiceServer() {} 169 | 170 | // UnsafeLoginServiceServer may be embedded to opt out of forward compatibility for this service. 171 | // Use of this interface is not recommended, as added methods to LoginServiceServer will 172 | // result in compilation errors. 173 | type UnsafeLoginServiceServer interface { 174 | mustEmbedUnimplementedLoginServiceServer() 175 | } 176 | 177 | func RegisterLoginServiceServer(s grpc.ServiceRegistrar, srv LoginServiceServer) { 178 | s.RegisterService(&LoginService_ServiceDesc, srv) 179 | } 180 | 181 | func _LoginService_LoginSimpleRPC_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { 182 | in := new(LoginRequest) 183 | if err := dec(in); err != nil { 184 | return nil, err 185 | } 186 | if interceptor == nil { 187 | return srv.(LoginServiceServer).LoginSimpleRPC(ctx, in) 188 | } 189 | info := &grpc.UnaryServerInfo{ 190 | Server: srv, 191 | FullMethod: "/rpc_auth.LoginService/LoginSimpleRPC", 192 | } 193 | handler := func(ctx context.Context, req interface{}) (interface{}, error) { 194 | return srv.(LoginServiceServer).LoginSimpleRPC(ctx, req.(*LoginRequest)) 195 | } 196 | return interceptor(ctx, in, info, handler) 197 | } 198 | 199 | func _LoginService_LoginServerStreamRPC_Handler(srv interface{}, stream grpc.ServerStream) error { 200 | m := new(LoginRequestList) 201 | if err := stream.RecvMsg(m); err != nil { 202 | return err 203 | } 204 | return srv.(LoginServiceServer).LoginServerStreamRPC(m, &loginServiceLoginServerStreamRPCServer{stream}) 205 | } 206 | 207 | type LoginService_LoginServerStreamRPCServer interface { 208 | Send(*LoginResponse) error 209 | grpc.ServerStream 210 | } 211 | 212 | type loginServiceLoginServerStreamRPCServer struct { 213 | grpc.ServerStream 214 | } 215 | 216 | func (x *loginServiceLoginServerStreamRPCServer) Send(m *LoginResponse) error { 217 | return x.ServerStream.SendMsg(m) 218 | } 219 | 220 | func _LoginService_LoginClientStreamRPC_Handler(srv interface{}, stream grpc.ServerStream) error { 221 | return srv.(LoginServiceServer).LoginClientStreamRPC(&loginServiceLoginClientStreamRPCServer{stream}) 222 | } 223 | 224 | type LoginService_LoginClientStreamRPCServer interface { 225 | SendAndClose(*LoginResponseList) error 226 | Recv() (*LoginRequest, error) 227 | grpc.ServerStream 228 | } 229 | 230 | type loginServiceLoginClientStreamRPCServer struct { 231 | grpc.ServerStream 232 | } 233 | 234 | func (x *loginServiceLoginClientStreamRPCServer) SendAndClose(m *LoginResponseList) error { 235 | return x.ServerStream.SendMsg(m) 236 | } 237 | 238 | func (x *loginServiceLoginClientStreamRPCServer) Recv() (*LoginRequest, error) { 239 | m := new(LoginRequest) 240 | if err := x.ServerStream.RecvMsg(m); err != nil { 241 | return nil, err 242 | } 243 | return m, nil 244 | } 245 | 246 | func _LoginService_LoginBiDirectionalRPC_Handler(srv interface{}, stream grpc.ServerStream) error { 247 | return srv.(LoginServiceServer).LoginBiDirectionalRPC(&loginServiceLoginBiDirectionalRPCServer{stream}) 248 | } 249 | 250 | type LoginService_LoginBiDirectionalRPCServer interface { 251 | Send(*LoginResponse) error 252 | Recv() (*LoginRequest, error) 253 | grpc.ServerStream 254 | } 255 | 256 | type loginServiceLoginBiDirectionalRPCServer struct { 257 | grpc.ServerStream 258 | } 259 | 260 | func (x *loginServiceLoginBiDirectionalRPCServer) Send(m *LoginResponse) error { 261 | return x.ServerStream.SendMsg(m) 262 | } 263 | 264 | func (x *loginServiceLoginBiDirectionalRPCServer) Recv() (*LoginRequest, error) { 265 | m := new(LoginRequest) 266 | if err := x.ServerStream.RecvMsg(m); err != nil { 267 | return nil, err 268 | } 269 | return m, nil 270 | } 271 | 272 | // LoginService_ServiceDesc is the grpc.ServiceDesc for LoginService service. 273 | // It's only intended for direct use with grpc.RegisterService, 274 | // and not to be introspected or modified (even as a copy) 275 | var LoginService_ServiceDesc = grpc.ServiceDesc{ 276 | ServiceName: "rpc_auth.LoginService", 277 | HandlerType: (*LoginServiceServer)(nil), 278 | Methods: []grpc.MethodDesc{ 279 | { 280 | MethodName: "LoginSimpleRPC", 281 | Handler: _LoginService_LoginSimpleRPC_Handler, 282 | }, 283 | }, 284 | Streams: []grpc.StreamDesc{ 285 | { 286 | StreamName: "LoginServerStreamRPC", 287 | Handler: _LoginService_LoginServerStreamRPC_Handler, 288 | ServerStreams: true, 289 | }, 290 | { 291 | StreamName: "LoginClientStreamRPC", 292 | Handler: _LoginService_LoginClientStreamRPC_Handler, 293 | ClientStreams: true, 294 | }, 295 | { 296 | StreamName: "LoginBiDirectionalRPC", 297 | Handler: _LoginService_LoginBiDirectionalRPC_Handler, 298 | ServerStreams: true, 299 | ClientStreams: true, 300 | }, 301 | }, 302 | Metadata: "login.proto", 303 | } 304 | -------------------------------------------------------------------------------- /rpc/rpc_auth/tokenValidate.pb.go: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-go. DO NOT EDIT. 2 | // versions: 3 | // protoc-gen-go v1.26.0 4 | // protoc v3.6.1 5 | // source: tokenValidate.proto 6 | 7 | package rpc_auth 8 | 9 | import ( 10 | protoreflect "google.golang.org/protobuf/reflect/protoreflect" 11 | protoimpl "google.golang.org/protobuf/runtime/protoimpl" 12 | reflect "reflect" 13 | sync "sync" 14 | ) 15 | 16 | const ( 17 | // Verify that this generated code is sufficiently up-to-date. 18 | _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) 19 | // Verify that runtime/protoimpl is sufficiently up-to-date. 20 | _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) 21 | ) 22 | 23 | type ValidateTokenRequest struct { 24 | state protoimpl.MessageState 25 | sizeCache protoimpl.SizeCache 26 | unknownFields protoimpl.UnknownFields 27 | 28 | Token string `protobuf:"bytes,1,opt,name=Token,proto3" json:"Token,omitempty"` 29 | } 30 | 31 | func (x *ValidateTokenRequest) Reset() { 32 | *x = ValidateTokenRequest{} 33 | if protoimpl.UnsafeEnabled { 34 | mi := &file_tokenValidate_proto_msgTypes[0] 35 | ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) 36 | ms.StoreMessageInfo(mi) 37 | } 38 | } 39 | 40 | func (x *ValidateTokenRequest) String() string { 41 | return protoimpl.X.MessageStringOf(x) 42 | } 43 | 44 | func (*ValidateTokenRequest) ProtoMessage() {} 45 | 46 | func (x *ValidateTokenRequest) ProtoReflect() protoreflect.Message { 47 | mi := &file_tokenValidate_proto_msgTypes[0] 48 | if protoimpl.UnsafeEnabled && x != nil { 49 | ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) 50 | if ms.LoadMessageInfo() == nil { 51 | ms.StoreMessageInfo(mi) 52 | } 53 | return ms 54 | } 55 | return mi.MessageOf(x) 56 | } 57 | 58 | // Deprecated: Use ValidateTokenRequest.ProtoReflect.Descriptor instead. 59 | func (*ValidateTokenRequest) Descriptor() ([]byte, []int) { 60 | return file_tokenValidate_proto_rawDescGZIP(), []int{0} 61 | } 62 | 63 | func (x *ValidateTokenRequest) GetToken() string { 64 | if x != nil { 65 | return x.Token 66 | } 67 | return "" 68 | } 69 | 70 | type ValidateTokenResponse struct { 71 | state protoimpl.MessageState 72 | sizeCache protoimpl.SizeCache 73 | unknownFields protoimpl.UnknownFields 74 | 75 | IsValid bool `protobuf:"varint,1,opt,name=IsValid,proto3" json:"IsValid,omitempty"` 76 | ComapnyId string `protobuf:"bytes,2,opt,name=ComapnyId,proto3" json:"ComapnyId,omitempty"` 77 | Username string `protobuf:"bytes,3,opt,name=Username,proto3" json:"Username,omitempty"` 78 | Roles []int32 `protobuf:"varint,4,rep,packed,name=Roles,proto3" json:"Roles,omitempty"` 79 | } 80 | 81 | func (x *ValidateTokenResponse) Reset() { 82 | *x = ValidateTokenResponse{} 83 | if protoimpl.UnsafeEnabled { 84 | mi := &file_tokenValidate_proto_msgTypes[1] 85 | ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) 86 | ms.StoreMessageInfo(mi) 87 | } 88 | } 89 | 90 | func (x *ValidateTokenResponse) String() string { 91 | return protoimpl.X.MessageStringOf(x) 92 | } 93 | 94 | func (*ValidateTokenResponse) ProtoMessage() {} 95 | 96 | func (x *ValidateTokenResponse) ProtoReflect() protoreflect.Message { 97 | mi := &file_tokenValidate_proto_msgTypes[1] 98 | if protoimpl.UnsafeEnabled && x != nil { 99 | ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) 100 | if ms.LoadMessageInfo() == nil { 101 | ms.StoreMessageInfo(mi) 102 | } 103 | return ms 104 | } 105 | return mi.MessageOf(x) 106 | } 107 | 108 | // Deprecated: Use ValidateTokenResponse.ProtoReflect.Descriptor instead. 109 | func (*ValidateTokenResponse) Descriptor() ([]byte, []int) { 110 | return file_tokenValidate_proto_rawDescGZIP(), []int{1} 111 | } 112 | 113 | func (x *ValidateTokenResponse) GetIsValid() bool { 114 | if x != nil { 115 | return x.IsValid 116 | } 117 | return false 118 | } 119 | 120 | func (x *ValidateTokenResponse) GetComapnyId() string { 121 | if x != nil { 122 | return x.ComapnyId 123 | } 124 | return "" 125 | } 126 | 127 | func (x *ValidateTokenResponse) GetUsername() string { 128 | if x != nil { 129 | return x.Username 130 | } 131 | return "" 132 | } 133 | 134 | func (x *ValidateTokenResponse) GetRoles() []int32 { 135 | if x != nil { 136 | return x.Roles 137 | } 138 | return nil 139 | } 140 | 141 | var File_tokenValidate_proto protoreflect.FileDescriptor 142 | 143 | var file_tokenValidate_proto_rawDesc = []byte{ 144 | 0x0a, 0x13, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x56, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x65, 0x2e, 145 | 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x08, 0x72, 0x70, 0x63, 0x5f, 0x61, 0x75, 0x74, 0x68, 0x22, 146 | 0x2c, 0x0a, 0x14, 0x56, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x65, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 147 | 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 148 | 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x22, 0x81, 0x01, 149 | 0x0a, 0x15, 0x56, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x65, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x52, 150 | 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x49, 0x73, 0x56, 0x61, 0x6c, 151 | 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x49, 0x73, 0x56, 0x61, 0x6c, 0x69, 152 | 0x64, 0x12, 0x1c, 0x0a, 0x09, 0x43, 0x6f, 0x6d, 0x61, 0x70, 0x6e, 0x79, 0x49, 0x64, 0x18, 0x02, 153 | 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x43, 0x6f, 0x6d, 0x61, 0x70, 0x6e, 0x79, 0x49, 0x64, 0x12, 154 | 0x1a, 0x0a, 0x08, 0x55, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 155 | 0x09, 0x52, 0x08, 0x55, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x52, 156 | 0x6f, 0x6c, 0x65, 0x73, 0x18, 0x04, 0x20, 0x03, 0x28, 0x05, 0x52, 0x05, 0x52, 0x6f, 0x6c, 0x65, 157 | 0x73, 0x32, 0x69, 0x0a, 0x14, 0x56, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x65, 0x54, 0x6f, 0x6b, 158 | 0x65, 0x6e, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x51, 0x0a, 0x08, 0x56, 0x61, 0x6c, 159 | 0x69, 0x64, 0x61, 0x74, 0x65, 0x12, 0x1e, 0x2e, 0x72, 0x70, 0x63, 0x5f, 0x61, 0x75, 0x74, 0x68, 160 | 0x2e, 0x56, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x65, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x52, 0x65, 161 | 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1f, 0x2e, 0x72, 0x70, 0x63, 0x5f, 0x61, 0x75, 0x74, 0x68, 162 | 0x2e, 0x56, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x65, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x52, 0x65, 163 | 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x28, 0x01, 0x30, 0x01, 0x42, 0x0c, 0x5a, 0x0a, 164 | 0x2e, 0x2f, 0x72, 0x70, 0x63, 0x5f, 0x61, 0x75, 0x74, 0x68, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 165 | 0x6f, 0x33, 166 | } 167 | 168 | var ( 169 | file_tokenValidate_proto_rawDescOnce sync.Once 170 | file_tokenValidate_proto_rawDescData = file_tokenValidate_proto_rawDesc 171 | ) 172 | 173 | func file_tokenValidate_proto_rawDescGZIP() []byte { 174 | file_tokenValidate_proto_rawDescOnce.Do(func() { 175 | file_tokenValidate_proto_rawDescData = protoimpl.X.CompressGZIP(file_tokenValidate_proto_rawDescData) 176 | }) 177 | return file_tokenValidate_proto_rawDescData 178 | } 179 | 180 | var file_tokenValidate_proto_msgTypes = make([]protoimpl.MessageInfo, 2) 181 | var file_tokenValidate_proto_goTypes = []interface{}{ 182 | (*ValidateTokenRequest)(nil), // 0: rpc_auth.ValidateTokenRequest 183 | (*ValidateTokenResponse)(nil), // 1: rpc_auth.ValidateTokenResponse 184 | } 185 | var file_tokenValidate_proto_depIdxs = []int32{ 186 | 0, // 0: rpc_auth.ValidateTokenService.Validate:input_type -> rpc_auth.ValidateTokenRequest 187 | 1, // 1: rpc_auth.ValidateTokenService.Validate:output_type -> rpc_auth.ValidateTokenResponse 188 | 1, // [1:2] is the sub-list for method output_type 189 | 0, // [0:1] is the sub-list for method input_type 190 | 0, // [0:0] is the sub-list for extension type_name 191 | 0, // [0:0] is the sub-list for extension extendee 192 | 0, // [0:0] is the sub-list for field type_name 193 | } 194 | 195 | func init() { file_tokenValidate_proto_init() } 196 | func file_tokenValidate_proto_init() { 197 | if File_tokenValidate_proto != nil { 198 | return 199 | } 200 | if !protoimpl.UnsafeEnabled { 201 | file_tokenValidate_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { 202 | switch v := v.(*ValidateTokenRequest); i { 203 | case 0: 204 | return &v.state 205 | case 1: 206 | return &v.sizeCache 207 | case 2: 208 | return &v.unknownFields 209 | default: 210 | return nil 211 | } 212 | } 213 | file_tokenValidate_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { 214 | switch v := v.(*ValidateTokenResponse); i { 215 | case 0: 216 | return &v.state 217 | case 1: 218 | return &v.sizeCache 219 | case 2: 220 | return &v.unknownFields 221 | default: 222 | return nil 223 | } 224 | } 225 | } 226 | type x struct{} 227 | out := protoimpl.TypeBuilder{ 228 | File: protoimpl.DescBuilder{ 229 | GoPackagePath: reflect.TypeOf(x{}).PkgPath(), 230 | RawDescriptor: file_tokenValidate_proto_rawDesc, 231 | NumEnums: 0, 232 | NumMessages: 2, 233 | NumExtensions: 0, 234 | NumServices: 1, 235 | }, 236 | GoTypes: file_tokenValidate_proto_goTypes, 237 | DependencyIndexes: file_tokenValidate_proto_depIdxs, 238 | MessageInfos: file_tokenValidate_proto_msgTypes, 239 | }.Build() 240 | File_tokenValidate_proto = out.File 241 | file_tokenValidate_proto_rawDesc = nil 242 | file_tokenValidate_proto_goTypes = nil 243 | file_tokenValidate_proto_depIdxs = nil 244 | } 245 | -------------------------------------------------------------------------------- /rpc/rpc_auth/tokenValidate_grpc.pb.go: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-go-grpc. DO NOT EDIT. 2 | 3 | package rpc_auth 4 | 5 | import ( 6 | context "context" 7 | grpc "google.golang.org/grpc" 8 | codes "google.golang.org/grpc/codes" 9 | status "google.golang.org/grpc/status" 10 | ) 11 | 12 | // This is a compile-time assertion to ensure that this generated file 13 | // is compatible with the grpc package it is being compiled against. 14 | // Requires gRPC-Go v1.32.0 or later. 15 | const _ = grpc.SupportPackageIsVersion7 16 | 17 | // ValidateTokenServiceClient is the client API for ValidateTokenService service. 18 | // 19 | // 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. 20 | type ValidateTokenServiceClient interface { 21 | Validate(ctx context.Context, opts ...grpc.CallOption) (ValidateTokenService_ValidateClient, error) 22 | } 23 | 24 | type validateTokenServiceClient struct { 25 | cc grpc.ClientConnInterface 26 | } 27 | 28 | func NewValidateTokenServiceClient(cc grpc.ClientConnInterface) ValidateTokenServiceClient { 29 | return &validateTokenServiceClient{cc} 30 | } 31 | 32 | func (c *validateTokenServiceClient) Validate(ctx context.Context, opts ...grpc.CallOption) (ValidateTokenService_ValidateClient, error) { 33 | stream, err := c.cc.NewStream(ctx, &ValidateTokenService_ServiceDesc.Streams[0], "/rpc_auth.ValidateTokenService/Validate", opts...) 34 | if err != nil { 35 | return nil, err 36 | } 37 | x := &validateTokenServiceValidateClient{stream} 38 | return x, nil 39 | } 40 | 41 | type ValidateTokenService_ValidateClient interface { 42 | Send(*ValidateTokenRequest) error 43 | Recv() (*ValidateTokenResponse, error) 44 | grpc.ClientStream 45 | } 46 | 47 | type validateTokenServiceValidateClient struct { 48 | grpc.ClientStream 49 | } 50 | 51 | func (x *validateTokenServiceValidateClient) Send(m *ValidateTokenRequest) error { 52 | return x.ClientStream.SendMsg(m) 53 | } 54 | 55 | func (x *validateTokenServiceValidateClient) Recv() (*ValidateTokenResponse, error) { 56 | m := new(ValidateTokenResponse) 57 | if err := x.ClientStream.RecvMsg(m); err != nil { 58 | return nil, err 59 | } 60 | return m, nil 61 | } 62 | 63 | // ValidateTokenServiceServer is the server API for ValidateTokenService service. 64 | // All implementations must embed UnimplementedValidateTokenServiceServer 65 | // for forward compatibility 66 | type ValidateTokenServiceServer interface { 67 | Validate(ValidateTokenService_ValidateServer) error 68 | mustEmbedUnimplementedValidateTokenServiceServer() 69 | } 70 | 71 | // UnimplementedValidateTokenServiceServer must be embedded to have forward compatible implementations. 72 | type UnimplementedValidateTokenServiceServer struct { 73 | } 74 | 75 | func (UnimplementedValidateTokenServiceServer) Validate(ValidateTokenService_ValidateServer) error { 76 | return status.Errorf(codes.Unimplemented, "method Validate not implemented") 77 | } 78 | func (UnimplementedValidateTokenServiceServer) mustEmbedUnimplementedValidateTokenServiceServer() {} 79 | 80 | // UnsafeValidateTokenServiceServer may be embedded to opt out of forward compatibility for this service. 81 | // Use of this interface is not recommended, as added methods to ValidateTokenServiceServer will 82 | // result in compilation errors. 83 | type UnsafeValidateTokenServiceServer interface { 84 | mustEmbedUnimplementedValidateTokenServiceServer() 85 | } 86 | 87 | func RegisterValidateTokenServiceServer(s grpc.ServiceRegistrar, srv ValidateTokenServiceServer) { 88 | s.RegisterService(&ValidateTokenService_ServiceDesc, srv) 89 | } 90 | 91 | func _ValidateTokenService_Validate_Handler(srv interface{}, stream grpc.ServerStream) error { 92 | return srv.(ValidateTokenServiceServer).Validate(&validateTokenServiceValidateServer{stream}) 93 | } 94 | 95 | type ValidateTokenService_ValidateServer interface { 96 | Send(*ValidateTokenResponse) error 97 | Recv() (*ValidateTokenRequest, error) 98 | grpc.ServerStream 99 | } 100 | 101 | type validateTokenServiceValidateServer struct { 102 | grpc.ServerStream 103 | } 104 | 105 | func (x *validateTokenServiceValidateServer) Send(m *ValidateTokenResponse) error { 106 | return x.ServerStream.SendMsg(m) 107 | } 108 | 109 | func (x *validateTokenServiceValidateServer) Recv() (*ValidateTokenRequest, error) { 110 | m := new(ValidateTokenRequest) 111 | if err := x.ServerStream.RecvMsg(m); err != nil { 112 | return nil, err 113 | } 114 | return m, nil 115 | } 116 | 117 | // ValidateTokenService_ServiceDesc is the grpc.ServiceDesc for ValidateTokenService service. 118 | // It's only intended for direct use with grpc.RegisterService, 119 | // and not to be introspected or modified (even as a copy) 120 | var ValidateTokenService_ServiceDesc = grpc.ServiceDesc{ 121 | ServiceName: "rpc_auth.ValidateTokenService", 122 | HandlerType: (*ValidateTokenServiceServer)(nil), 123 | Methods: []grpc.MethodDesc{}, 124 | Streams: []grpc.StreamDesc{ 125 | { 126 | StreamName: "Validate", 127 | Handler: _ValidateTokenService_Validate_Handler, 128 | ServerStreams: true, 129 | ClientStreams: true, 130 | }, 131 | }, 132 | Metadata: "tokenValidate.proto", 133 | } 134 | -------------------------------------------------------------------------------- /task/add/add.task.handler.go: -------------------------------------------------------------------------------- 1 | package add 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/task/common" 5 | "github.com/architagr/golang-microservice-tutorial/task/data" 6 | "fmt" 7 | "net/http" 8 | 9 | "github.com/gin-gonic/gin" 10 | ) 11 | 12 | type Handler struct{ 13 | service *Service 14 | } 15 | 16 | func InitHandler(service *Service) *Handler { 17 | return &Handler{ 18 | service: service, 19 | } 20 | } 21 | 22 | 23 | func (handler * Handler) Add(c *gin.Context){ 24 | var addObj data.Task 25 | if err := c.ShouldBindJSON(&addObj); err != nil { 26 | common.BadRequest(c, http.StatusBadRequest, fmt.Sprintf("Request has invalid body"), 27 | []data.ErrorDetail{ 28 | data.ErrorDetail{ 29 | ErrorType: data.ErrorTypeError, 30 | ErrorMessage: fmt.Sprintf("Request has invalid body"), 31 | }, 32 | data.ErrorDetail{ 33 | ErrorType: data.ErrorTypeValidation, 34 | ErrorMessage: err.Error(), 35 | }, 36 | }) 37 | return 38 | } 39 | result, errorResponse := handler.service.Add(&addObj) 40 | if errorResponse != nil { 41 | common.BadRequest(c, http.StatusBadRequest, fmt.Sprintf("Error in Adding task by name %s", addObj.Name), 42 | []data.ErrorDetail{ 43 | *errorResponse, 44 | }) 45 | return 46 | } 47 | 48 | common.Ok(c, http.StatusOK, fmt.Sprintf("successfully Added task with name %s", addObj.Name), result) 49 | return 50 | } 51 | -------------------------------------------------------------------------------- /task/add/add.task.router.go: -------------------------------------------------------------------------------- 1 | package add 2 | 3 | import "github.com/gin-gonic/gin" 4 | 5 | type Router struct { 6 | addHandler *Handler 7 | } 8 | func InitRouter(h *Handler) *Router{ 9 | return &Router{ 10 | addHandler: h, 11 | } 12 | } 13 | func (r *Router) RegisterRoutes(group *gin.RouterGroup) { 14 | group.POST("", r.addHandler.Add) 15 | } 16 | -------------------------------------------------------------------------------- /task/add/add.task.service.go: -------------------------------------------------------------------------------- 1 | package add 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/task/data" 5 | "github.com/architagr/golang-microservice-tutorial/task/persistance" 6 | ) 7 | 8 | type Service struct { 9 | repository persistance.ITaskDbContext 10 | } 11 | 12 | func InitService(repo persistance.ITaskDbContext) *Service { 13 | return &Service{ 14 | repository: repo, 15 | } 16 | } 17 | 18 | func (service *Service) Add(emp *data.Task) (*data.Task, *data.ErrorDetail) { 19 | return service.repository.Add(emp) 20 | } 21 | -------------------------------------------------------------------------------- /task/cmd/task-api.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/task/add" 5 | "github.com/architagr/golang-microservice-tutorial/task/data" 6 | "github.com/architagr/golang-microservice-tutorial/task/delete" 7 | "github.com/architagr/golang-microservice-tutorial/task/get" 8 | "github.com/architagr/golang-microservice-tutorial/task/persistance" 9 | "github.com/architagr/golang-microservice-tutorial/task/update" 10 | 11 | "flag" 12 | 13 | "github.com/gin-gonic/gin" 14 | ) 15 | 16 | var ( 17 | port = flag.String("port", "8080", "port to be used") 18 | ip = flag.String("ip", "localhost", "ip to be used") 19 | ) 20 | 21 | func main() { 22 | flag.Parse() 23 | flags := data.NewFlags(*ip, *port) 24 | url, _ := flags.GetApplicationUrl() 25 | ginR := gin.Default() 26 | group := ginR.Group("api/task") 27 | 28 | repo := getPersistanceObj() 29 | registerGetRoutes(group, repo) 30 | registerPutRoutes(group, repo) 31 | registerAddRoutes(group, repo) 32 | registerDeleteRoutes(group, repo) 33 | 34 | ginR.Run(*url) 35 | } 36 | 37 | func getPersistanceObj() persistance.ITaskDbContext { 38 | return persistance.InitMongoDb("", "") 39 | } 40 | 41 | func registerGetRoutes(group *gin.RouterGroup, repo persistance.ITaskDbContext) { 42 | service := get.InitService(repo) 43 | handler := get.InitHandler(service) 44 | getRouter := get.InitRouter(handler) 45 | getRouter.RegisterRoutes(group) 46 | } 47 | 48 | func registerPutRoutes(group *gin.RouterGroup, repo persistance.ITaskDbContext) { 49 | service := update.InitService(repo) 50 | handler := update.InitHandler(service) 51 | putRouter := update.InitRouter(handler) 52 | putRouter.RegisterRoutes(group) 53 | } 54 | 55 | func registerAddRoutes(group *gin.RouterGroup, repo persistance.ITaskDbContext) { 56 | service := add.InitService(repo) 57 | handler := add.InitHandler(service) 58 | addRouter := add.InitRouter(handler) 59 | addRouter.RegisterRoutes(group) 60 | } 61 | 62 | func registerDeleteRoutes(group *gin.RouterGroup, repo persistance.ITaskDbContext) { 63 | service := delete.InitService(repo) 64 | handler := delete.InitHandler(service) 65 | deleteRouter := delete.InitRouter(handler) 66 | deleteRouter.RegisterRoutes(group) 67 | } 68 | -------------------------------------------------------------------------------- /task/common/commonFunctions.handler.go: -------------------------------------------------------------------------------- 1 | package common 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/task/data" 5 | "net/http" 6 | 7 | "github.com/gin-gonic/gin" 8 | ) 9 | 10 | func Ok(context *gin.Context, status int, message string, result interface{}) { 11 | context.AbortWithStatusJSON(status, data.Response{ 12 | Data: result, 13 | Status: status, 14 | Message: message, 15 | }) 16 | } 17 | func BadRequest(context *gin.Context, status int, message string, errors []data.ErrorDetail) { 18 | context.AbortWithStatusJSON(status, data.Response{ 19 | Error: errors, 20 | Status: status, 21 | Message: message, 22 | }) 23 | } 24 | 25 | func ReturnUnauthorized(context *gin.Context) { 26 | context.AbortWithStatusJSON(http.StatusUnauthorized, data.Response{ 27 | Error: []data.ErrorDetail{ 28 | { 29 | ErrorType: data.ErrorTypeUnauthorized, 30 | ErrorMessage: "You are not authorized to access this path", 31 | }, 32 | }, 33 | Status: http.StatusUnauthorized, 34 | Message: "Unauthorized access", 35 | }) 36 | } 37 | -------------------------------------------------------------------------------- /task/data/flags.go: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | import "fmt" 4 | 5 | type Flags struct { 6 | Ip string 7 | Port string 8 | } 9 | 10 | func (f *Flags) GetApplicationUrl() (*string, *ErrorDetail) { 11 | f, err := GetFlags() 12 | if err != nil { 13 | return nil, err 14 | } 15 | url := fmt.Sprintf("%s:%s", f.Ip, f.Port) 16 | return &url, nil 17 | } 18 | 19 | var flagsObj *Flags 20 | 21 | func NewFlags(ip, port string) *Flags { 22 | if flagsObj == nil { 23 | flagsObj = &Flags{ 24 | Ip: ip, 25 | Port: port, 26 | } 27 | } 28 | return flagsObj 29 | } 30 | 31 | func GetFlags() (*Flags, *ErrorDetail) { 32 | if flagsObj == nil { 33 | return nil, &ErrorDetail{ 34 | ErrorType: ErrorTypeFatal, 35 | ErrorMessage: "Flags not set", 36 | } 37 | } 38 | 39 | return flagsObj, nil 40 | } 41 | -------------------------------------------------------------------------------- /task/data/genricModels.go: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | import "fmt" 4 | 5 | const ( 6 | ErrorTypeFatal = "Fatal" 7 | ErrorTypeError = "Error" 8 | ErrorTypeValidation = "Validation Error" 9 | ErrorTypeInfo = "Info" 10 | ErrorTypeDebug = "Debug" 11 | ErrorTypeUnauthorized = "Unauthorized" 12 | ) 13 | 14 | type ErrorDetail struct { 15 | ErrorType string `json:"errorType,omitempty"` 16 | ErrorMessage string `json:"errorMessage,omitempty"` 17 | } 18 | 19 | func (err *ErrorDetail) Error() string { 20 | return fmt.Sprintf("ErrorType: %s, Error Message: %s", err.ErrorType, err.ErrorMessage) 21 | } 22 | 23 | type Response struct { 24 | Data interface{} `json:"data,omitempty"` 25 | Status int `json:"status,omitempty"` 26 | Error []ErrorDetail `json:"errors,omitempty"` 27 | Message string `json:"message,omitempty"` 28 | } 29 | -------------------------------------------------------------------------------- /task/data/task.data.go: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | var TaskData []Task 4 | 5 | func InitEmpData() { 6 | TaskData = []Task{ 7 | Task{ 8 | ID: 1, 9 | Name: "Tak 1", 10 | Employee: 1, 11 | }, 12 | Task{ 13 | ID: 2, 14 | Name: "Task 2", 15 | Employee: 2, 16 | }, 17 | Task{ 18 | ID: 3, 19 | Name: "Task 3", 20 | Employee: 3, 21 | }, 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /task/data/task.model.go: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | type Task struct { 4 | ID int `json:"id" uri:"id", form:"id"` 5 | Name string `json:"name" form:"name"` 6 | Employee int `json:"employee" form:"employee"` 7 | } 8 | -------------------------------------------------------------------------------- /task/delete/delete.task.handler.go: -------------------------------------------------------------------------------- 1 | package delete 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/task/common" 5 | "github.com/architagr/golang-microservice-tutorial/task/data" 6 | "fmt" 7 | "net/http" 8 | "strconv" 9 | 10 | "github.com/gin-gonic/gin" 11 | ) 12 | 13 | type Handler struct{ 14 | service *Service 15 | } 16 | 17 | func InitHandler(service *Service) *Handler { 18 | return &Handler{ 19 | service: service, 20 | } 21 | } 22 | 23 | 24 | func (handler * Handler) Delete(c *gin.Context){ 25 | id, err := strconv.Atoi(c.Param("id")) 26 | if err != nil { 27 | common.BadRequest(c, http.StatusBadRequest, fmt.Sprintf("Request has invalid task id"), 28 | []data.ErrorDetail{ 29 | data.ErrorDetail{ 30 | ErrorType: data.ErrorTypeError, 31 | ErrorMessage: fmt.Sprintf("Request has invalid task id"), 32 | }, 33 | }) 34 | return 35 | } 36 | result, errorResponse := handler.service.Delete(id) 37 | if errorResponse != nil { 38 | common.BadRequest(c, http.StatusBadRequest, fmt.Sprintf("Error in deleting task by id %d", id), 39 | []data.ErrorDetail{ 40 | *errorResponse, 41 | }) 42 | return 43 | } 44 | 45 | common.Ok(c, http.StatusOK, fmt.Sprintf("successfully deleted task with id: %d", id), result) 46 | return 47 | } -------------------------------------------------------------------------------- /task/delete/delete.task.router.go: -------------------------------------------------------------------------------- 1 | package delete 2 | 3 | import "github.com/gin-gonic/gin" 4 | 5 | type Router struct { 6 | deleteHandler *Handler 7 | } 8 | func InitRouter(h *Handler) *Router{ 9 | return &Router{ 10 | deleteHandler: h, 11 | } 12 | } 13 | func (r *Router) RegisterRoutes(group *gin.RouterGroup) { 14 | group.DELETE("/:id", r.deleteHandler.Delete) 15 | } 16 | -------------------------------------------------------------------------------- /task/delete/delete.task.service.go: -------------------------------------------------------------------------------- 1 | package delete 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/task/data" 5 | "github.com/architagr/golang-microservice-tutorial/task/persistance" 6 | ) 7 | 8 | type Service struct { 9 | repository persistance.ITaskDbContext 10 | } 11 | 12 | func InitService(repo persistance.ITaskDbContext) *Service { 13 | return &Service{ 14 | repository: repo, 15 | } 16 | } 17 | 18 | func (service *Service) Delete(id int) (*data.Task, *data.ErrorDetail) { 19 | return service.repository.Delete(id) 20 | } 21 | -------------------------------------------------------------------------------- /task/get/get.task.handler.go: -------------------------------------------------------------------------------- 1 | package get 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | "strconv" 7 | 8 | "github.com/architagr/golang-microservice-tutorial/task/common" 9 | "github.com/architagr/golang-microservice-tutorial/task/data" 10 | 11 | "github.com/gin-gonic/gin" 12 | ) 13 | 14 | type Handler struct { 15 | service *Service 16 | } 17 | 18 | func InitHandler(service *Service) *Handler { 19 | return &Handler{ 20 | service: service, 21 | } 22 | } 23 | 24 | func (handler *Handler) GetAll(c *gin.Context) { 25 | 26 | result, err := handler.service.GetAll() 27 | 28 | if err != nil { 29 | common.BadRequest(c, http.StatusBadRequest, fmt.Sprintf("Error in getting all tasks"), 30 | []data.ErrorDetail{ 31 | *err, 32 | }) 33 | return 34 | } 35 | 36 | common.Ok(c, http.StatusOK, fmt.Sprintf("successfully got list of task"), result) 37 | return 38 | } 39 | 40 | func (handler *Handler) GetById(c *gin.Context) { 41 | id, err := strconv.Atoi(c.Param("id")) 42 | if err != nil { 43 | common.BadRequest(c, http.StatusBadRequest, fmt.Sprintf("Request has invalid task id"), 44 | []data.ErrorDetail{ 45 | data.ErrorDetail{ 46 | ErrorType: data.ErrorTypeError, 47 | ErrorMessage: fmt.Sprintf("Request has invalid task id"), 48 | }, 49 | }) 50 | return 51 | } 52 | result, errorResponse := handler.service.GetById(id) 53 | if errorResponse != nil { 54 | common.BadRequest(c, http.StatusBadRequest, fmt.Sprintf("Error in getting task by id %d", id), 55 | []data.ErrorDetail{ 56 | *errorResponse, 57 | }) 58 | return 59 | } 60 | 61 | common.Ok(c, http.StatusOK, fmt.Sprintf("successfully got list of task"), result) 62 | return 63 | } 64 | -------------------------------------------------------------------------------- /task/get/get.task.router.go: -------------------------------------------------------------------------------- 1 | package get 2 | 3 | import ( 4 | "github.com/gin-gonic/gin" 5 | ) 6 | 7 | type Router struct { 8 | getHandler *Handler 9 | } 10 | 11 | func InitRouter(h *Handler) *Router{ 12 | return &Router{ 13 | getHandler: h, 14 | } 15 | } 16 | 17 | func (r *Router) RegisterRoutes(group *gin.RouterGroup) { 18 | group.GET("", r.getHandler.GetAll) 19 | group.GET("/:id", r.getHandler.GetById) 20 | } 21 | -------------------------------------------------------------------------------- /task/get/get.task.service.go: -------------------------------------------------------------------------------- 1 | package get 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/task/data" 5 | "github.com/architagr/golang-microservice-tutorial/task/persistance" 6 | ) 7 | 8 | type Service struct { 9 | repository persistance.ITaskDbContext 10 | } 11 | 12 | func InitService(repo persistance.ITaskDbContext) *Service { 13 | return &Service{ 14 | repository: repo, 15 | } 16 | } 17 | 18 | func (service *Service) GetAll() ([]data.Task, *data.ErrorDetail) { 19 | return service.repository.GetAll() 20 | } 21 | 22 | func (service *Service) GetById(id int) (*data.Task, *data.ErrorDetail) { 23 | return service.repository.GetById(id) 24 | } 25 | -------------------------------------------------------------------------------- /task/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/architagr/golang-microservice-tutorial/task 2 | 3 | go 1.16 4 | 5 | require github.com/gin-gonic/gin v1.7.2 // indirect 6 | -------------------------------------------------------------------------------- /task/go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 2 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 3 | github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= 4 | github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= 5 | github.com/gin-gonic/gin v1.7.2 h1:Tg03T9yM2xa8j6I3Z3oqLaQRSmKvxPd6g/2HJ6zICFA= 6 | github.com/gin-gonic/gin v1.7.2/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY= 7 | github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= 8 | github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= 9 | github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= 10 | github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= 11 | github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= 12 | github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE= 13 | github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= 14 | github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= 15 | github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= 16 | github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= 17 | github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= 18 | github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= 19 | github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= 20 | github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= 21 | github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= 22 | github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= 23 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= 24 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 25 | github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= 26 | github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= 27 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 28 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 29 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 30 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 31 | github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= 32 | github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= 33 | github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= 34 | github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= 35 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 36 | golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI= 37 | golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 38 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 39 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 40 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 41 | golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg= 42 | golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 43 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 44 | golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= 45 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 46 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 47 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 48 | gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= 49 | gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 50 | -------------------------------------------------------------------------------- /task/persistance/contract.go: -------------------------------------------------------------------------------- 1 | package persistance 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/task/data" 5 | ) 6 | type ITaskDbContext interface{ 7 | GetAll() ([]data.Task, *data.ErrorDetail) 8 | GetById(id int) (*data.Task, *data.ErrorDetail) 9 | Update(emp *data.Task) (*data.Task, *data.ErrorDetail) 10 | Add(emp *data.Task) (*data.Task, *data.ErrorDetail) 11 | Delete(id int) (*data.Task, *data.ErrorDetail) 12 | } 13 | 14 | -------------------------------------------------------------------------------- /task/persistance/task.mongodb.go: -------------------------------------------------------------------------------- 1 | package persistance 2 | 3 | import ( 4 | "fmt" 5 | "github.com/architagr/golang-microservice-tutorial/task/data" 6 | ) 7 | 8 | type TaskMongoDb struct { 9 | connctionString string 10 | dbname string 11 | } 12 | 13 | func InitMongoDb(connctionString, dbname string) TaskMongoDb { 14 | data.InitEmpData() 15 | return TaskMongoDb{ 16 | connctionString: connctionString, 17 | dbname: dbname, 18 | } 19 | } 20 | 21 | func (dbContext TaskMongoDb) GetAll() ([]data.Task, *data.ErrorDetail) { 22 | return data.TaskData, nil 23 | } 24 | 25 | func (dbContext TaskMongoDb) GetById(id int) (*data.Task, *data.ErrorDetail) { 26 | for _, t := range data.TaskData { 27 | if t.ID == id { 28 | return &t, nil 29 | } 30 | } 31 | 32 | return nil, &data.ErrorDetail{ 33 | ErrorType: data.ErrorTypeError, 34 | ErrorMessage: fmt.Sprintf("Task with id %d not found", id), 35 | } 36 | } 37 | 38 | func (dbContext TaskMongoDb) Update(task *data.Task) (*data.Task, *data.ErrorDetail) { 39 | var result []data.Task 40 | for _, t := range data.TaskData { 41 | if t.ID != task.ID { 42 | result = append(result, t) 43 | } 44 | } 45 | result = append(result, *task) 46 | data.TaskData = result 47 | return task, nil 48 | } 49 | func (dbContext TaskMongoDb) Add(task *data.Task) (*data.Task, *data.ErrorDetail) { 50 | task.ID = len(data.TaskData) + 1 51 | result := data.TaskData 52 | 53 | result = append(result, *task) 54 | data.TaskData = result 55 | 56 | return task, nil 57 | } 58 | func (dbContext TaskMongoDb) Delete(id int) (*data.Task, *data.ErrorDetail) { 59 | var result []data.Task 60 | found := false 61 | var returnData data.Task 62 | for _, t := range data.TaskData { 63 | if t.ID == id { 64 | found = true 65 | returnData = t 66 | } else { 67 | result = append(result, t) 68 | } 69 | } 70 | if found { 71 | data.TaskData = result 72 | return &returnData, nil 73 | } else { 74 | return nil, &data.ErrorDetail{ 75 | ErrorType: data.ErrorTypeError, 76 | ErrorMessage: fmt.Sprintf("Task with id %d not found", id), 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /task/update/update.task.handler.go: -------------------------------------------------------------------------------- 1 | package update 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/task/common" 5 | "github.com/architagr/golang-microservice-tutorial/task/data" 6 | 7 | "fmt" 8 | "net/http" 9 | "strconv" 10 | 11 | "github.com/gin-gonic/gin" 12 | ) 13 | 14 | type Handler struct { 15 | service *Service 16 | } 17 | 18 | func InitHandler(service *Service) *Handler { 19 | return &Handler{ 20 | service: service, 21 | } 22 | } 23 | 24 | func (h *Handler) Update(c *gin.Context) { 25 | id, err := strconv.Atoi(c.Param("id")) 26 | if err != nil { 27 | common.BadRequest(c, http.StatusBadRequest, fmt.Sprintf("Request has invalid task id"), 28 | []data.ErrorDetail{ 29 | data.ErrorDetail{ 30 | ErrorType: data.ErrorTypeError, 31 | ErrorMessage: fmt.Sprintf("Request has invalid task id"), 32 | }, 33 | }) 34 | return 35 | } 36 | var updateObj data.Task 37 | if err := c.ShouldBindJSON(&updateObj); err != nil { 38 | common.BadRequest(c, http.StatusBadRequest, fmt.Sprintf("Request has invalid body"), 39 | []data.ErrorDetail{ 40 | data.ErrorDetail{ 41 | ErrorType: data.ErrorTypeError, 42 | ErrorMessage: fmt.Sprintf("Request has invalid body"), 43 | }, 44 | data.ErrorDetail{ 45 | ErrorType: data.ErrorTypeValidation, 46 | ErrorMessage: err.Error(), 47 | }, 48 | }) 49 | return 50 | } 51 | updateObj.ID = id 52 | 53 | result, errorResponse := h.service.Update(&updateObj) 54 | if errorResponse != nil { 55 | common.BadRequest(c, http.StatusBadRequest, fmt.Sprintf("Error in updating task by id %d", id), 56 | []data.ErrorDetail{ 57 | *errorResponse, 58 | }) 59 | return 60 | } 61 | 62 | common.Ok(c, http.StatusOK, fmt.Sprintf("successfully updated task with id: %d", id), result) 63 | return 64 | } 65 | -------------------------------------------------------------------------------- /task/update/update.task.router.go: -------------------------------------------------------------------------------- 1 | package update 2 | 3 | import "github.com/gin-gonic/gin" 4 | 5 | type Router struct { 6 | updateHandler *Handler 7 | } 8 | func InitRouter(h *Handler) *Router{ 9 | return &Router{ 10 | updateHandler: h, 11 | } 12 | } 13 | func (r *Router) RegisterRoutes(group *gin.RouterGroup) { 14 | group.PUT("/:id", r.updateHandler.Update) 15 | } 16 | -------------------------------------------------------------------------------- /task/update/update.task.service.go: -------------------------------------------------------------------------------- 1 | package update 2 | 3 | import ( 4 | "github.com/architagr/golang-microservice-tutorial/task/data" 5 | "github.com/architagr/golang-microservice-tutorial/task/persistance" 6 | ) 7 | 8 | type Service struct { 9 | repository persistance.ITaskDbContext 10 | } 11 | 12 | func InitService(repo persistance.ITaskDbContext) *Service { 13 | return &Service{ 14 | repository: repo, 15 | } 16 | } 17 | 18 | func (service *Service) Update(emp *data.Task) (*data.Task, *data.ErrorDetail) { 19 | return service.repository.Update(emp) 20 | } 21 | -------------------------------------------------------------------------------- /testGrpc/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/architagr/golang-microservice-tutorial/testGrpc 2 | 3 | go 1.16 4 | 5 | replace github.com/architagr/golang-microservice-tutorial/rpc => ../rpc 6 | 7 | require ( 8 | github.com/architagr/golang-microservice-tutorial/rpc v0.0.0-00010101000000-000000000000 9 | google.golang.org/grpc v1.39.0 10 | ) 11 | -------------------------------------------------------------------------------- /testGrpc/go.sum: -------------------------------------------------------------------------------- 1 | cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= 2 | cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= 3 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 4 | github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= 5 | github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= 6 | github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= 7 | github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= 8 | github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= 9 | github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= 10 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 11 | github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= 12 | github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= 13 | github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= 14 | github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= 15 | github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.mod h1:hliV/p42l8fGbc6Y9bQ70uLwIvmJyVE5k4iMKlh8wCQ= 16 | github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= 17 | github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= 18 | github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= 19 | github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= 20 | github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 21 | github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 22 | github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= 23 | github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= 24 | github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= 25 | github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= 26 | github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= 27 | github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= 28 | github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= 29 | github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= 30 | github.com/golang/protobuf v1.4.3 h1:JjCZWpVbqXDqFVmTfYWEVTMIYrL/NPdPSCHPJ0T/raM= 31 | github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= 32 | github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= 33 | github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= 34 | github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= 35 | github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 36 | github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w= 37 | github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 38 | github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 39 | github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= 40 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 41 | github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= 42 | github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= 43 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 44 | github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= 45 | go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= 46 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 47 | golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 48 | golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= 49 | golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= 50 | golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= 51 | golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= 52 | golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 53 | golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 54 | golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 55 | golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 56 | golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 57 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 58 | golang.org/x/net v0.0.0-20200822124328-c89045814202 h1:VvcQYSHwXgi7W+TpUR6A9g6Up98WAHf3f/ulnJ62IyA= 59 | golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= 60 | golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= 61 | golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= 62 | golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 63 | golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 64 | golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 65 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 66 | golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 67 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 68 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 69 | golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884= 70 | golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 71 | golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= 72 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 73 | golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 74 | golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= 75 | golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= 76 | golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= 77 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 78 | golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= 79 | golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 80 | google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= 81 | google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= 82 | google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= 83 | google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= 84 | google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= 85 | google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 h1:+kGHl1aib/qcwaRi1CbqBZ1rk19r85MNUf8HaBghugY= 86 | google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= 87 | google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= 88 | google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= 89 | google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= 90 | google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= 91 | google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0= 92 | google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= 93 | google.golang.org/grpc v1.39.0 h1:Klz8I9kdtkIN6EpHHUOMLCYhTn/2WAe5a0s1hcBkdTI= 94 | google.golang.org/grpc v1.39.0/go.mod h1:PImNr+rS9TWYb2O4/emRugxiyHZ5JyHW5F+RPnDzfrE= 95 | google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= 96 | google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= 97 | google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= 98 | google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= 99 | google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= 100 | google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= 101 | google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= 102 | google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= 103 | google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c= 104 | google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= 105 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 106 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 107 | gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 108 | honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= 109 | honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= 110 | -------------------------------------------------------------------------------- /testGrpc/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "io" 7 | "log" 8 | 9 | rpc_auth "github.com/architagr/golang-microservice-tutorial/rpc/rpc_auth" 10 | "google.golang.org/grpc" 11 | ) 12 | 13 | func main() { 14 | var opts []grpc.DialOption 15 | opts = append(opts, grpc.WithInsecure()) 16 | conn, err := grpc.Dial("localhost:8080", opts...) 17 | if err != nil { 18 | fmt.Println("error in dial", err) 19 | } 20 | defer conn.Close() 21 | 22 | loginClient := rpc_auth.NewLoginServiceClient(conn) 23 | validateClient := rpc_auth.NewValidateTokenServiceClient(conn) 24 | 25 | loginResponse, err := loginClient.LoginSimpleRPC(context.Background(), &rpc_auth.LoginRequest{ 26 | Username: "steve@yopmail.com", 27 | Password: "steve123", 28 | RememberMe: true, 29 | }) 30 | 31 | if err != nil { 32 | fmt.Println("Simple RPC login error", err) 33 | } else { 34 | fmt.Println("Simple RPC login", loginResponse) 35 | } 36 | 37 | stream, err := validateClient.Validate(context.Background()) 38 | waitc := make(chan struct{}) 39 | go func() { 40 | for { 41 | in, err := stream.Recv() 42 | if err == io.EOF { 43 | // read done. 44 | close(waitc) 45 | return 46 | } 47 | if err != nil { 48 | log.Fatalf("Failed to receive a claims : %v", err) 49 | } 50 | log.Printf("Got claims - %+v -", in) 51 | } 52 | }() 53 | 54 | if err := stream.Send(&rpc_auth.ValidateTokenRequest{ 55 | Token: loginResponse.Token, 56 | }); err != nil { 57 | log.Fatalf("Failed to send a note: %v", err) 58 | } 59 | fmt.Println("sending validate request 2nd time") 60 | if err := stream.Send(&rpc_auth.ValidateTokenRequest{ 61 | Token: loginResponse.Token+"1", 62 | }); err != nil { 63 | log.Fatalf("Failed to send a note: %v", err) 64 | } 65 | stream.CloseSend() 66 | <-waitc 67 | } 68 | --------------------------------------------------------------------------------