Skip to content

Commit b38f118

Browse files
committed
adding user service
1 parent 7b62af7 commit b38f118

27 files changed

+696
-39
lines changed

consignment-cli/Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM golang:1.9.0 as builder
22

3-
WORKDIR /go/src/github.com/rahul2393/rahul-microservice/consignment-cli
3+
WORKDIR /go/src/github.com/rahul-microservice/consignment-cli
44

55
COPY . .
66

@@ -15,7 +15,7 @@ RUN apk --no-cache add ca-certificates
1515
RUN mkdir /app
1616
WORKDIR /app
1717
COPY consignment.json /app/consignment.json
18-
COPY --from=builder /go/src/github.com/rahul2393/rahul-microservice/consignment-cli/consignment-cli .
18+
COPY --from=builder /go/src/github.com/rahul-microservice/consignment-cli/consignment-cli .
1919

2020
ENTRYPOINT ["./consignment-cli", "consignment.json"]
2121
CMD ["./consignment-cli"]

consignment-service/Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM golang:1.9.0 as builder
22

3-
WORKDIR /go/src/github.com/rahul2393/rahul-microservice/consignment-service
3+
WORKDIR /go/src/github.com/rahul-microservice/consignment-service
44

55
COPY . .
66

@@ -13,6 +13,6 @@ RUN apk --no-cache add ca-certificates
1313

1414
RUN mkdir /app
1515
WORKDIR /app
16-
COPY --from=builder /go/src/github.com/rahul2393/rahul-microservice/consignment-service/consignment-service .
16+
COPY --from=builder /go/src/github.com/rahul-microservice/consignment-service/consignment-service .
1717

1818
CMD ["./consignment-service"]

consignment-service/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
build:
2-
protoc -I. --go_out=plugins=micro:$(GOPATH)/src/github.com/rahul2393/rahul-microservice/consignment-service \
2+
protoc -I. --go_out=plugins=micro:$(GOPATH)/src/github.com/rahul-microservice/consignment-service \
33
proto/consignment/consignment.proto
44
GOOS=linux GOARCH=amd64
55
docker build -t consignment-service .

consignment-service/handler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
import (
44
"log"
55
"golang.org/x/net/context"
6-
pb "github.com/rahul2393/rahul-microservice/consignment-service/proto/consignment"
6+
pb "github.com/rahul-microservice/consignment-service/proto/consignment"
77
vesselProto "github.com/rahul2393/rahul-microservice/vessel-service/proto/vessel"
88
"gopkg.in/mgo.v2"
99
)

consignment-service/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"fmt"
77
"log"
88

9-
pb "github.com/rahul2393/rahul-microservice/consignment-service/proto/consignment"
9+
pb "github.com/rahul-microservice/consignment-service/proto/consignment"
1010
vesselProto "github.com/rahul2393/rahul-microservice/vessel-service/proto/vessel"
1111
"github.com/micro/go-micro"
1212
"os"
@@ -24,7 +24,6 @@ func main() {
2424
if host == "" {
2525
host = defaultHost
2626
}
27-
2827
session, err := CreateSession(host)
2928

3029
// Mgo creates a 'master' session, we need to end that session

consignment-service/repository.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
import (
4-
pb "github.com/rahul2393/rahul-microservice/consignment-service/proto/consignment"
4+
pb "github.com/rahul-microservice/consignment-service/proto/consignment"
55
"gopkg.in/mgo.v2"
66
)
77

docker-compose.yml

+23-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ version: '3.1'
33

44
services:
55

6+
datastore:
7+
image: mongo
8+
ports:
9+
- 27017:27017
10+
611
consignment-cli:
712
build: ./consignment-cli
813
environment:
@@ -23,4 +28,21 @@ services:
2328
- 50052:50051
2429
environment:
2530
MICRO_ADDRESS: ":50051"
26-
MICRO_REGISTRY: "mdns"
31+
MICRO_REGISTRY: "mdns"
32+
DB_HOST: "datastore:27017"
33+
34+
35+
user-service:
36+
build: ./user-service
37+
ports:
38+
- 50053:50051
39+
environment:
40+
MICRO_ADDRESS: ":50051"
41+
MICRO_REGISTRY: "mdns"
42+
DB_HOST: "postgres:5432"
43+
44+
postgres:
45+
image: postgres
46+
ports:
47+
-5432:5432
48+

user-cli/Dockerfile

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM golang:1.9.0 as builder
2+
3+
WORKDIR /go/src/github.com/rahul-microservice/user-cli
4+
5+
COPY . .
6+
7+
RUN go get
8+
RUN CGO_ENABLED=0 GOOS=linux go build -o user-cli -a -installsuffix cgo cli.go
9+
10+
11+
FROM alpine:latest
12+
13+
RUN apk --no-cache add ca-certificates
14+
15+
RUN mkdir /app
16+
WORKDIR /app
17+
COPY user.json /app/user.json
18+
COPY --from=builder /go/src/github.com/rahul-microservice/user-cli/user-cli .
19+
20+
ENTRYPOINT ["./user-cli", "user.json"]
21+
CMD ["./user-cli"]

user-cli/Makefile

Whitespace-only changes.

user-cli/cli.go

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"os"
6+
7+
pb "github.com/rahul2393/rahul-microservice/user-service/proto/user"
8+
microclient "github.com/micro/go-micro/client"
9+
"github.com/micro/go-micro/cmd"
10+
"golang.org/x/net/context"
11+
"github.com/micro/cli"
12+
"github.com/micro/go-micro"
13+
)
14+
15+
16+
func main() {
17+
18+
cmd.Init()
19+
20+
// Create new greeter client
21+
client := pb.NewUserServiceClient("go.micro.srv.user", microclient.DefaultClient)
22+
23+
// Define our flags
24+
service := micro.NewService(
25+
micro.Flags(
26+
cli.StringFlag{
27+
Name: "name",
28+
Usage: "You full name",
29+
},
30+
cli.StringFlag{
31+
Name: "email",
32+
Usage: "Your email",
33+
},
34+
cli.StringFlag{
35+
Name: "password",
36+
Usage: "Your password",
37+
},
38+
cli.StringFlag{
39+
Name: "company",
40+
Usage: "Your company",
41+
},
42+
),
43+
)
44+
45+
// Start as service
46+
service.Init(
47+
48+
micro.Action(func(c *cli.Context) {
49+
50+
name := c.String("name")
51+
email := c.String("email")
52+
password := c.String("password")
53+
company := c.String("company")
54+
55+
// Call our user service
56+
r, err := client.Create(context.TODO(), &pb.User{
57+
Name: name,
58+
Email: email,
59+
Password: password,
60+
Company: company,
61+
})
62+
if err != nil {
63+
log.Fatalf("Could not create: %v", err)
64+
}
65+
log.Printf("Created: %s", r.User.Id)
66+
67+
getAll, err := client.GetAll(context.Background(), &pb.Request{})
68+
if err != nil {
69+
log.Fatalf("Could not list users: %v", err)
70+
}
71+
for _, v := range getAll.Users {
72+
log.Println(v)
73+
}
74+
75+
os.Exit(0)
76+
}),
77+
)
78+
79+
// Run the server
80+
if err := service.Run(); err != nil {
81+
log.Println(err)
82+
}
83+
}

user-cli/user.json

Whitespace-only changes.

user-service/Dockerfile

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM golang:1.9.0 as builder
2+
3+
WORKDIR /go/src/github.com/rahul-microservice/user-service
4+
5+
COPY . .
6+
7+
RUN go get
8+
RUN CGO_ENABLED=0 GOOS=linux go build -o user-service -a -installsuffix cgo main.go repository.go handler.go database.go
9+
10+
FROM alpine:latest
11+
12+
RUN apk --no-cache add ca-certificates
13+
14+
RUN mkdir /app
15+
WORKDIR /app
16+
COPY --from=builder /go/src/github.com/rahul-microservice/user-service/user-service .
17+
18+
CMD ["./user-service"]

user-service/Makefile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
build:
2+
protoc -I. --go_out=plugins=micro:$(GOPATH)/src/github.com/rahul-microservice/user-service \
3+
proto/user/user.proto
4+
GOOS=linux GOARCH=amd64
5+
docker build -t user-service .
6+
7+
run:
8+
docker run -p 50053:50051 -e MICRO_SERVER_ADDRESS=:50051 -e MICRO_REGISTRY=mdns user-service

user-service/database.go

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package user_service

user-service/handler.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"golang.org/x/net/context"
5+
pb "github.com/rahul-microservice/user-service/proto/user"
6+
)
7+
8+
type service struct {
9+
repo Repository
10+
tokenService Authable
11+
}
12+
13+
func (srv *service) Get(ctx context.Context, req *pb.User, res *pb.Response) error {
14+
user, err := srv.repo.Get(req.Id)
15+
if err != nil {
16+
return err
17+
}
18+
res.User = user
19+
return nil
20+
}
21+
22+
func (srv *service) GetAll(ctx context.Context, req *pb.Request, res *pb.Response) error {
23+
users, err := srv.repo.GetAll()
24+
if err != nil {
25+
return err
26+
}
27+
res.Users = users
28+
return nil
29+
}
30+
31+
func (srv *service) Auth(ctx context.Context, req *pb.User, res *pb.Token) error {
32+
user, err := srv.repo.GetByEmailAndPassword(req)
33+
if err != nil {
34+
return err
35+
}
36+
res.Token = "testingabc"
37+
return nil
38+
}
39+
40+
func (srv *service) Create(ctx context.Context, req *pb.User, res *pb.Response) error {
41+
if err := srv.repo.Create(req); err != nil {
42+
return err
43+
}
44+
res.User = req
45+
return nil
46+
}
47+
48+
func (srv *service) ValidateToken(ctx context.Context, req *pb.Token, res *pb.Token) error {
49+
return nil
50+
}

user-service/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package user_service

user-service/proto/user/extensions.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package go_micro_srv_user
2+
3+
import (
4+
"github.com/jinzhu/gorm"
5+
uuid "github.com/satori/go.uuid"
6+
)
7+
8+
func (model *User) BeforeCreate(scope *gorm.Scope) error {
9+
uuid,_ := uuid.NewV4()
10+
return scope.SetColumn("Id", uuid.String())
11+
}

0 commit comments

Comments
 (0)