Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microservices #34

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
all: lint
all: build

name := shop

build:
@echo "------------------"
@echo "Building app... "
@echo "------------------"
go build cmd/shop/shop.go
go build -o $(name) ./cmd/shop/shop.go

swag:
@echo "------------------"
Expand All @@ -18,11 +20,8 @@ lint:
@echo "------------------"
golangci-lint run ./...

jaeger:
docker run -dp 6831:6831/udp -p 16686:16686 jaegertracing/all-in-one:latest

clear:
rm shop *.out
rm $(name) *.out

clean:
go clean -testcache
Expand Down
80 changes: 80 additions & 0 deletions cmd/inventory/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package main

import (
"context"
"errors"
"fmt"
"log"
"net"

"google.golang.org/grpc"

pb "github.com/Pavel7004/WebShop/pkg/pb/inventory"
)

type InventoryServer struct {
pb.UnimplementedInventoryServiceServer

data map[string]int32
}

func (s *InventoryServer) ReserveItems(
ctx context.Context,
in *pb.ReserveItemsRequest,
) (*pb.ReserveItemsResponse, error) {
for _, el := range in.GetItems() {
if kol, ok := s.data[el.GetItemId()]; ok {
if kol >= el.GetQuantity() {
s.data[el.GetItemId()] -= el.GetQuantity()
fmt.Printf("Item %s - %d\n", el.GetItemId(), s.data[el.GetItemId()])
} else {
return &pb.ReserveItemsResponse{
Success: false,
}, errors.New("Not enough items")
}
}
}

return &pb.ReserveItemsResponse{
Success: true,
}, nil
}

func (s *InventoryServer) CancelReserve(
ctx context.Context,
in *pb.CancelReserveRequest,
) (*pb.CancelReserveResponse, error) {

for _, el := range in.GetItems() {
if _, ok := s.data[el.GetItemId()]; ok {
s.data[el.GetItemId()] += el.GetQuantity()
fmt.Printf("Item %s - %d\n", el.GetItemId(), s.data[el.GetItemId()])
}
}

return &pb.CancelReserveResponse{
Success: true,
}, nil
}

func main() {
lis, err := net.Listen("tcp", ":50051")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
defer lis.Close()

s := grpc.NewServer()
invServer := &InventoryServer{
data: map[string]int32{
"1111": 10,
},
}

pb.RegisterInventoryServiceServer(s, invServer)

log.Println("Server is running on port :50051")
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
51 changes: 51 additions & 0 deletions cmd/order/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"context"
"log"
"time"

"google.golang.org/grpc"

pb "github.com/Pavel7004/WebShop/pkg/pb/inventory"
)

func main() {
conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewInventoryServiceClient(conn)

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

// Вызов ReserveItems
resReq := &pb.ReserveItemsRequest{
Items: []*pb.InventoryItem{
{
ItemId: "1111",
Quantity: 22,
},
},
}
res, err := c.ReserveItems(ctx, resReq)
if err != nil {
log.Fatalf("could not reserve items: %v", err)
}
log.Printf("ReserveItems Response: %v", res.Success)

// Вызов CancelReserve
canReq := &pb.CancelReserveRequest{
Items: []*pb.InventoryItem{{
ItemId: "1111",
Quantity: 2,
}},
}
canRes, err := c.CancelReserve(ctx, canReq)
if err != nil {
log.Fatalf("could not cancel reservation: %v", err)
}
log.Printf("CancelReserve Response: %v", canRes.Success)
}
22 changes: 19 additions & 3 deletions cmd/shop/shop.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright © 2024 Kovalev Pavel

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main

import (
Expand All @@ -18,14 +34,14 @@ import (
)

func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout})

closer := initTracing()
defer closer.Close()

log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout})

cfg, err := config.Get()
if err != nil {
log.Error().Err(err).Msg("Failed to read config")
log.Error().Err(err).Msg("Failed to get config")
return
}

Expand Down
70 changes: 65 additions & 5 deletions docs/docs.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// Code generated by swaggo/swag. DO NOT EDIT.

// Package docs Code generated by swaggo/swag. DO NOT EDIT
package docs

import (
"github.com/swaggo/swag"
)
import "github.com/swaggo/swag"

const docTemplate = `{
"schemes": {{ marshal .Schemes }},
Expand All @@ -25,6 +22,58 @@ const docTemplate = `{
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/login": {
"post": {
"description": "Login user",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Users"
],
"summary": "Login user",
"parameters": [
{
"description": "Request to login user",
"name": "req",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/domain.LoginUserRequest"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/domain.Error"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/domain.Error"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/domain.Error"
}
}
}
}
},
"/shop/v1/items": {
"get": {
"description": "Get items with specified price range",
Expand Down Expand Up @@ -589,6 +638,17 @@ const docTemplate = `{
}
}
},
"domain.LoginUserRequest": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"password": {
"type": "string"
}
}
},
"domain.OrderItem": {
"type": "object",
"properties": {
Expand Down
63 changes: 63 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,58 @@
},
"host": "localhost:8080",
"paths": {
"/login": {
"post": {
"description": "Login user",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Users"
],
"summary": "Login user",
"parameters": [
{
"description": "Request to login user",
"name": "req",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/domain.LoginUserRequest"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/domain.Error"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/domain.Error"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/domain.Error"
}
}
}
}
},
"/shop/v1/items": {
"get": {
"description": "Get items with specified price range",
Expand Down Expand Up @@ -579,6 +631,17 @@
}
}
},
"domain.LoginUserRequest": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"password": {
"type": "string"
}
}
},
"domain.OrderItem": {
"type": "object",
"properties": {
Expand Down
Loading
Loading