├── .gitignore ├── LICENSE ├── README.md ├── docker-compose.yml ├── list ├── Dockerfile ├── main.go ├── models │ └── models.go └── server │ └── server.go ├── notifier ├── Dockerfile ├── main.go ├── server │ └── server.go └── smtp2go │ └── smtp2go.go ├── proto ├── list │ ├── service.pb.go │ └── service.proto ├── notifier │ ├── service.pb.go │ └── service.proto └── users │ ├── service.pb.go │ └── service.proto ├── users ├── Dockerfile ├── main.go ├── models │ └── models.go └── server │ └── server.go └── web ├── Dockerfile ├── main.go ├── pkg ├── tpl │ └── tpl.go └── web │ ├── handlers.go │ └── pages.go └── templates ├── home.html ├── layout.html └── user.html /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.dll 4 | *.so 5 | *.dylib 6 | 7 | # Test binary, build with `go test -c` 8 | *.test 9 | 10 | # Output of the go coverage tool, specifically when used with LiteIDE 11 | *.out 12 | 13 | # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 14 | .glide/ 15 | vendor 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Alex Pliutau 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 |  2 | 3 | ### "TODO List" experiment project 4 | 5 | This repository is an example project which demonstrates the use of microservices for a fictional TODO list application. The TODO backend is powered by 3 microservices, all of which happen to be written in Go, using MongoDB for manage the database and Docker to isolate and deploy the ecosystem. 6 | 7 | In real world each service should live in a separate repository, so teams can work separately and don't overlap each other, however in this demo project they just located in separate folders for easy use. 8 | 9 | ### Services organization 10 | 11 | The application consists of the following application services: 12 | 13 | | Service | Port | Description | Methods | 14 | |----------|-------|-------------------------------|--------------------------------------| 15 | | users | 50000 | Provides users information | CreateUser | 16 | | list | 50001 | Manages items in todo lists | CreateItem, GetUserItems, DeleteItem | 17 | | notifier | 50002 | Send email notifications | Email | 18 | 19 | Client web application is working on [http://localhost:3030](http://localhost:3030). 20 | 21 |  22 | 23 | ### Requirements 24 | 25 | - Install [Docker](https://www.docker.com/get-docker) 26 | - Install [Docker Compose](https://docs.docker.com/compose/install) 27 | 28 | ### Run 29 | 30 | ``` 31 | docker-compose pull 32 | docker-compose up 33 | ``` 34 | 35 | Go to [http://localhost:3030](http://localhost:3030) to test gRPC from webapp. 36 | 37 | ### Generate source code for the gRPC client from .proto files 38 | 39 | - Install [Go](https://golang.org/dl/) 40 | - Install [Protocol Buffers](https://github.com/google/protobuf/releases) 41 | - Install protoc plugin: `go get github.com/golang/protobuf/proto github.com/golang/protobuf/protoc-gen-go` 42 | 43 | ``` 44 | protoc --go_out=plugins=grpc:. proto/users/service.proto 45 | protoc --go_out=plugins=grpc:. proto/list/service.proto 46 | protoc --go_out=plugins=grpc:. proto/notifier/service.proto 47 | ``` 48 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | list: 4 | build: 5 | context: . 6 | dockerfile: list/Dockerfile 7 | environment: 8 | - "MONGO_HOST=db" 9 | image: pltvs/todo-list 10 | container_name: todo-list 11 | entrypoint: /go/bin/list 12 | depends_on: 13 | - db 14 | links: 15 | - db 16 | ports: 17 | - "50001:8080" 18 | 19 | users: 20 | build: 21 | context: . 22 | dockerfile: users/Dockerfile 23 | environment: 24 | - "MONGO_HOST=db" 25 | - "SRV_LIST_ADDR=list:8080" 26 | - "SRV_NOTIFIER_ADDR=notifier:8080" 27 | image: pltvs/todo-users 28 | container_name: todo-users 29 | entrypoint: /go/bin/users 30 | depends_on: 31 | - list 32 | - notifier 33 | - db 34 | links: 35 | - list 36 | - notifier 37 | - db 38 | ports: 39 | - "50000:8080" 40 | 41 | notifier: 42 | build: 43 | context: . 44 | dockerfile: notifier/Dockerfile 45 | container_name: todo-notifier 46 | entrypoint: /go/bin/notifier 47 | ports: 48 | - "50002:8080" 49 | 50 | web: 51 | build: 52 | context: . 53 | dockerfile: web/Dockerfile 54 | environment: 55 | - "SRV_USERS_ADDR=users:8080" 56 | - "SRV_LIST_ADDR=list:8080" 57 | image: pltvs/todo-web 58 | container_name: todo-web 59 | depends_on: 60 | - list 61 | - users 62 | links: 63 | - list 64 | - users 65 | ports: 66 | - "3030:8080" 67 | 68 | db: 69 | image: mongo:3.3 70 | container_name: todo-db 71 | logging: 72 | driver: none 73 | -------------------------------------------------------------------------------- /list/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.9-alpine 2 | 3 | ENV SRV_NAME list 4 | 5 | ENV PKG_PATH /go/src/github.com/wizelineacademy/GoWorkshop 6 | 7 | ADD proto $PKG_PATH/proto 8 | ADD $SRV_NAME $PKG_PATH/$SRV_NAME 9 | WORKDIR $PKG_PATH/$SRV_NAME 10 | RUN apk update && apk upgrade && \ 11 | apk add --no-cache bash git openssh 12 | RUN go get google.golang.org/grpc golang.org/x/net/context gopkg.in/mgo.v2 github.com/golang/protobuf/proto github.com/sirupsen/logrus 13 | 14 | RUN go install 15 | EXPOSE 8080 16 | -------------------------------------------------------------------------------- /list/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net" 5 | 6 | log "github.com/sirupsen/logrus" 7 | "github.com/wizelineacademy/GoWorkshop/list/server" 8 | "github.com/wizelineacademy/GoWorkshop/proto/list" 9 | "google.golang.org/grpc" 10 | ) 11 | 12 | func main() { 13 | listener, err := net.Listen("tcp", ":8080") 14 | if err != nil { 15 | log.WithError(err).Error("could not start service") 16 | return 17 | } 18 | 19 | log.Info("starting service on :8080") 20 | 21 | srv := grpc.NewServer() 22 | list.RegisterListServer(srv, &server.Server{}) 23 | srv.Serve(listener) 24 | } 25 | -------------------------------------------------------------------------------- /list/models/models.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | import ( 4 | "os" 5 | "time" 6 | 7 | log "github.com/sirupsen/logrus" 8 | mgo "gopkg.in/mgo.v2" 9 | "gopkg.in/mgo.v2/bson" 10 | ) 11 | 12 | type ( 13 | // Item type 14 | Item struct { 15 | Id bson.ObjectId `bson:"_id,omitempty" json:"id"` 16 | UserId string `bson:"user_id" json:"user_id"` 17 | Message string `json:"message"` 18 | } 19 | // ListRepository type 20 | ListRepository struct { 21 | C *mgo.Collection 22 | } 23 | ) 24 | 25 | var ( 26 | mongoSession *mgo.Session 27 | ) 28 | 29 | // CreateItem func 30 | func CreateItem(message string, userID string) (string, error) { 31 | c := getSession().Copy().DB("todo").C("list") 32 | 33 | repo := &ListRepository{c} 34 | 35 | return repo.Create(&Item{ 36 | Message: message, 37 | UserId: userID, 38 | }) 39 | } 40 | 41 | // DeleteItem func 42 | func DeleteItem(itemID string) error { 43 | c := getSession().Copy().DB("todo").C("list") 44 | 45 | repo := &ListRepository{c} 46 | return repo.Delete(itemID) 47 | } 48 | 49 | // GetUserItems func 50 | func GetUserItems(userID string) []Item { 51 | c := getSession().Copy().DB("todo").C("list") 52 | 53 | repo := &ListRepository{c} 54 | return repo.GetAll(userID) 55 | } 56 | 57 | // Create item 58 | func (r *ListRepository) Create(item *Item) (string, error) { 59 | objID := bson.NewObjectId() 60 | item.Id = objID 61 | err := r.C.Insert(&item) 62 | return item.Id.Hex(), err 63 | } 64 | 65 | // GetAll items 66 | func (r *ListRepository) GetAll(userID string) (items []Item) { 67 | iter := r.C.Find(bson.M{"user_id": userID}).Iter() 68 | result := Item{} 69 | for iter.Next(&result) { 70 | items = append(items, result) 71 | } 72 | return 73 | } 74 | 75 | // Delete item 76 | func (r *ListRepository) Delete(id string) error { 77 | return r.C.Remove(bson.M{"_id": bson.ObjectIdHex(id)}) 78 | } 79 | 80 | func getSession() *mgo.Session { 81 | if mongoSession == nil { 82 | var err error 83 | mongoSession, err = mgo.DialWithInfo(&mgo.DialInfo{ 84 | Addrs: []string{os.Getenv("MONGO_HOST")}, 85 | Username: "", 86 | Password: "", 87 | Timeout: 60 * time.Second, 88 | }) 89 | if err != nil { 90 | log.WithError(err).Fatal("could not connect to mongo") 91 | } 92 | } 93 | return mongoSession 94 | } 95 | -------------------------------------------------------------------------------- /list/server/server.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "net/http" 5 | 6 | log "github.com/sirupsen/logrus" 7 | "github.com/wizelineacademy/GoWorkshop/list/models" 8 | "github.com/wizelineacademy/GoWorkshop/proto/list" 9 | "golang.org/x/net/context" 10 | ) 11 | 12 | type Server struct{} 13 | 14 | func (s *Server) CreateItem(ctx context.Context, in *list.CreateItemRequest) (*list.CreateItemResponse, error) { 15 | itemID, err := models.CreateItem(in.Message, in.UserId) 16 | 17 | response := new(list.CreateItemResponse) 18 | if err == nil { 19 | log.WithField("id", itemID).Info("item created") 20 | 21 | response.Id = itemID 22 | response.Message = "Item created successfully" 23 | response.Code = http.StatusCreated 24 | } else { 25 | log.WithError(err).Error("unable to create item") 26 | 27 | response.Message = err.Error() 28 | response.Code = http.StatusInternalServerError 29 | } 30 | 31 | return response, err 32 | } 33 | 34 | func (s *Server) GetUserItems(ctx context.Context, in *list.GetUserItemsRequest) (*list.GetUserItemsResponse, error) { 35 | items := getUserItems(in.UserId) 36 | 37 | response := &list.GetUserItemsResponse{ 38 | Items: items, 39 | Code: http.StatusOK, 40 | } 41 | 42 | return response, nil 43 | } 44 | 45 | func (s *Server) DeleteItem(ctx context.Context, in *list.DeleteItemRequest) (*list.DeleteItemResponse, error) { 46 | err := models.DeleteItem(in.Id) 47 | 48 | response := new(list.DeleteItemResponse) 49 | if err == nil { 50 | log.WithField("id", in.Id).Info("item deleted") 51 | 52 | response.Message = "Item deleted successfully" 53 | response.Code = http.StatusOK 54 | } else { 55 | log.WithError(err).Error("unable to delete item") 56 | 57 | response.Message = err.Error() 58 | response.Code = http.StatusInternalServerError 59 | } 60 | 61 | return response, err 62 | } 63 | 64 | func getUserItems(userID string) []*list.Item { 65 | docs := models.GetUserItems(userID) 66 | 67 | items := []*list.Item{} 68 | for _, item := range docs { 69 | items = append(items, &list.Item{ 70 | Id: item.Id.Hex(), 71 | Message: item.Message, 72 | UserId: item.UserId, 73 | }) 74 | } 75 | 76 | return items 77 | } 78 | -------------------------------------------------------------------------------- /notifier/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.9-alpine 2 | 3 | ENV SRV_NAME notifier 4 | 5 | ENV PKG_PATH /go/src/github.com/wizelineacademy/GoWorkshop 6 | 7 | ADD proto $PKG_PATH/proto 8 | ADD $SRV_NAME $PKG_PATH/$SRV_NAME 9 | WORKDIR $PKG_PATH/$SRV_NAME 10 | RUN apk update && apk upgrade && \ 11 | apk add --no-cache bash git openssh 12 | RUN go get google.golang.org/grpc golang.org/x/net/context gopkg.in/mgo.v2 github.com/golang/protobuf/proto github.com/sirupsen/logrus 13 | 14 | RUN go install 15 | EXPOSE 8080 16 | -------------------------------------------------------------------------------- /notifier/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net" 5 | 6 | log "github.com/sirupsen/logrus" 7 | 8 | "github.com/wizelineacademy/GoWorkshop/notifier/server" 9 | "github.com/wizelineacademy/GoWorkshop/proto/notifier" 10 | "google.golang.org/grpc" 11 | ) 12 | 13 | func main() { 14 | listener, err := net.Listen("tcp", ":8080") 15 | if err != nil { 16 | log.WithError(err).Error("could not start service") 17 | return 18 | } 19 | 20 | log.Info("starting service on :8080") 21 | 22 | srv := grpc.NewServer() 23 | notifier.RegisterNotifierServer(srv, &server.Server{}) 24 | srv.Serve(listener) 25 | } 26 | -------------------------------------------------------------------------------- /notifier/server/server.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "github.com/wizelineacademy/GoWorkshop/notifier/smtp2go" 5 | "github.com/wizelineacademy/GoWorkshop/proto/notifier" 6 | "golang.org/x/net/context" 7 | ) 8 | 9 | type Server struct{} 10 | 11 | func (s *Server) Email(ctx context.Context, in *notifier.EmailRequest) (*notifier.EmailResponse, error) { 12 | err := smtp2go.SendEmail(in.Email, "Welcome to Go Workshop", "GitHub repository: https://github.com/wizelineacademy/GoWorkshop") 13 | 14 | if err != nil { 15 | return ¬ifier.EmailResponse{ 16 | Message: err.Error(), 17 | Code: 500, 18 | }, nil 19 | } 20 | 21 | return ¬ifier.EmailResponse{ 22 | Message: "OK", 23 | Code: 200, 24 | }, nil 25 | } 26 | -------------------------------------------------------------------------------- /notifier/smtp2go/smtp2go.go: -------------------------------------------------------------------------------- 1 | package smtp2go 2 | 3 | import ( 4 | "encoding/base64" 5 | "fmt" 6 | "net/smtp" 7 | ) 8 | 9 | // SendEmail : sends email using smtp2go testing SMTP service 10 | func SendEmail(to string, subject string, body string) error { 11 | pass, err := base64.StdEncoding.DecodeString("WlVjdGJ2VGxBbDBM") 12 | if err != nil { 13 | return err 14 | } 15 | 16 | auth := smtp.PlainAuth("", 17 | "alexander.plutov@gmail.com", 18 | string(pass), 19 | "mail.smtp2go.com", 20 | ) 21 | 22 | header := make(map[string]string) 23 | header["From"] = "alexander.plutov@gmail.com" 24 | header["To"] = to 25 | header["Subject"] = subject 26 | header["MIME-Version"] = "1.0" 27 | header["Content-Type"] = "text/plain; charset=\"utf-8\"" 28 | header["Content-Transfer-Encoding"] = "base64" 29 | 30 | message := "" 31 | for k, v := range header { 32 | message += fmt.Sprintf("%s: %s\r\n", k, v) 33 | } 34 | message += "\r\n" + base64.StdEncoding.EncodeToString([]byte(body)) 35 | 36 | return smtp.SendMail("mail.smtp2go.com:587", auth, "alexander.plutov@gmail.com", []string{to}, []byte(message)) 37 | } 38 | -------------------------------------------------------------------------------- /proto/list/service.pb.go: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-go. DO NOT EDIT. 2 | // source: proto/list/service.proto 3 | 4 | /* 5 | Package list is a generated protocol buffer package. 6 | 7 | It is generated from these files: 8 | proto/list/service.proto 9 | 10 | It has these top-level messages: 11 | CreateItemRequest 12 | CreateItemResponse 13 | GetUserItemsRequest 14 | GetUserItemsResponse 15 | Item 16 | DeleteItemRequest 17 | DeleteItemResponse 18 | */ 19 | package list 20 | 21 | import proto "github.com/golang/protobuf/proto" 22 | import fmt "fmt" 23 | import math "math" 24 | 25 | import ( 26 | context "golang.org/x/net/context" 27 | grpc "google.golang.org/grpc" 28 | ) 29 | 30 | // Reference imports to suppress errors if they are not otherwise used. 31 | var _ = proto.Marshal 32 | var _ = fmt.Errorf 33 | var _ = math.Inf 34 | 35 | // This is a compile-time assertion to ensure that this generated file 36 | // is compatible with the proto package it is being compiled against. 37 | // A compilation error at this line likely means your copy of the 38 | // proto package needs to be updated. 39 | const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package 40 | 41 | type CreateItemRequest struct { 42 | Message string `protobuf:"bytes,1,opt,name=message" json:"message,omitempty"` 43 | UserId string `protobuf:"bytes,2,opt,name=userId" json:"userId,omitempty"` 44 | } 45 | 46 | func (m *CreateItemRequest) Reset() { *m = CreateItemRequest{} } 47 | func (m *CreateItemRequest) String() string { return proto.CompactTextString(m) } 48 | func (*CreateItemRequest) ProtoMessage() {} 49 | func (*CreateItemRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } 50 | 51 | func (m *CreateItemRequest) GetMessage() string { 52 | if m != nil { 53 | return m.Message 54 | } 55 | return "" 56 | } 57 | 58 | func (m *CreateItemRequest) GetUserId() string { 59 | if m != nil { 60 | return m.UserId 61 | } 62 | return "" 63 | } 64 | 65 | type CreateItemResponse struct { 66 | Message string `protobuf:"bytes,1,opt,name=message" json:"message,omitempty"` 67 | Code uint64 `protobuf:"varint,2,opt,name=code" json:"code,omitempty"` 68 | Id string `protobuf:"bytes,3,opt,name=id" json:"id,omitempty"` 69 | } 70 | 71 | func (m *CreateItemResponse) Reset() { *m = CreateItemResponse{} } 72 | func (m *CreateItemResponse) String() string { return proto.CompactTextString(m) } 73 | func (*CreateItemResponse) ProtoMessage() {} 74 | func (*CreateItemResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} } 75 | 76 | func (m *CreateItemResponse) GetMessage() string { 77 | if m != nil { 78 | return m.Message 79 | } 80 | return "" 81 | } 82 | 83 | func (m *CreateItemResponse) GetCode() uint64 { 84 | if m != nil { 85 | return m.Code 86 | } 87 | return 0 88 | } 89 | 90 | func (m *CreateItemResponse) GetId() string { 91 | if m != nil { 92 | return m.Id 93 | } 94 | return "" 95 | } 96 | 97 | type GetUserItemsRequest struct { 98 | UserId string `protobuf:"bytes,1,opt,name=userId" json:"userId,omitempty"` 99 | } 100 | 101 | func (m *GetUserItemsRequest) Reset() { *m = GetUserItemsRequest{} } 102 | func (m *GetUserItemsRequest) String() string { return proto.CompactTextString(m) } 103 | func (*GetUserItemsRequest) ProtoMessage() {} 104 | func (*GetUserItemsRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} } 105 | 106 | func (m *GetUserItemsRequest) GetUserId() string { 107 | if m != nil { 108 | return m.UserId 109 | } 110 | return "" 111 | } 112 | 113 | type GetUserItemsResponse struct { 114 | Items []*Item `protobuf:"bytes,1,rep,name=items" json:"items,omitempty"` 115 | Code uint64 `protobuf:"varint,2,opt,name=code" json:"code,omitempty"` 116 | } 117 | 118 | func (m *GetUserItemsResponse) Reset() { *m = GetUserItemsResponse{} } 119 | func (m *GetUserItemsResponse) String() string { return proto.CompactTextString(m) } 120 | func (*GetUserItemsResponse) ProtoMessage() {} 121 | func (*GetUserItemsResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{3} } 122 | 123 | func (m *GetUserItemsResponse) GetItems() []*Item { 124 | if m != nil { 125 | return m.Items 126 | } 127 | return nil 128 | } 129 | 130 | func (m *GetUserItemsResponse) GetCode() uint64 { 131 | if m != nil { 132 | return m.Code 133 | } 134 | return 0 135 | } 136 | 137 | type Item struct { 138 | Id string `protobuf:"bytes,1,opt,name=id" json:"id,omitempty"` 139 | Message string `protobuf:"bytes,2,opt,name=message" json:"message,omitempty"` 140 | UserId string `protobuf:"bytes,3,opt,name=userId" json:"userId,omitempty"` 141 | } 142 | 143 | func (m *Item) Reset() { *m = Item{} } 144 | func (m *Item) String() string { return proto.CompactTextString(m) } 145 | func (*Item) ProtoMessage() {} 146 | func (*Item) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{4} } 147 | 148 | func (m *Item) GetId() string { 149 | if m != nil { 150 | return m.Id 151 | } 152 | return "" 153 | } 154 | 155 | func (m *Item) GetMessage() string { 156 | if m != nil { 157 | return m.Message 158 | } 159 | return "" 160 | } 161 | 162 | func (m *Item) GetUserId() string { 163 | if m != nil { 164 | return m.UserId 165 | } 166 | return "" 167 | } 168 | 169 | type DeleteItemRequest struct { 170 | Id string `protobuf:"bytes,1,opt,name=id" json:"id,omitempty"` 171 | } 172 | 173 | func (m *DeleteItemRequest) Reset() { *m = DeleteItemRequest{} } 174 | func (m *DeleteItemRequest) String() string { return proto.CompactTextString(m) } 175 | func (*DeleteItemRequest) ProtoMessage() {} 176 | func (*DeleteItemRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{5} } 177 | 178 | func (m *DeleteItemRequest) GetId() string { 179 | if m != nil { 180 | return m.Id 181 | } 182 | return "" 183 | } 184 | 185 | type DeleteItemResponse struct { 186 | Message string `protobuf:"bytes,1,opt,name=message" json:"message,omitempty"` 187 | Code uint64 `protobuf:"varint,2,opt,name=code" json:"code,omitempty"` 188 | } 189 | 190 | func (m *DeleteItemResponse) Reset() { *m = DeleteItemResponse{} } 191 | func (m *DeleteItemResponse) String() string { return proto.CompactTextString(m) } 192 | func (*DeleteItemResponse) ProtoMessage() {} 193 | func (*DeleteItemResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{6} } 194 | 195 | func (m *DeleteItemResponse) GetMessage() string { 196 | if m != nil { 197 | return m.Message 198 | } 199 | return "" 200 | } 201 | 202 | func (m *DeleteItemResponse) GetCode() uint64 { 203 | if m != nil { 204 | return m.Code 205 | } 206 | return 0 207 | } 208 | 209 | func init() { 210 | proto.RegisterType((*CreateItemRequest)(nil), "list.CreateItemRequest") 211 | proto.RegisterType((*CreateItemResponse)(nil), "list.CreateItemResponse") 212 | proto.RegisterType((*GetUserItemsRequest)(nil), "list.GetUserItemsRequest") 213 | proto.RegisterType((*GetUserItemsResponse)(nil), "list.GetUserItemsResponse") 214 | proto.RegisterType((*Item)(nil), "list.Item") 215 | proto.RegisterType((*DeleteItemRequest)(nil), "list.DeleteItemRequest") 216 | proto.RegisterType((*DeleteItemResponse)(nil), "list.DeleteItemResponse") 217 | } 218 | 219 | // Reference imports to suppress errors if they are not otherwise used. 220 | var _ context.Context 221 | var _ grpc.ClientConn 222 | 223 | // This is a compile-time assertion to ensure that this generated file 224 | // is compatible with the grpc package it is being compiled against. 225 | const _ = grpc.SupportPackageIsVersion4 226 | 227 | // Client API for List service 228 | 229 | type ListClient interface { 230 | CreateItem(ctx context.Context, in *CreateItemRequest, opts ...grpc.CallOption) (*CreateItemResponse, error) 231 | GetUserItems(ctx context.Context, in *GetUserItemsRequest, opts ...grpc.CallOption) (*GetUserItemsResponse, error) 232 | DeleteItem(ctx context.Context, in *DeleteItemRequest, opts ...grpc.CallOption) (*DeleteItemResponse, error) 233 | } 234 | 235 | type listClient struct { 236 | cc *grpc.ClientConn 237 | } 238 | 239 | func NewListClient(cc *grpc.ClientConn) ListClient { 240 | return &listClient{cc} 241 | } 242 | 243 | func (c *listClient) CreateItem(ctx context.Context, in *CreateItemRequest, opts ...grpc.CallOption) (*CreateItemResponse, error) { 244 | out := new(CreateItemResponse) 245 | err := grpc.Invoke(ctx, "/list.List/CreateItem", in, out, c.cc, opts...) 246 | if err != nil { 247 | return nil, err 248 | } 249 | return out, nil 250 | } 251 | 252 | func (c *listClient) GetUserItems(ctx context.Context, in *GetUserItemsRequest, opts ...grpc.CallOption) (*GetUserItemsResponse, error) { 253 | out := new(GetUserItemsResponse) 254 | err := grpc.Invoke(ctx, "/list.List/GetUserItems", in, out, c.cc, opts...) 255 | if err != nil { 256 | return nil, err 257 | } 258 | return out, nil 259 | } 260 | 261 | func (c *listClient) DeleteItem(ctx context.Context, in *DeleteItemRequest, opts ...grpc.CallOption) (*DeleteItemResponse, error) { 262 | out := new(DeleteItemResponse) 263 | err := grpc.Invoke(ctx, "/list.List/DeleteItem", in, out, c.cc, opts...) 264 | if err != nil { 265 | return nil, err 266 | } 267 | return out, nil 268 | } 269 | 270 | // Server API for List service 271 | 272 | type ListServer interface { 273 | CreateItem(context.Context, *CreateItemRequest) (*CreateItemResponse, error) 274 | GetUserItems(context.Context, *GetUserItemsRequest) (*GetUserItemsResponse, error) 275 | DeleteItem(context.Context, *DeleteItemRequest) (*DeleteItemResponse, error) 276 | } 277 | 278 | func RegisterListServer(s *grpc.Server, srv ListServer) { 279 | s.RegisterService(&_List_serviceDesc, srv) 280 | } 281 | 282 | func _List_CreateItem_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { 283 | in := new(CreateItemRequest) 284 | if err := dec(in); err != nil { 285 | return nil, err 286 | } 287 | if interceptor == nil { 288 | return srv.(ListServer).CreateItem(ctx, in) 289 | } 290 | info := &grpc.UnaryServerInfo{ 291 | Server: srv, 292 | FullMethod: "/list.List/CreateItem", 293 | } 294 | handler := func(ctx context.Context, req interface{}) (interface{}, error) { 295 | return srv.(ListServer).CreateItem(ctx, req.(*CreateItemRequest)) 296 | } 297 | return interceptor(ctx, in, info, handler) 298 | } 299 | 300 | func _List_GetUserItems_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { 301 | in := new(GetUserItemsRequest) 302 | if err := dec(in); err != nil { 303 | return nil, err 304 | } 305 | if interceptor == nil { 306 | return srv.(ListServer).GetUserItems(ctx, in) 307 | } 308 | info := &grpc.UnaryServerInfo{ 309 | Server: srv, 310 | FullMethod: "/list.List/GetUserItems", 311 | } 312 | handler := func(ctx context.Context, req interface{}) (interface{}, error) { 313 | return srv.(ListServer).GetUserItems(ctx, req.(*GetUserItemsRequest)) 314 | } 315 | return interceptor(ctx, in, info, handler) 316 | } 317 | 318 | func _List_DeleteItem_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { 319 | in := new(DeleteItemRequest) 320 | if err := dec(in); err != nil { 321 | return nil, err 322 | } 323 | if interceptor == nil { 324 | return srv.(ListServer).DeleteItem(ctx, in) 325 | } 326 | info := &grpc.UnaryServerInfo{ 327 | Server: srv, 328 | FullMethod: "/list.List/DeleteItem", 329 | } 330 | handler := func(ctx context.Context, req interface{}) (interface{}, error) { 331 | return srv.(ListServer).DeleteItem(ctx, req.(*DeleteItemRequest)) 332 | } 333 | return interceptor(ctx, in, info, handler) 334 | } 335 | 336 | var _List_serviceDesc = grpc.ServiceDesc{ 337 | ServiceName: "list.List", 338 | HandlerType: (*ListServer)(nil), 339 | Methods: []grpc.MethodDesc{ 340 | { 341 | MethodName: "CreateItem", 342 | Handler: _List_CreateItem_Handler, 343 | }, 344 | { 345 | MethodName: "GetUserItems", 346 | Handler: _List_GetUserItems_Handler, 347 | }, 348 | { 349 | MethodName: "DeleteItem", 350 | Handler: _List_DeleteItem_Handler, 351 | }, 352 | }, 353 | Streams: []grpc.StreamDesc{}, 354 | Metadata: "proto/list/service.proto", 355 | } 356 | 357 | func init() { proto.RegisterFile("proto/list/service.proto", fileDescriptor0) } 358 | 359 | var fileDescriptor0 = []byte{ 360 | // 302 bytes of a gzipped FileDescriptorProto 361 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x92, 0xcf, 0x4e, 0x83, 0x40, 362 | 0x10, 0xc6, 0xbb, 0x14, 0x6b, 0x1c, 0x8d, 0x49, 0x47, 0xa3, 0x2b, 0x27, 0xb2, 0x5e, 0x7a, 0x91, 363 | 0x26, 0xf5, 0x09, 0xea, 0x9f, 0xd4, 0x26, 0x3d, 0x91, 0xf8, 0x00, 0xb5, 0x4c, 0xcc, 0x26, 0x45, 364 | 0x2a, 0xb3, 0xf5, 0x4d, 0x7d, 0x1f, 0xc3, 0xc2, 0x0a, 0x08, 0x7a, 0xf0, 0xc6, 0xce, 0x7c, 0xf9, 365 | 0xe6, 0x37, 0xdf, 0x00, 0x72, 0x97, 0x67, 0x26, 0x9b, 0x6e, 0x35, 0x9b, 0x29, 0x53, 0xfe, 0xa1, 366 | 0x37, 0x14, 0xd9, 0x12, 0xfa, 0x45, 0x4d, 0x3d, 0xc2, 0xf8, 0x3e, 0xa7, 0xb5, 0xa1, 0xa5, 0xa1, 367 | 0x34, 0xa6, 0xf7, 0x3d, 0xb1, 0x41, 0x09, 0x87, 0x29, 0x31, 0xaf, 0x5f, 0x49, 0x8a, 0x50, 0x4c, 368 | 0x8e, 0x62, 0xf7, 0xc4, 0x0b, 0x18, 0xed, 0x99, 0xf2, 0x65, 0x22, 0x3d, 0xdb, 0xa8, 0x5e, 0x2a, 369 | 0x06, 0x6c, 0xda, 0xf0, 0x2e, 0x7b, 0x63, 0xfa, 0xc3, 0x07, 0xc1, 0xdf, 0x64, 0x09, 0x59, 0x17, 370 | 0x3f, 0xb6, 0xdf, 0x78, 0x0a, 0x9e, 0x4e, 0xe4, 0xd0, 0x0a, 0x3d, 0x9d, 0xa8, 0x1b, 0x38, 0x5b, 371 | 0x90, 0x79, 0x2e, 0x06, 0x18, 0x4a, 0xd9, 0xc1, 0xd5, 0x08, 0xa2, 0x85, 0xb0, 0x82, 0xf3, 0xb6, 372 | 0xbc, 0x82, 0x08, 0xe1, 0x40, 0x17, 0x05, 0x29, 0xc2, 0xe1, 0xe4, 0x78, 0x06, 0x51, 0xb1, 0x77, 373 | 0x64, 0x39, 0xcb, 0x46, 0x1f, 0x8c, 0x7a, 0x02, 0xbf, 0x90, 0x54, 0x50, 0xc2, 0x41, 0x35, 0x57, 374 | 0xf2, 0x7e, 0x8b, 0x66, 0xd8, 0xe2, 0xba, 0x86, 0xf1, 0x03, 0x6d, 0xa9, 0x9d, 0xf0, 0x0f, 0x5b, 375 | 0x75, 0x07, 0xd8, 0x14, 0xfd, 0x27, 0xbf, 0xd9, 0xa7, 0x00, 0x7f, 0xa5, 0xd9, 0xe0, 0x1c, 0xa0, 376 | 0x3e, 0x06, 0x5e, 0x96, 0x0b, 0x77, 0xae, 0x1c, 0xc8, 0x6e, 0xa3, 0x9c, 0xab, 0x06, 0xb8, 0x80, 377 | 0x93, 0x66, 0x98, 0x78, 0x55, 0x6a, 0x7b, 0xee, 0x11, 0x04, 0x7d, 0xad, 0x6f, 0xa3, 0x39, 0x40, 378 | 0xbd, 0x98, 0x63, 0xe9, 0xe4, 0xe1, 0x58, 0xba, 0x19, 0xa8, 0xc1, 0xcb, 0xc8, 0xfe, 0xaf, 0xb7, 379 | 0x5f, 0x01, 0x00, 0x00, 0xff, 0xff, 0xe3, 0x97, 0x64, 0x64, 0xcb, 0x02, 0x00, 0x00, 380 | } 381 | -------------------------------------------------------------------------------- /proto/list/service.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package list; 4 | 5 | service List { 6 | rpc CreateItem(CreateItemRequest) returns (CreateItemResponse) {} 7 | rpc GetUserItems(GetUserItemsRequest) returns (GetUserItemsResponse) {} 8 | rpc DeleteItem(DeleteItemRequest) returns (DeleteItemResponse) {} 9 | } 10 | 11 | message CreateItemRequest { 12 | string message = 1; 13 | string userId = 2; 14 | } 15 | 16 | message CreateItemResponse { 17 | string message = 1; 18 | uint64 code = 2; 19 | string id = 3; 20 | } 21 | 22 | message GetUserItemsRequest { 23 | string userId = 1; 24 | } 25 | 26 | message GetUserItemsResponse { 27 | repeated Item items = 1; 28 | uint64 code = 2; 29 | } 30 | 31 | message Item { 32 | string id = 1; 33 | string message = 2; 34 | string userId = 3; 35 | } 36 | 37 | message DeleteItemRequest { 38 | string id = 1; 39 | } 40 | 41 | message DeleteItemResponse { 42 | string message = 1; 43 | uint64 code = 2; 44 | } 45 | -------------------------------------------------------------------------------- /proto/notifier/service.pb.go: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-go. DO NOT EDIT. 2 | // source: proto/notifier/service.proto 3 | 4 | /* 5 | Package notifier is a generated protocol buffer package. 6 | 7 | It is generated from these files: 8 | proto/notifier/service.proto 9 | 10 | It has these top-level messages: 11 | EmailRequest 12 | EmailResponse 13 | */ 14 | package notifier 15 | 16 | import proto "github.com/golang/protobuf/proto" 17 | import fmt "fmt" 18 | import math "math" 19 | 20 | import ( 21 | context "golang.org/x/net/context" 22 | grpc "google.golang.org/grpc" 23 | ) 24 | 25 | // Reference imports to suppress errors if they are not otherwise used. 26 | var _ = proto.Marshal 27 | var _ = fmt.Errorf 28 | var _ = math.Inf 29 | 30 | // This is a compile-time assertion to ensure that this generated file 31 | // is compatible with the proto package it is being compiled against. 32 | // A compilation error at this line likely means your copy of the 33 | // proto package needs to be updated. 34 | const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package 35 | 36 | type EmailRequest struct { 37 | Email string `protobuf:"bytes,1,opt,name=email" json:"email,omitempty"` 38 | } 39 | 40 | func (m *EmailRequest) Reset() { *m = EmailRequest{} } 41 | func (m *EmailRequest) String() string { return proto.CompactTextString(m) } 42 | func (*EmailRequest) ProtoMessage() {} 43 | func (*EmailRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } 44 | 45 | func (m *EmailRequest) GetEmail() string { 46 | if m != nil { 47 | return m.Email 48 | } 49 | return "" 50 | } 51 | 52 | type EmailResponse struct { 53 | Message string `protobuf:"bytes,1,opt,name=message" json:"message,omitempty"` 54 | Code uint64 `protobuf:"varint,2,opt,name=code" json:"code,omitempty"` 55 | } 56 | 57 | func (m *EmailResponse) Reset() { *m = EmailResponse{} } 58 | func (m *EmailResponse) String() string { return proto.CompactTextString(m) } 59 | func (*EmailResponse) ProtoMessage() {} 60 | func (*EmailResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} } 61 | 62 | func (m *EmailResponse) GetMessage() string { 63 | if m != nil { 64 | return m.Message 65 | } 66 | return "" 67 | } 68 | 69 | func (m *EmailResponse) GetCode() uint64 { 70 | if m != nil { 71 | return m.Code 72 | } 73 | return 0 74 | } 75 | 76 | func init() { 77 | proto.RegisterType((*EmailRequest)(nil), "notifier.EmailRequest") 78 | proto.RegisterType((*EmailResponse)(nil), "notifier.EmailResponse") 79 | } 80 | 81 | // Reference imports to suppress errors if they are not otherwise used. 82 | var _ context.Context 83 | var _ grpc.ClientConn 84 | 85 | // This is a compile-time assertion to ensure that this generated file 86 | // is compatible with the grpc package it is being compiled against. 87 | const _ = grpc.SupportPackageIsVersion4 88 | 89 | // Client API for Notifier service 90 | 91 | type NotifierClient interface { 92 | Email(ctx context.Context, in *EmailRequest, opts ...grpc.CallOption) (*EmailResponse, error) 93 | } 94 | 95 | type notifierClient struct { 96 | cc *grpc.ClientConn 97 | } 98 | 99 | func NewNotifierClient(cc *grpc.ClientConn) NotifierClient { 100 | return ¬ifierClient{cc} 101 | } 102 | 103 | func (c *notifierClient) Email(ctx context.Context, in *EmailRequest, opts ...grpc.CallOption) (*EmailResponse, error) { 104 | out := new(EmailResponse) 105 | err := grpc.Invoke(ctx, "/notifier.Notifier/Email", in, out, c.cc, opts...) 106 | if err != nil { 107 | return nil, err 108 | } 109 | return out, nil 110 | } 111 | 112 | // Server API for Notifier service 113 | 114 | type NotifierServer interface { 115 | Email(context.Context, *EmailRequest) (*EmailResponse, error) 116 | } 117 | 118 | func RegisterNotifierServer(s *grpc.Server, srv NotifierServer) { 119 | s.RegisterService(&_Notifier_serviceDesc, srv) 120 | } 121 | 122 | func _Notifier_Email_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { 123 | in := new(EmailRequest) 124 | if err := dec(in); err != nil { 125 | return nil, err 126 | } 127 | if interceptor == nil { 128 | return srv.(NotifierServer).Email(ctx, in) 129 | } 130 | info := &grpc.UnaryServerInfo{ 131 | Server: srv, 132 | FullMethod: "/notifier.Notifier/Email", 133 | } 134 | handler := func(ctx context.Context, req interface{}) (interface{}, error) { 135 | return srv.(NotifierServer).Email(ctx, req.(*EmailRequest)) 136 | } 137 | return interceptor(ctx, in, info, handler) 138 | } 139 | 140 | var _Notifier_serviceDesc = grpc.ServiceDesc{ 141 | ServiceName: "notifier.Notifier", 142 | HandlerType: (*NotifierServer)(nil), 143 | Methods: []grpc.MethodDesc{ 144 | { 145 | MethodName: "Email", 146 | Handler: _Notifier_Email_Handler, 147 | }, 148 | }, 149 | Streams: []grpc.StreamDesc{}, 150 | Metadata: "proto/notifier/service.proto", 151 | } 152 | 153 | func init() { proto.RegisterFile("proto/notifier/service.proto", fileDescriptor0) } 154 | 155 | var fileDescriptor0 = []byte{ 156 | // 165 bytes of a gzipped FileDescriptorProto 157 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x29, 0x28, 0xca, 0x2f, 158 | 0xc9, 0xd7, 0xcf, 0xcb, 0x2f, 0xc9, 0x4c, 0xcb, 0x4c, 0x2d, 0xd2, 0x2f, 0x4e, 0x2d, 0x2a, 0xcb, 159 | 0x4c, 0x4e, 0xd5, 0x03, 0x0b, 0x0b, 0x71, 0xc0, 0xc4, 0x95, 0x54, 0xb8, 0x78, 0x5c, 0x73, 0x13, 160 | 0x33, 0x73, 0x82, 0x52, 0x0b, 0x4b, 0x53, 0x8b, 0x4b, 0x84, 0x44, 0xb8, 0x58, 0x53, 0x41, 0x7c, 161 | 0x09, 0x46, 0x05, 0x46, 0x0d, 0xce, 0x20, 0x08, 0x47, 0xc9, 0x96, 0x8b, 0x17, 0xaa, 0xaa, 0xb8, 162 | 0x20, 0x3f, 0xaf, 0x38, 0x55, 0x48, 0x82, 0x8b, 0x3d, 0x37, 0xb5, 0xb8, 0x38, 0x31, 0x3d, 0x15, 163 | 0xaa, 0x10, 0xc6, 0x15, 0x12, 0xe2, 0x62, 0x49, 0xce, 0x4f, 0x49, 0x95, 0x60, 0x52, 0x60, 0xd4, 164 | 0x60, 0x09, 0x02, 0xb3, 0x8d, 0xdc, 0xb8, 0x38, 0xfc, 0xa0, 0x16, 0x0a, 0x59, 0x71, 0xb1, 0x82, 165 | 0x8d, 0x12, 0x12, 0xd3, 0x83, 0x39, 0x42, 0x0f, 0xd9, 0x05, 0x52, 0xe2, 0x18, 0xe2, 0x10, 0x3b, 166 | 0x95, 0x18, 0x92, 0xd8, 0xc0, 0xae, 0x37, 0x06, 0x04, 0x00, 0x00, 0xff, 0xff, 0x7d, 0xe0, 0xe4, 167 | 0xb6, 0xdd, 0x00, 0x00, 0x00, 168 | } 169 | -------------------------------------------------------------------------------- /proto/notifier/service.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package notifier; 4 | 5 | service Notifier { 6 | rpc Email(EmailRequest) returns (EmailResponse) {} 7 | } 8 | 9 | message EmailRequest { 10 | string email = 1; 11 | } 12 | 13 | message EmailResponse { 14 | string message = 1; 15 | uint64 code = 2; 16 | } 17 | -------------------------------------------------------------------------------- /proto/users/service.pb.go: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-go. DO NOT EDIT. 2 | // source: proto/users/service.proto 3 | 4 | /* 5 | Package users is a generated protocol buffer package. 6 | 7 | It is generated from these files: 8 | proto/users/service.proto 9 | 10 | It has these top-level messages: 11 | CreateUserRequest 12 | CreateUserResponse 13 | */ 14 | package users 15 | 16 | import proto "github.com/golang/protobuf/proto" 17 | import fmt "fmt" 18 | import math "math" 19 | 20 | import ( 21 | context "golang.org/x/net/context" 22 | grpc "google.golang.org/grpc" 23 | ) 24 | 25 | // Reference imports to suppress errors if they are not otherwise used. 26 | var _ = proto.Marshal 27 | var _ = fmt.Errorf 28 | var _ = math.Inf 29 | 30 | // This is a compile-time assertion to ensure that this generated file 31 | // is compatible with the proto package it is being compiled against. 32 | // A compilation error at this line likely means your copy of the 33 | // proto package needs to be updated. 34 | const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package 35 | 36 | type CreateUserRequest struct { 37 | Email string `protobuf:"bytes,1,opt,name=email" json:"email,omitempty"` 38 | } 39 | 40 | func (m *CreateUserRequest) Reset() { *m = CreateUserRequest{} } 41 | func (m *CreateUserRequest) String() string { return proto.CompactTextString(m) } 42 | func (*CreateUserRequest) ProtoMessage() {} 43 | func (*CreateUserRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } 44 | 45 | func (m *CreateUserRequest) GetEmail() string { 46 | if m != nil { 47 | return m.Email 48 | } 49 | return "" 50 | } 51 | 52 | type CreateUserResponse struct { 53 | Message string `protobuf:"bytes,1,opt,name=message" json:"message,omitempty"` 54 | Code uint64 `protobuf:"varint,2,opt,name=code" json:"code,omitempty"` 55 | Id string `protobuf:"bytes,3,opt,name=id" json:"id,omitempty"` 56 | } 57 | 58 | func (m *CreateUserResponse) Reset() { *m = CreateUserResponse{} } 59 | func (m *CreateUserResponse) String() string { return proto.CompactTextString(m) } 60 | func (*CreateUserResponse) ProtoMessage() {} 61 | func (*CreateUserResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} } 62 | 63 | func (m *CreateUserResponse) GetMessage() string { 64 | if m != nil { 65 | return m.Message 66 | } 67 | return "" 68 | } 69 | 70 | func (m *CreateUserResponse) GetCode() uint64 { 71 | if m != nil { 72 | return m.Code 73 | } 74 | return 0 75 | } 76 | 77 | func (m *CreateUserResponse) GetId() string { 78 | if m != nil { 79 | return m.Id 80 | } 81 | return "" 82 | } 83 | 84 | func init() { 85 | proto.RegisterType((*CreateUserRequest)(nil), "users.CreateUserRequest") 86 | proto.RegisterType((*CreateUserResponse)(nil), "users.CreateUserResponse") 87 | } 88 | 89 | // Reference imports to suppress errors if they are not otherwise used. 90 | var _ context.Context 91 | var _ grpc.ClientConn 92 | 93 | // This is a compile-time assertion to ensure that this generated file 94 | // is compatible with the grpc package it is being compiled against. 95 | const _ = grpc.SupportPackageIsVersion4 96 | 97 | // Client API for Users service 98 | 99 | type UsersClient interface { 100 | CreateUser(ctx context.Context, in *CreateUserRequest, opts ...grpc.CallOption) (*CreateUserResponse, error) 101 | } 102 | 103 | type usersClient struct { 104 | cc *grpc.ClientConn 105 | } 106 | 107 | func NewUsersClient(cc *grpc.ClientConn) UsersClient { 108 | return &usersClient{cc} 109 | } 110 | 111 | func (c *usersClient) CreateUser(ctx context.Context, in *CreateUserRequest, opts ...grpc.CallOption) (*CreateUserResponse, error) { 112 | out := new(CreateUserResponse) 113 | err := grpc.Invoke(ctx, "/users.Users/CreateUser", in, out, c.cc, opts...) 114 | if err != nil { 115 | return nil, err 116 | } 117 | return out, nil 118 | } 119 | 120 | // Server API for Users service 121 | 122 | type UsersServer interface { 123 | CreateUser(context.Context, *CreateUserRequest) (*CreateUserResponse, error) 124 | } 125 | 126 | func RegisterUsersServer(s *grpc.Server, srv UsersServer) { 127 | s.RegisterService(&_Users_serviceDesc, srv) 128 | } 129 | 130 | func _Users_CreateUser_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { 131 | in := new(CreateUserRequest) 132 | if err := dec(in); err != nil { 133 | return nil, err 134 | } 135 | if interceptor == nil { 136 | return srv.(UsersServer).CreateUser(ctx, in) 137 | } 138 | info := &grpc.UnaryServerInfo{ 139 | Server: srv, 140 | FullMethod: "/users.Users/CreateUser", 141 | } 142 | handler := func(ctx context.Context, req interface{}) (interface{}, error) { 143 | return srv.(UsersServer).CreateUser(ctx, req.(*CreateUserRequest)) 144 | } 145 | return interceptor(ctx, in, info, handler) 146 | } 147 | 148 | var _Users_serviceDesc = grpc.ServiceDesc{ 149 | ServiceName: "users.Users", 150 | HandlerType: (*UsersServer)(nil), 151 | Methods: []grpc.MethodDesc{ 152 | { 153 | MethodName: "CreateUser", 154 | Handler: _Users_CreateUser_Handler, 155 | }, 156 | }, 157 | Streams: []grpc.StreamDesc{}, 158 | Metadata: "proto/users/service.proto", 159 | } 160 | 161 | func init() { proto.RegisterFile("proto/users/service.proto", fileDescriptor0) } 162 | 163 | var fileDescriptor0 = []byte{ 164 | // 181 bytes of a gzipped FileDescriptorProto 165 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x8f, 0x41, 0xab, 0x82, 0x40, 166 | 0x14, 0x85, 0x9f, 0x3e, 0x7d, 0x8f, 0xee, 0x22, 0xe8, 0xd2, 0x62, 0x6c, 0x25, 0xae, 0x6c, 0xa3, 167 | 0x50, 0x3f, 0xc1, 0x6d, 0xab, 0x81, 0x7e, 0x80, 0xe9, 0x21, 0x06, 0xb2, 0xb1, 0xb9, 0xda, 0xef, 168 | 0x8f, 0xc6, 0xa2, 0xa0, 0x76, 0x73, 0xce, 0x7c, 0x1c, 0xbe, 0x4b, 0x49, 0xef, 0xec, 0x60, 0xcb, 169 | 0x51, 0xe0, 0xa4, 0x14, 0xb8, 0xab, 0x69, 0x50, 0xf8, 0x8e, 0x63, 0x5f, 0x66, 0x6b, 0x5a, 0x54, 170 | 0x0e, 0xf5, 0x80, 0xbd, 0xc0, 0x69, 0x5c, 0x46, 0xc8, 0xc0, 0x4b, 0x8a, 0xd1, 0xd5, 0xe6, 0xa4, 171 | 0x82, 0x34, 0xc8, 0x67, 0x7a, 0x0a, 0x99, 0x26, 0x7e, 0x47, 0xa5, 0xb7, 0x67, 0x01, 0x2b, 0xfa, 172 | 0xef, 0x20, 0x52, 0x1f, 0xf1, 0xa0, 0x9f, 0x91, 0x99, 0xa2, 0xc6, 0xb6, 0x50, 0x61, 0x1a, 0xe4, 173 | 0x91, 0xf6, 0x6f, 0x9e, 0x53, 0x68, 0x5a, 0xf5, 0xeb, 0xc1, 0xd0, 0xb4, 0x9b, 0x1d, 0xc5, 0xf7, 174 | 0x35, 0xe1, 0x8a, 0xe8, 0x35, 0xce, 0xaa, 0xf0, 0x76, 0xc5, 0x87, 0xda, 0x2a, 0xf9, 0xf2, 0x33, 175 | 0x99, 0x64, 0x3f, 0x87, 0x3f, 0x7f, 0xda, 0xf6, 0x16, 0x00, 0x00, 0xff, 0xff, 0x71, 0xd8, 0xbd, 176 | 0xce, 0xf7, 0x00, 0x00, 0x00, 177 | } 178 | -------------------------------------------------------------------------------- /proto/users/service.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package users; 4 | 5 | service Users { 6 | rpc CreateUser(CreateUserRequest) returns (CreateUserResponse) {} 7 | } 8 | 9 | message CreateUserRequest { 10 | string email = 1; 11 | } 12 | 13 | message CreateUserResponse { 14 | string message = 1; 15 | uint64 code = 2; 16 | string id = 3; 17 | } 18 | -------------------------------------------------------------------------------- /users/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.9-alpine 2 | 3 | ENV SRV_NAME users 4 | 5 | ENV PKG_PATH /go/src/github.com/wizelineacademy/GoWorkshop 6 | 7 | ADD proto $PKG_PATH/proto 8 | ADD $SRV_NAME $PKG_PATH/$SRV_NAME 9 | WORKDIR $PKG_PATH/$SRV_NAME 10 | RUN apk update && apk upgrade && \ 11 | apk add --no-cache bash git openssh 12 | RUN go get google.golang.org/grpc golang.org/x/net/context gopkg.in/mgo.v2 github.com/golang/protobuf/proto github.com/sirupsen/logrus 13 | 14 | RUN go install 15 | EXPOSE 8080 16 | -------------------------------------------------------------------------------- /users/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net" 5 | 6 | log "github.com/sirupsen/logrus" 7 | "github.com/wizelineacademy/GoWorkshop/proto/users" 8 | "github.com/wizelineacademy/GoWorkshop/users/server" 9 | "google.golang.org/grpc" 10 | ) 11 | 12 | func main() { 13 | listener, err := net.Listen("tcp", ":8080") 14 | if err != nil { 15 | log.WithError(err).Error("could not start service") 16 | return 17 | } 18 | 19 | log.Info("starting service on :8080") 20 | 21 | srv := grpc.NewServer() 22 | users.RegisterUsersServer(srv, &server.Server{}) 23 | srv.Serve(listener) 24 | } 25 | -------------------------------------------------------------------------------- /users/models/models.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | import ( 4 | "os" 5 | "time" 6 | 7 | log "github.com/sirupsen/logrus" 8 | mgo "gopkg.in/mgo.v2" 9 | "gopkg.in/mgo.v2/bson" 10 | ) 11 | 12 | type ( 13 | // User type 14 | User struct { 15 | Id bson.ObjectId `bson:"_id,omitempty" json:"id"` 16 | Email string `json:"email"` 17 | } 18 | // UserRepository type 19 | UserRepository struct { 20 | C *mgo.Collection 21 | } 22 | ) 23 | 24 | var ( 25 | mongoSession *mgo.Session 26 | ) 27 | 28 | // CreateUser func 29 | func CreateUser(email string) (string, error) { 30 | c := getSession().Copy().DB("todo").C("users") 31 | 32 | repo := &UserRepository{c} 33 | return repo.Create(&User{ 34 | Email: email, 35 | }) 36 | } 37 | 38 | // Create user 39 | func (r *UserRepository) Create(user *User) (string, error) { 40 | objID := bson.NewObjectId() 41 | user.Id = objID 42 | err := r.C.Insert(&user) 43 | return user.Id.Hex(), err 44 | } 45 | 46 | func getSession() *mgo.Session { 47 | if mongoSession == nil { 48 | var err error 49 | mongoSession, err = mgo.DialWithInfo(&mgo.DialInfo{ 50 | Addrs: []string{os.Getenv("MONGO_HOST")}, 51 | Username: "", 52 | Password: "", 53 | Timeout: 60 * time.Second, 54 | }) 55 | if err != nil { 56 | log.WithError(err).Fatal("could not connect to mongo") 57 | } 58 | } 59 | return mongoSession 60 | } 61 | -------------------------------------------------------------------------------- /users/server/server.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "net/http" 5 | "os" 6 | 7 | log "github.com/sirupsen/logrus" 8 | "github.com/wizelineacademy/GoWorkshop/proto/list" 9 | "github.com/wizelineacademy/GoWorkshop/proto/notifier" 10 | "github.com/wizelineacademy/GoWorkshop/proto/users" 11 | "github.com/wizelineacademy/GoWorkshop/users/models" 12 | "golang.org/x/net/context" 13 | "google.golang.org/grpc" 14 | ) 15 | 16 | type Server struct{} 17 | 18 | func (s *Server) CreateUser(ctx context.Context, in *users.CreateUserRequest) (*users.CreateUserResponse, error) { 19 | userID, err := models.CreateUser(in.Email) 20 | 21 | response := new(users.CreateUserResponse) 22 | if err == nil { 23 | log.WithField("id", userID).Info("user created") 24 | 25 | createInitialItem(userID) 26 | go notify(in.Email) 27 | 28 | response.Message = "User created successfully" 29 | response.Id = userID 30 | response.Code = http.StatusCreated 31 | } else { 32 | log.WithError(err).Error("unable to create user") 33 | 34 | response.Message = err.Error() 35 | response.Code = http.StatusInternalServerError 36 | } 37 | 38 | return response, err 39 | } 40 | 41 | // Call notifier service 42 | func notify(email string) { 43 | conn, err := grpc.Dial(os.Getenv("SRV_NOTIFIER_ADDR"), grpc.WithInsecure()) 44 | if err != nil { 45 | log.WithError(err).Error("cannot dial notifier service") 46 | return 47 | } 48 | 49 | _, err = notifier.NewNotifierClient(conn).Email(context.Background(), ¬ifier.EmailRequest{ 50 | Email: email, 51 | }) 52 | 53 | if err != nil { 54 | log.WithError(err).Error("unable to notifier user") 55 | } else { 56 | log.WithField("email", email).Error("user notified") 57 | } 58 | } 59 | 60 | // Create initial item in todo list 61 | func createInitialItem(userID string) { 62 | conn, err := grpc.Dial(os.Getenv("SRV_LIST_ADDR"), grpc.WithInsecure()) 63 | if err != nil { 64 | log.WithError(err).Error("cannot dial list service") 65 | return 66 | } 67 | 68 | if _, err = list.NewListClient(conn).CreateItem(context.Background(), &list.CreateItemRequest{ 69 | Message: "Welcome to Workshop!", 70 | UserId: userID, 71 | }); err != nil { 72 | log.WithError(err).Error("unable to create initial item") 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /web/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.9-alpine 2 | 3 | ENV PKG_PATH /go/src/github.com/wizelineacademy/GoWorkshop 4 | 5 | ADD proto $PKG_PATH/proto 6 | ADD web $PKG_PATH/web 7 | WORKDIR $PKG_PATH/web 8 | RUN apk update && apk upgrade && \ 9 | apk add --no-cache bash git openssh 10 | 11 | RUN go get github.com/gocraft/web github.com/gorilla/context google.golang.org/grpc golang.org/x/net/context github.com/sirupsen/logrus 12 | 13 | RUN go install 14 | 15 | ENTRYPOINT /go/bin/web 16 | EXPOSE 8080 17 | -------------------------------------------------------------------------------- /web/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/wizelineacademy/GoWorkshop/web/pkg/web" 5 | ) 6 | 7 | func main() { 8 | web.ListenAndServe() 9 | } 10 | -------------------------------------------------------------------------------- /web/pkg/tpl/tpl.go: -------------------------------------------------------------------------------- 1 | package tpl 2 | 3 | import ( 4 | "errors" 5 | "html/template" 6 | 7 | "github.com/gocraft/web" 8 | ) 9 | 10 | // Data struct 11 | type Data struct { 12 | TemplateFile string 13 | Data interface{} 14 | } 15 | 16 | // Render func 17 | func (s Data) Render(w web.ResponseWriter, r *web.Request) error { 18 | if s.TemplateFile == "" { 19 | tplErr := errors.New("tpl.Render requires non-empty TemplateFile") 20 | return tplErr 21 | } 22 | 23 | t := template.Must(template.ParseFiles("templates/"+s.TemplateFile, "templates/layout.html")) 24 | err := t.ExecuteTemplate(w, "base", s) 25 | 26 | return err 27 | } 28 | -------------------------------------------------------------------------------- /web/pkg/web/handlers.go: -------------------------------------------------------------------------------- 1 | package web 2 | 3 | import ( 4 | "net/http" 5 | "os" 6 | 7 | "google.golang.org/grpc" 8 | 9 | "github.com/gocraft/web" 10 | "github.com/gorilla/context" 11 | 12 | log "github.com/sirupsen/logrus" 13 | pbList "github.com/wizelineacademy/GoWorkshop/proto/list" 14 | pbUsers "github.com/wizelineacademy/GoWorkshop/proto/users" 15 | ) 16 | 17 | // Context struct 18 | type Context struct { 19 | UsersService pbUsers.UsersClient 20 | ListService pbList.ListClient 21 | } 22 | 23 | // ListenAndServe func 24 | func ListenAndServe() { 25 | connUsers, errUsers := grpc.Dial(os.Getenv("SRV_USERS_ADDR"), grpc.WithInsecure()) 26 | if errUsers != nil { 27 | log.WithError(errUsers).Fatal("could not connect to users service") 28 | } 29 | 30 | connList, errList := grpc.Dial(os.Getenv("SRV_LIST_ADDR"), grpc.WithInsecure()) 31 | if errList != nil { 32 | log.WithError(errList).Fatal("could not connect to list service") 33 | } 34 | 35 | ctx := new(Context) 36 | ctx.UsersService = pbUsers.NewUsersClient(connUsers) 37 | ctx.ListService = pbList.NewListClient(connList) 38 | 39 | r := web.New(Context{}). 40 | Get("/", ctx.home). 41 | Post("/", ctx.createUser). 42 | Get("/user/:id", ctx.user). 43 | Post("/user/:id", ctx.user) 44 | 45 | serveMux := http.NewServeMux() 46 | serveMux.Handle("/", r) 47 | 48 | log.Info("web service started") 49 | 50 | http.ListenAndServe(":8080", context.ClearHandler(serveMux)) 51 | } 52 | -------------------------------------------------------------------------------- /web/pkg/web/pages.go: -------------------------------------------------------------------------------- 1 | package web 2 | 3 | import ( 4 | "context" 5 | "net/http" 6 | 7 | "github.com/wizelineacademy/GoWorkshop/web/pkg/tpl" 8 | 9 | "github.com/gocraft/web" 10 | pbList "github.com/wizelineacademy/GoWorkshop/proto/list" 11 | pbUsers "github.com/wizelineacademy/GoWorkshop/proto/users" 12 | ) 13 | 14 | func (c *Context) home(w web.ResponseWriter, r *web.Request) { 15 | d := tpl.Data{ 16 | TemplateFile: "home.html", 17 | Data: struct { 18 | Error string 19 | }{}, 20 | } 21 | 22 | d.Render(w, r) 23 | } 24 | 25 | func (c *Context) user(w web.ResponseWriter, r *web.Request) { 26 | var errorMsg string 27 | 28 | id := r.PathParams["id"] 29 | deleteItemID := r.FormValue("delete_id") 30 | itemMessage := r.FormValue("item_message") 31 | 32 | if len(deleteItemID) > 0 { 33 | _, err := c.ListService.DeleteItem(context.Background(), &pbList.DeleteItemRequest{ 34 | Id: deleteItemID, 35 | }) 36 | if err != nil { 37 | errorMsg = err.Error() 38 | } 39 | } 40 | 41 | if len(itemMessage) > 0 { 42 | _, err := c.ListService.CreateItem(context.Background(), &pbList.CreateItemRequest{ 43 | Message: itemMessage, 44 | UserId: id, 45 | }) 46 | if err != nil { 47 | errorMsg = err.Error() 48 | } 49 | } 50 | 51 | // gRPC call 52 | resp, err := c.ListService.GetUserItems(context.Background(), &pbList.GetUserItemsRequest{ 53 | UserId: id, 54 | }) 55 | 56 | if err != nil && len(errorMsg) == 0 { 57 | errorMsg = err.Error() 58 | } 59 | 60 | d := tpl.Data{ 61 | TemplateFile: "user.html", 62 | Data: struct { 63 | ID string 64 | Error string 65 | Resp *pbList.GetUserItemsResponse 66 | }{ 67 | ID: id, 68 | Error: errorMsg, 69 | Resp: resp, 70 | }, 71 | } 72 | 73 | d.Render(w, r) 74 | } 75 | 76 | func (c *Context) createUser(w web.ResponseWriter, r *web.Request) { 77 | email := r.FormValue("email") 78 | 79 | // gRPC call 80 | resp, err := c.UsersService.CreateUser(context.Background(), &pbUsers.CreateUserRequest{ 81 | Email: email, 82 | }) 83 | if err == nil && resp != nil && resp.Code == http.StatusCreated { 84 | http.Redirect(w, r.Request, "/user/"+resp.GetId(), http.StatusFound) 85 | return 86 | } 87 | 88 | var errorMsg string 89 | if err != nil { 90 | errorMsg = err.Error() 91 | } else if resp != nil { 92 | errorMsg = resp.Message 93 | } 94 | 95 | d := tpl.Data{ 96 | TemplateFile: "pages/home.html", 97 | Data: struct { 98 | Error string 99 | }{ 100 | Error: errorMsg, 101 | }, 102 | } 103 | 104 | d.Render(w, r) 105 | } 106 | -------------------------------------------------------------------------------- /web/templates/home.html: -------------------------------------------------------------------------------- 1 | {{define "body"}} 2 |