Skip to content

Commit 725fb45

Browse files
committed
Merger http/grpc servers into one binary
1 parent 99d3631 commit 725fb45

File tree

11 files changed

+423
-343
lines changed

11 files changed

+423
-343
lines changed

Dockerfile

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ COPY . .
1717
RUN export VERSION=$(cat VERSION) && \
1818
go build -ldflags "-X github.com/opencars/vin-decoder-api/pkg/version.Version=$VERSION" -o /go/bin/server ./cmd/server/main.go
1919

20-
RUN export VERSION=$(cat VERSION) && \
21-
go build -ldflags "-X github.com/opencars/vin-decoder-api/pkg/version.Version=$VERSION" -o /go/bin/grpc-server ./cmd/grpc-server/main.go
22-
2320
FROM alpine
2421

2522
RUN apk update && apk upgrade && apk add curl
@@ -28,5 +25,5 @@ WORKDIR /app
2825

2926
COPY --from=build /go/bin/ ./
3027

31-
EXPOSE 8080
28+
EXPOSE 8080 3000
3229
CMD ["./server"]

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.PHONY: default all clean
2-
APPS := server grpc-server
2+
APPS := server
33
BLDDIR := bin
44
VERSION := $(shell cat VERSION)
55

cmd/grpc-server/main.go

Lines changed: 0 additions & 48 deletions
This file was deleted.

cmd/server/main.go

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99

1010
"github.com/opencars/schema/client"
1111
"github.com/opencars/seedwork/logger"
12+
"golang.org/x/sync/errgroup"
1213

14+
"github.com/opencars/vin-decoder-api/pkg/api/grpc"
1315
"github.com/opencars/vin-decoder-api/pkg/api/http"
1416
"github.com/opencars/vin-decoder-api/pkg/config"
1517
"github.com/opencars/vin-decoder-api/pkg/domain/service"
@@ -18,7 +20,8 @@ import (
1820

1921
func main() {
2022
cfg := flag.String("config", "config/config.yaml", "Path to the configuration file")
21-
port := flag.Int("port", 8080, "Port of the server")
23+
httpPort := flag.Int("http-port", 8080, "Port for HTTP server")
24+
grpcPort := flag.Int("grpc-port", 3000, "Port for gRPC server")
2225

2326
flag.Parse()
2427

@@ -29,6 +32,13 @@ func main() {
2932

3033
logger.NewLogger(logger.LogLevel(conf.Log.Level), conf.Log.Mode == "dev")
3134

35+
// Initialize store
36+
store, err := sqlstore.New(&conf.DB)
37+
if err != nil {
38+
logger.Fatalf("store: %v", err)
39+
}
40+
41+
// Initialize NATS client
3242
c, err := client.New(conf.NATS.Address())
3343
if err != nil {
3444
logger.Fatalf("nats: %v", err)
@@ -39,20 +49,36 @@ func main() {
3949
logger.Fatalf("producer: %v", err)
4050
}
4151

42-
store, err := sqlstore.New(&conf.DB)
43-
if err != nil {
44-
logger.Fatalf("store: %v", err)
45-
}
46-
47-
svc := service.NewCustomerService(store.Manufacturer(), producer)
48-
49-
addr := ":" + strconv.Itoa(*port)
52+
// Create services
53+
customerSvc := service.NewCustomerService(store.Manufacturer(), producer)
54+
internalSvc := service.NewInternalService(store.Manufacturer())
5055

56+
// Create context with cancellation
5157
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
5258
defer stop()
5359

54-
logger.Infof("Listening on %s...", addr)
55-
if err := http.Start(ctx, addr, &conf.Server, svc); err != nil {
56-
logger.Fatalf("http server failed: %v", err)
60+
// Create errgroup with cancellation
61+
g, ctx := errgroup.WithContext(ctx)
62+
63+
// Start HTTP server
64+
g.Go(func() error {
65+
addr := ":" + strconv.Itoa(*httpPort)
66+
logger.Infof("Starting HTTP server on %s...", addr)
67+
return http.Start(ctx, addr, &conf.Server, customerSvc)
68+
})
69+
70+
// Start gRPC server
71+
g.Go(func() error {
72+
addr := ":" + strconv.Itoa(*grpcPort)
73+
logger.Infof("Starting gRPC server on %s...", addr)
74+
api := grpc.New(addr, internalSvc)
75+
return api.Run(ctx)
76+
})
77+
78+
// Wait for interrupt signal or error from servers
79+
logger.Infof("Servers started successfully. Press Ctrl+C to stop...")
80+
if err := g.Wait(); err != nil {
81+
logger.Fatalf("Server error: %v", err)
5782
}
83+
logger.Infof("Servers stopped gracefully")
5884
}

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module github.com/opencars/vin-decoder-api
22

3-
go 1.19
3+
go 1.23.0
4+
5+
toolchain go1.23.2
46

57
require (
68
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
@@ -32,6 +34,7 @@ require (
3234
github.com/satori/go.uuid v1.2.0 // indirect
3335
golang.org/x/crypto v0.0.0-20220112180741-5e0467b6c7ce // indirect
3436
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
37+
golang.org/x/sync v0.12.0 // indirect
3538
golang.org/x/sys v0.0.0-20220111092808-5a964db01320 // indirect
3639
golang.org/x/text v0.3.6 // indirect
3740
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJ
277277
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
278278
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
279279
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
280+
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
281+
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
280282
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
281283
golang.org/x/sys v0.0.0-20190130150945-aca44879d564/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
282284
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

pkg/domain/model/types.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import (
44
"regexp"
55
)
66

7-
var (
8-
IsVIN = regexp.MustCompile(`^[A-HJ-NPR-Z0-9]{17}$`)
9-
)
7+
var IsVIN = regexp.MustCompile(`^[A-HJ-NPR-Z0-9]{17}$`)
108

119
// Result is a union of information about vin-code and decoded vehicle.
1210
type Result struct {
@@ -35,6 +33,7 @@ type VIN struct {
3533
type Vehicle struct {
3634
Manufacturer string `json:"manufacturer"`
3735
Country string `json:"country"`
36+
CountryUA string `json:"country_ua"`
3837
Year *uint `json:"year,omitempty"`
3938
Region Region `json:"region"`
4039
Check bool `json:"check"`

0 commit comments

Comments
 (0)