Skip to content

Commit 08eb2c0

Browse files
committed
Merge http/grpc servers into one binary
1 parent e34c83d commit 08eb2c0

7 files changed

Lines changed: 53 additions & 71 deletions

File tree

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
- name: Set up Go
1515
uses: actions/setup-go@v2
1616
with:
17-
go-version: 1.19
17+
go-version: 1.23
1818

1919
- name: Build
2020
run: "go build -v ./..."
@@ -40,7 +40,7 @@ jobs:
4040
- name: Set up Go 1.x
4141
uses: actions/setup-go@v2
4242
with:
43-
go-version: ^1.19
43+
go-version: ^1.23
4444
id: go
4545

4646
- name: Check out code into the Go module directory

Dockerfile

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1-
FROM golang:1.19-alpine AS build
1+
FROM golang:1.23-alpine AS build
22

33
ENV GO111MODULE=on
44

55
WORKDIR /go/src/app
66

7-
LABEL maintainer="ashanaakh@gmail.com"
8-
97
RUN apk add bash ca-certificates git gcc g++ libc-dev
108

119
COPY go.mod go.sum ./
1210

1311
RUN go mod download
1412

1513
COPY . .
16-
17-
RUN go build -o /go/bin/grpc-server ./cmd/grpc-server/main.go && \
18-
go build -o /go/bin/http-server ./cmd/http-server/main.go && \
14+
RUN go build -o /go/bin/server ./cmd/server/main.go && \
1915
go build -o /go/bin/parser ./cmd/parser/main.go
2016

2117
FROM alpine
@@ -29,4 +25,4 @@ COPY ./config ./config
2925

3026
EXPOSE 8080
3127

32-
CMD ["./http-server"]
28+
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 := http-server grpc-server parser
2+
APPS := server
33
BLDDIR := bin
44
IMPORT_BASE := github.com/opencars/koatuu
55

cmd/grpc-server/main.go

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

cmd/http-server/main.go

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import (
1010
_ "github.com/lib/pq"
1111
"github.com/opencars/schema/client"
1212
"github.com/opencars/seedwork/logger"
13+
"golang.org/x/sync/errgroup"
1314

15+
"github.com/opencars/koatuu/pkg/api/grpc"
1416
"github.com/opencars/koatuu/pkg/api/http"
1517
"github.com/opencars/koatuu/pkg/config"
1618
"github.com/opencars/koatuu/pkg/domain/service"
@@ -19,7 +21,8 @@ import (
1921

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

2427
flag.Parse()
2528

@@ -30,6 +33,13 @@ func main() {
3033

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

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

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

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

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

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/opencars/koatuu
22

3-
go 1.19
3+
go 1.23
44

55
require (
66
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
@@ -13,6 +13,7 @@ require (
1313
github.com/opencars/schema v0.0.12
1414
github.com/opencars/seedwork v0.0.2
1515
github.com/stretchr/testify v1.8.0
16+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
1617
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013
1718
google.golang.org/grpc v1.50.0
1819
google.golang.org/protobuf v1.28.1

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
22
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
33
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=
44
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d h1:Byv0BzEl3/e6D5CLfI0j/7hiIEtvGVFPCZ7Ei2oq8iQ=
5+
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
56
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
67
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
78
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
@@ -42,6 +43,7 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
4243
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
4344
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
4445
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
46+
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
4547
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
4648
github.com/gorilla/handlers v1.5.0 h1:4wjo3sf9azi99c8hTmyaxp9y5S+pFszsy3pP0rAw/lw=
4749
github.com/gorilla/handlers v1.5.0/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
@@ -50,13 +52,16 @@ github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7
5052
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
5153
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
5254
github.com/klauspost/compress v1.15.11 h1:Lcadnb3RKGin4FYM/orgq0qde+nc15E5Cbqg4B9Sx9c=
55+
github.com/klauspost/compress v1.15.11/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM=
5356
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
5457
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
5558
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
5659
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
5760
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
5861
github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA/g=
62+
github.com/minio/highwayhash v1.0.2/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY=
5963
github.com/nats-io/jwt/v2 v2.3.0 h1:z2mA1a7tIf5ShggOFlR1oBPgd6hGqcDYsISxZByUzdI=
64+
github.com/nats-io/jwt/v2 v2.3.0/go.mod h1:0tqz9Hlu6bCBFLWAASKhE5vUA4c24L9KPUUgvwumE/k=
6065
github.com/nats-io/nats-server/v2 v2.9.2 h1:XNDgJgOYYaYlquLdbSHI3xssLipfKUOq3EmYIMNCOsE=
6166
github.com/nats-io/nats-server/v2 v2.9.2/go.mod h1:4sq8wvrpbvSzL1n3ZfEYnH4qeUuIl5W990j3kw13rRk=
6267
github.com/nats-io/nats.go v1.17.0 h1:1jp5BThsdGlN91hW0k3YEfJbfACjiOYtUiLXG0RL4IE=
@@ -116,6 +121,7 @@ golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAG
116121
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
117122
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
118123
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
124+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
119125
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
120126
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
121127
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@@ -132,6 +138,7 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
132138
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
133139
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
134140
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af h1:Yx9k8YCG3dvF87UAn2tu2HQLf2dt/eR1bXxpLMWeH+Y=
141+
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
135142
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
136143
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
137144
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=

0 commit comments

Comments
 (0)