Skip to content

Commit 18bfc46

Browse files
Merge pull request #5 from jeferagudeloc/develop
[develop]: add debugger and server grpc users + debug dockerfile
2 parents 8e19d6e + 53df095 commit 18bfc46

File tree

17 files changed

+335
-147
lines changed

17 files changed

+335
-147
lines changed

config/nginx/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM nginx:latest
2+
COPY nginx.conf /etc/nginx/nginx.conf

config/nginx/nginx.conf

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
worker_processes 1;
2+
3+
events {
4+
worker_connections 1024;
5+
}
6+
7+
http {
8+
upstream gateway {
9+
server gateway:8080;
10+
}
11+
12+
server {
13+
listen 80;
14+
15+
location / {
16+
if ($request_method = 'OPTIONS') {
17+
add_header 'Access-Control-Allow-Origin' '*';
18+
add_header 'Access-Control-Allow-Methods' 'POST, GET, OPTIONS';
19+
add_header 'Access-Control-Allow-Headers' 'Content-Type,x-endpoint-api-userinfo,x-commerce,x-country,authorization,x-environment';
20+
add_header 'Access-Control-Max-Age' 1728000;
21+
add_header 'Content-Type' 'text/plain; charset=utf-8';
22+
add_header 'Content-Length' 0;
23+
return 204;
24+
}
25+
26+
add_header 'Access-Control-Allow-Origin' '*';
27+
proxy_pass http://gateway;
28+
proxy_set_header Host $host;
29+
proxy_set_header X-Real-IP $remote_addr;
30+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
31+
}
32+
}
33+
}

docker-compose.yml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ services:
99
- ./src/ui:/app/
1010
ports:
1111
- 3000:3000
12-
networks:
13-
- bridge
1412
server:
1513
build:
1614
context: './src/server'
@@ -32,17 +30,29 @@ services:
3230
gateway:
3331
build:
3432
context: './src/gateway'
35-
dockerfile: 'Dockerfile'
33+
dockerfile: 'Dockerfile.debug'
3634
container_name: gateway
3735
ports:
38-
- 8080:3000
36+
- 8080:8080
37+
- 4000:4000
3938
depends_on:
4039
- server
4140
restart: on-failure
4241
volumes:
4342
- ./src/gateway:/usr/src/app/
4443
networks:
4544
- bridge
45+
nginx:
46+
image: nginx
47+
container_name: nginx
48+
ports:
49+
- 9080:80
50+
volumes:
51+
- ./config/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
52+
networks:
53+
- bridge
54+
depends_on:
55+
- gateway
4656
mysql:
4757
image: mysql
4858
command: --default-authentication-plugin=mysql_native_password

src/gateway/.vscode/launch.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
"mode": "auto",
1212
"program": "${workspaceFolder}/main.go",
1313
"args": [],
14+
},
15+
{
16+
"name": "Docker Debug",
17+
"type": "go",
18+
"request": "attach",
19+
"mode": "remote",
20+
"port": 4000,
21+
"host": "127.0.0.1"
1422
}
1523
]
1624
}

src/gateway/Dockerfile.debug

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM golang:1.18-alpine
2+
3+
EXPOSE 3000 4000
4+
5+
WORKDIR /app
6+
COPY go.mod go.sum ./
7+
RUN go mod download
8+
9+
COPY . .
10+
11+
RUN CGO_ENABLED=0 go install -ldflags "-s -w -extldflags '-static'" github.com/go-delve/delve/cmd/dlv@latest
12+
13+
RUN CGO_ENABLED=0 go build -gcflags "all=-N -l" -o go-debugger .
14+
15+
16+
CMD [ "/go/bin/dlv", "--listen=:4000", "--headless=true", "--log=true", "--accept-multiclient", "--api-version=2", "exec", "/app/go-debugger" ]

src/gateway/application/usecase/get_orders_grpc.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package usecase
22

33
import (
44
"context"
5+
"fmt"
56
"time"
67

78
"github.com/jeferagudeloc/grpc-http-gateway/src/gateway/application/adapter/logger"
89
"github.com/jeferagudeloc/grpc-http-gateway/src/gateway/domain"
10+
"github.com/jeferagudeloc/grpc-http-gateway/src/server/application/adapter/grpc/order"
911
"google.golang.org/grpc"
1012
)
1113

@@ -34,5 +36,31 @@ func NewGetOrdersGrpcInteractor(
3436
}
3537

3638
func (a GetOrdersGrpcInteractor) Execute(ctx context.Context) ([]domain.Order, error) {
37-
return nil, nil
39+
40+
output := make([]domain.Order, 0)
41+
orderClient := order.NewOrderHandlerClient(a.grpcClient)
42+
43+
a.logger.Infof("get orders calling grpc client")
44+
response, err := orderClient.GetOrders(ctx, &order.GetOrdersRequest{})
45+
46+
for _, o := range response.Orders {
47+
output = append(output, domain.Order{
48+
ID: o.Id,
49+
OrderType: o.OrderType,
50+
Store: o.Store,
51+
Address: o.Address,
52+
CreationDate: o.CreationDate,
53+
Status: o.Status,
54+
})
55+
}
56+
57+
fmt.Printf("output server: %v\n", len(output))
58+
59+
if err != nil {
60+
a.logger.Fatalln("Error when trying to say hello: %v", err)
61+
}
62+
63+
a.logger.Infof("response from server: %v", response)
64+
65+
return output, nil
3866
}

src/gateway/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ go 1.19
44

55
require (
66
github.com/gorilla/mux v1.8.0
7-
github.com/jeferagudeloc/grpc-http-gateway/src/server v0.0.0-20230521043111-a1f7c6b005ed
7+
github.com/jeferagudeloc/grpc-http-gateway/src/server v0.0.0-20230525065623-8e19d6e95f77
88
github.com/joho/godotenv v1.5.1
99
github.com/pkg/errors v0.9.1
10+
github.com/rs/cors v1.9.0
1011
github.com/sirupsen/logrus v1.9.2
1112
github.com/urfave/negroni v1.0.0
1213
google.golang.org/grpc v1.55.0

src/gateway/go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
88
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
99
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
1010
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
11-
github.com/jeferagudeloc/grpc-http-gateway/src/server v0.0.0-20230521043111-a1f7c6b005ed h1:gZIWLnFzR0Ufn/91UR97RiSLkn3P+ShwV/cZyovxjx8=
12-
github.com/jeferagudeloc/grpc-http-gateway/src/server v0.0.0-20230521043111-a1f7c6b005ed/go.mod h1:XWMEqHYMaxoHe0MxTcVhZO79qTaXAMgTrM7evPYGr4k=
11+
github.com/jeferagudeloc/grpc-http-gateway/src/server v0.0.0-20230525065623-8e19d6e95f77 h1:nnEt9HbOULOmDTPL6+RG32/x/RNUdvbIj2BtQPboBXE=
12+
github.com/jeferagudeloc/grpc-http-gateway/src/server v0.0.0-20230525065623-8e19d6e95f77/go.mod h1:nYtuBCS9GhxyB8vHEEeSWOujbyO6ZIHQId5zsab2ZRU=
1313
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
1414
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
1515
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
1616
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1717
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1818
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
19+
github.com/rs/cors v1.9.0 h1:l9HGsTsHJcvW14Nk7J9KFz8bzeAWXn3CG6bgt7LsrAE=
20+
github.com/rs/cors v1.9.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
1921
github.com/sirupsen/logrus v1.9.2 h1:oxx1eChJGI6Uks2ZC4W1zpLlVgqB8ner4EuQwV4Ik1Y=
2022
github.com/sirupsen/logrus v1.9.2/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
2123
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

src/gateway/infrastructure/router/gorilla_mux.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import (
1010
"time"
1111

1212
"github.com/gorilla/mux"
13+
"github.com/rs/cors"
1314
"google.golang.org/grpc"
1415
"google.golang.org/grpc/credentials/insecure"
1516

1617
"github.com/jeferagudeloc/grpc-http-gateway/src/gateway/application/adapter/api/action"
1718
"github.com/jeferagudeloc/grpc-http-gateway/src/gateway/application/adapter/api/middleware"
18-
"github.com/jeferagudeloc/grpc-http-gateway/src/gateway/application/usecase"
19-
2019
"github.com/jeferagudeloc/grpc-http-gateway/src/gateway/application/adapter/logger"
20+
"github.com/jeferagudeloc/grpc-http-gateway/src/gateway/application/usecase"
2121
"github.com/urfave/negroni"
2222
)
2323

@@ -44,6 +44,16 @@ func (g gorillaMux) Listen() {
4444
g.setAppHandlers(g.router)
4545
g.middleware.UseHandler(g.router)
4646

47+
// Enable CORS middleware
48+
c := cors.New(cors.Options{
49+
AllowedOrigins: []string{"*"},
50+
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
51+
AllowedHeaders: []string{"Content-Type", "Authorization"},
52+
})
53+
54+
// Add the CORS middleware to Negroni
55+
g.middleware.UseHandler(c.Handler(g.router))
56+
4757
server := &http.Server{
4858
ReadTimeout: 5 * time.Second,
4959
WriteTimeout: 15 * time.Second,

src/server/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
MYSQL_HOST=mysql
1+
MYSQL_HOST=localhost
22
MYSQL_DATABASE=http_gateway_grpc
33
MYSQL_PORT=3306
44
MYSQL_USER=root

0 commit comments

Comments
 (0)