Skip to content

Commit d3e1065

Browse files
authored
Merge pull request #1 from cronnoss/develop
developed interaction between services
2 parents 74758c2 + e5e544f commit d3e1065

37 files changed

+4144
-0
lines changed

.github/workflows/ci.yml

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: BannersCI
2+
3+
on:
4+
push:
5+
branches:
6+
- "*"
7+
8+
env:
9+
GO111MODULE: "on"
10+
11+
jobs:
12+
lint:
13+
runs-on: ubuntu-latest
14+
steps:
15+
16+
- name: Set up Go
17+
uses: actions/setup-go@v3
18+
with:
19+
go-version: ^1.21
20+
21+
- name: Checkout code
22+
uses: actions/checkout@v3
23+
24+
- name: Linters
25+
uses: golangci/golangci-lint-action@v3
26+
with:
27+
version: latest
28+
working-directory: .
29+
30+
build:
31+
runs-on: ubuntu-latest
32+
steps:
33+
34+
- name: Set up Go
35+
uses: actions/setup-go@v3
36+
with:
37+
go-version: ^1.21
38+
39+
- name: Check out code
40+
uses: actions/checkout@v3
41+
42+
- name: Build service binary
43+
run: go build -o banner ./cmd/banner
44+
45+
tests:
46+
runs-on: ubuntu-latest
47+
steps:
48+
49+
- name: Set up Go
50+
uses: actions/setup-go@v3
51+
with:
52+
go-version: ^1.21
53+
54+
- name: Check out code
55+
uses: actions/checkout@v3
56+
57+
- name: Unit tests
58+
run: go test -v -count=1 -race -timeout=1m ./...
59+
working-directory: .

.golangci.yml

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
run:
2+
tests: true
3+
4+
linters-settings:
5+
funlen:
6+
lines: 150
7+
statements: 80
8+
9+
issues:
10+
exclude-rules:
11+
- path: _test\.go
12+
linters:
13+
- depguard
14+
- path: \.go
15+
linters:
16+
- depguard
17+
18+
linters:
19+
disable-all: true
20+
enable:
21+
- asciicheck
22+
- bodyclose
23+
- deadcode
24+
- depguard
25+
- dogsled
26+
- dupl
27+
- durationcheck
28+
- errorlint
29+
- exhaustive
30+
- exportloopref
31+
- funlen
32+
- gci
33+
- gocognit
34+
- goconst
35+
- gocritic
36+
- gocyclo
37+
- godot
38+
- gofmt
39+
- gofumpt
40+
- goheader
41+
- gomoddirectives
42+
- gomodguard
43+
- goprintffuncname
44+
- gosec
45+
- gosimple
46+
- govet
47+
- ifshort
48+
- importas
49+
- ineffassign
50+
- lll
51+
- makezero
52+
- misspell
53+
- nestif
54+
- nilerr
55+
- noctx
56+
- nolintlint
57+
- prealloc
58+
- predeclared
59+
- revive
60+
- rowserrcheck
61+
- sqlclosecheck
62+
- staticcheck
63+
- structcheck
64+
- stylecheck
65+
- tagliatelle
66+
- thelper
67+
- tparallel
68+
- typecheck
69+
- unconvert
70+
- unparam
71+
- unused
72+
- varcheck
73+
- wastedassign
74+
- whitespace

Makefile

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
API_BIN := "./bin/banner"
2+
DOCKER_IMG="banner:develop"
3+
4+
GIT_HASH := $(shell git log --format="%h" -n 1)
5+
LDFLAGS := -X main.release="develop" -X main.buildDate=$(shell date -u +%Y-%m-%dT%H:%M:%S) -X main.gitHash=$(GIT_HASH)
6+
7+
install-lint-deps:
8+
(which golangci-lint > /dev/null) || curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.55.2
9+
10+
lint: install-lint-deps
11+
golangci-lint run ./...
12+
13+
.PHONY: build run build-img run-img version test lint
14+
15+
generate:
16+
rm -rf internal/server/pb
17+
mkdir -p internal/server/pb
18+
19+
protoc \
20+
--proto_path=./api/ \
21+
--go_opt=paths=source_relative \
22+
--go-grpc_opt=paths=source_relative \
23+
--go_out=./internal/server/pb \
24+
--go-grpc_out=./internal/server/pb \
25+
api/*.proto
26+
27+
build-img:
28+
docker build \
29+
--build-arg=LDFLAGS="$(LDFLAGS)" \
30+
-t $(DOCKER_IMG) \
31+
-f build/banner/Dockerfile .
32+
33+
run-img: build-img
34+
docker run $(DOCKER_IMG)
35+
36+
up:
37+
docker-compose -f docker-compose.yaml up --build -d ;\
38+
docker-compose up -d
39+
40+
down:
41+
docker-compose down
42+
43+
test:
44+
go clean -testcache;
45+
go test -v -race -count 100 ./internal/...
46+
47+
integration-tests:
48+
set -e ;\
49+
docker-compose -f docker-compose.test.yaml up --build -d ;\
50+
test_status_code=0 ;\
51+
docker-compose -f docker-compose.test.yaml run integration_tests go test ./test/integration_test.go || test_status_code=$$? ;\
52+
docker-compose -f docker-compose.test.yaml down ;\
53+
echo $$test_status_code ;\
54+
exit $$test_status_code ;
55+
56+
57+
build:
58+
go build -v -o $(API_BIN) -ldflags "$(LDFLAGS)" ./cmd/banner
59+
60+
run: build
61+
$(API_BIN) -config ./configs/banner_config.yaml
62+
63+
build-debug:
64+
go build -gcflags="all=-N -l" -o $(API_BIN) -ldflags "$(LDFLAGS)" ./cmd/banner
65+
66+
run-debug: build-debug
67+
$(API_BIN) -config ./configs/banner_config.yaml
68+
69+
run-postgres:
70+
docker run -d --rm --name composepostgres -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e PGDATA=/var/lib/postgresql/data/pgdata -v psqldata:/var/lib/postgresql/data -p 5432:5432 postgres:latest
71+
72+
migrate-up:
73+
goose -dir migrations postgres "host=localhost user=postgres password=postgres dbname=postgres sslmode=disable" up
74+
75+
migrate-down:
76+
goose -dir migrations postgres "host=localhost user=postgres password=postgres dbname=postgres sslmode=disable" down
77+
78+
migrate-down-to-0001:
79+
goose -dir migrations postgres "host=localhost user=postgres password=postgres dbname=postgres sslmode=disable" down-to 0001
80+
81+
migrate-status:
82+
goose -dir migrations postgres "host=localhost user=postgres password=postgres dbname=postgres sslmode=disable" status
83+
84+
migrate-reset:
85+
goose -dir migrations postgres "host=localhost user=postgres password=postgres dbname=postgres sslmode=disable" reset
86+
87+
run-rabbitmq:
88+
docker run -d --rm --name rabbitmq -e RABBITMQ_DEFAULT_USER=guest -e RABBITMQ_DEFAULT_PASS=guest -p 15672:15672 -p 5672:5672 rabbitmq:3-management
89+
90+
stop-rabbitmq:
91+
docker stop rabbitmq

api/Service.proto

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
syntax = "proto3";
2+
3+
package banner;
4+
option go_package = "./;pb";
5+
6+
service BannerService {
7+
rpc AddBanner (AddBannerRequest) returns (AddBannerResponse) {}
8+
rpc RemoveBanner (RemoveBannerRequest) returns (RemoveBannerResponse) {}
9+
rpc ClickBanner (ClickBannerRequest) returns (ClickBannerResponse) {}
10+
rpc PickBanner (PickBannerRequest) returns (PickBannerResponse) {}
11+
}
12+
13+
message AddBannerRequest {
14+
int32 banner_id = 1;
15+
int32 slot_id = 2;
16+
}
17+
18+
message AddBannerResponse {
19+
string message = 1;
20+
}
21+
22+
message RemoveBannerRequest {
23+
int32 banner_id = 1;
24+
int32 slot_id = 2;
25+
}
26+
27+
message RemoveBannerResponse {
28+
string message = 1;
29+
}
30+
31+
message ClickBannerRequest {
32+
int32 banner_id = 1;
33+
int32 slot_id = 2;
34+
int32 usergroup_id = 3;
35+
}
36+
37+
message ClickBannerResponse {
38+
string message = 1;
39+
}
40+
41+
message PickBannerRequest {
42+
int32 slot_id = 1;
43+
int32 usergroup_id = 2;
44+
}
45+
46+
message PickBannerResponse {
47+
int32 banner_id = 1;
48+
string message = 2;
49+
}

build/banner/Dockerfile

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
FROM golang:1.21 as build
2+
3+
ENV BIN_FILE /opt/banner/banner-app
4+
ENV CODE_DIR /go/src/
5+
6+
# Set destination for COPY
7+
WORKDIR ${CODE_DIR}
8+
9+
# Caching layers with modules
10+
COPY ../../go.mod .
11+
COPY ../../go.sum .
12+
13+
# Download Go modules
14+
RUN go mod download
15+
16+
COPY build/banner ${CODE_DIR}
17+
18+
# Build a static Go binary (without C API dependencies),
19+
# otherwise it will not work in the alpine image.
20+
ARG LDFLAGS
21+
COPY . /go/src/
22+
RUN CGO_ENABLED=0 go build \
23+
-ldflags "$LDFLAGS" \
24+
-o ${BIN_FILE} cmd/banner/*
25+
26+
# The output is a thin image
27+
FROM alpine:3.19
28+
29+
LABEL ORGANIZATION="OTUS Online Education"
30+
LABEL SERVICE="banner"
31+
LABEL MAINTAINERS="[email protected]"
32+
33+
ENV BIN_FILE "/opt/banner/banner-app"
34+
COPY --from=build ${BIN_FILE} ${BIN_FILE}
35+
36+
ENV CONFIG_FILE /etc/banner/banner_config.yaml
37+
COPY ./configs/banner_config.yaml ${CONFIG_FILE}
38+
39+
ENV CONFIG_MIGRATION /etc/migrations
40+
COPY ./migrations ${CONFIG_MIGRATION}
41+
42+
CMD ${BIN_FILE} -config ${CONFIG_FILE}

cmd/banner/main.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//go:generate protoc --go_out=../../internal/server/pb --proto_path=../../api/ ../../api/Service.proto
2+
//go:generate protoc --go-grpc_out=../../internal/server/pb --proto_path=../../api/ ../../api/Service.proto
3+
package main
4+
5+
import (
6+
"context"
7+
"flag"
8+
"fmt"
9+
"log"
10+
"os"
11+
"os/signal"
12+
"syscall"
13+
14+
"github.com/cronnoss/banners-rotation/internal/app/banner"
15+
"github.com/cronnoss/banners-rotation/internal/config"
16+
"github.com/pkg/errors"
17+
)
18+
19+
var bannerConfigFile string
20+
21+
func init() {
22+
flag.StringVar(&bannerConfigFile, "config", "banner_config.yaml", "Path to configuration file")
23+
}
24+
25+
func main() {
26+
if err := mainImpl(); err != nil {
27+
log.Fatal(err)
28+
}
29+
}
30+
31+
func mainImpl() error {
32+
ctx, cancel := context.WithCancel(context.TODO())
33+
defer cancel()
34+
35+
flag.Parse()
36+
37+
if bannerConfigFile == "" {
38+
return fmt.Errorf("please set: '--config=<Path to configuration file>'")
39+
}
40+
41+
conf := new(config.BannerConfig)
42+
if err := conf.Init(bannerConfigFile); err != nil {
43+
return errors.Wrap(err, "failed to init config")
44+
}
45+
46+
_, err := banner.NewApp(ctx, conf)
47+
if err != nil {
48+
return fmt.Errorf("failed to create app: %w", err)
49+
}
50+
51+
sigChan := make(chan os.Signal, 1)
52+
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
53+
<-sigChan
54+
55+
return nil
56+
}

0 commit comments

Comments
 (0)