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

developed interaction between services #1

Merged
merged 2 commits into from
Jan 27, 2024
Merged
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
59 changes: 59 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: BannersCI

on:
push:
branches:
- "*"

env:
GO111MODULE: "on"

jobs:
lint:
runs-on: ubuntu-latest
steps:

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ^1.21

- name: Checkout code
uses: actions/checkout@v3

- name: Linters
uses: golangci/golangci-lint-action@v3
with:
version: latest
working-directory: .

build:
runs-on: ubuntu-latest
steps:

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ^1.21

- name: Check out code
uses: actions/checkout@v3

- name: Build service binary
run: go build -o banner ./cmd/banner

tests:
runs-on: ubuntu-latest
steps:

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ^1.21

- name: Check out code
uses: actions/checkout@v3

- name: Unit tests
run: go test -v -count=1 -race -timeout=1m ./...
working-directory: .
74 changes: 74 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
run:
tests: true

linters-settings:
funlen:
lines: 150
statements: 80

issues:
exclude-rules:
- path: _test\.go
linters:
- depguard
- path: \.go
linters:
- depguard

linters:
disable-all: true
enable:
- asciicheck
- bodyclose
- deadcode
- depguard
- dogsled
- dupl
- durationcheck
- errorlint
- exhaustive
- exportloopref
- funlen
- gci
- gocognit
- goconst
- gocritic
- gocyclo
- godot
- gofmt
- gofumpt
- goheader
- gomoddirectives
- gomodguard
- goprintffuncname
- gosec
- gosimple
- govet
- ifshort
- importas
- ineffassign
- lll
- makezero
- misspell
- nestif
- nilerr
- noctx
- nolintlint
- prealloc
- predeclared
- revive
- rowserrcheck
- sqlclosecheck
- staticcheck
- structcheck
- stylecheck
- tagliatelle
- thelper
- tparallel
- typecheck
- unconvert
- unparam
- unused
- varcheck
- wastedassign
- whitespace
91 changes: 91 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
API_BIN := "./bin/banner"
DOCKER_IMG="banner:develop"

GIT_HASH := $(shell git log --format="%h" -n 1)
LDFLAGS := -X main.release="develop" -X main.buildDate=$(shell date -u +%Y-%m-%dT%H:%M:%S) -X main.gitHash=$(GIT_HASH)

install-lint-deps:
(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

lint: install-lint-deps
golangci-lint run ./...

.PHONY: build run build-img run-img version test lint

generate:
rm -rf internal/server/pb
mkdir -p internal/server/pb

protoc \
--proto_path=./api/ \
--go_opt=paths=source_relative \
--go-grpc_opt=paths=source_relative \
--go_out=./internal/server/pb \
--go-grpc_out=./internal/server/pb \
api/*.proto

build-img:
docker build \
--build-arg=LDFLAGS="$(LDFLAGS)" \
-t $(DOCKER_IMG) \
-f build/banner/Dockerfile .

run-img: build-img
docker run $(DOCKER_IMG)

up:
docker-compose -f docker-compose.yaml up --build -d ;\
docker-compose up -d

down:
docker-compose down

test:
go clean -testcache;
go test -v -race -count 100 ./internal/...

integration-tests:
set -e ;\
docker-compose -f docker-compose.test.yaml up --build -d ;\
test_status_code=0 ;\
docker-compose -f docker-compose.test.yaml run integration_tests go test ./test/integration_test.go || test_status_code=$$? ;\
docker-compose -f docker-compose.test.yaml down ;\
echo $$test_status_code ;\
exit $$test_status_code ;


build:
go build -v -o $(API_BIN) -ldflags "$(LDFLAGS)" ./cmd/banner

run: build
$(API_BIN) -config ./configs/banner_config.yaml

build-debug:
go build -gcflags="all=-N -l" -o $(API_BIN) -ldflags "$(LDFLAGS)" ./cmd/banner

run-debug: build-debug
$(API_BIN) -config ./configs/banner_config.yaml

run-postgres:
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

migrate-up:
goose -dir migrations postgres "host=localhost user=postgres password=postgres dbname=postgres sslmode=disable" up

migrate-down:
goose -dir migrations postgres "host=localhost user=postgres password=postgres dbname=postgres sslmode=disable" down

migrate-down-to-0001:
goose -dir migrations postgres "host=localhost user=postgres password=postgres dbname=postgres sslmode=disable" down-to 0001

migrate-status:
goose -dir migrations postgres "host=localhost user=postgres password=postgres dbname=postgres sslmode=disable" status

migrate-reset:
goose -dir migrations postgres "host=localhost user=postgres password=postgres dbname=postgres sslmode=disable" reset

run-rabbitmq:
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

stop-rabbitmq:
docker stop rabbitmq
49 changes: 49 additions & 0 deletions api/Service.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
syntax = "proto3";

package banner;
option go_package = "./;pb";

service BannerService {
rpc AddBanner (AddBannerRequest) returns (AddBannerResponse) {}
rpc RemoveBanner (RemoveBannerRequest) returns (RemoveBannerResponse) {}
rpc ClickBanner (ClickBannerRequest) returns (ClickBannerResponse) {}
rpc PickBanner (PickBannerRequest) returns (PickBannerResponse) {}
}

message AddBannerRequest {
int32 banner_id = 1;
int32 slot_id = 2;
}

message AddBannerResponse {
string message = 1;
}

message RemoveBannerRequest {
int32 banner_id = 1;
int32 slot_id = 2;
}

message RemoveBannerResponse {
string message = 1;
}

message ClickBannerRequest {
int32 banner_id = 1;
int32 slot_id = 2;
int32 usergroup_id = 3;
}

message ClickBannerResponse {
string message = 1;
}

message PickBannerRequest {
int32 slot_id = 1;
int32 usergroup_id = 2;
}

message PickBannerResponse {
int32 banner_id = 1;
string message = 2;
}
42 changes: 42 additions & 0 deletions build/banner/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
FROM golang:1.21 as build

ENV BIN_FILE /opt/banner/banner-app
ENV CODE_DIR /go/src/

# Set destination for COPY
WORKDIR ${CODE_DIR}

# Caching layers with modules
COPY ../../go.mod .
COPY ../../go.sum .

# Download Go modules
RUN go mod download

COPY build/banner ${CODE_DIR}

# Build a static Go binary (without C API dependencies),
# otherwise it will not work in the alpine image.
ARG LDFLAGS
COPY . /go/src/
RUN CGO_ENABLED=0 go build \
-ldflags "$LDFLAGS" \
-o ${BIN_FILE} cmd/banner/*

# The output is a thin image
FROM alpine:3.19

LABEL ORGANIZATION="OTUS Online Education"
LABEL SERVICE="banner"
LABEL MAINTAINERS="[email protected]"

ENV BIN_FILE "/opt/banner/banner-app"
COPY --from=build ${BIN_FILE} ${BIN_FILE}

ENV CONFIG_FILE /etc/banner/banner_config.yaml
COPY ./configs/banner_config.yaml ${CONFIG_FILE}

ENV CONFIG_MIGRATION /etc/migrations
COPY ./migrations ${CONFIG_MIGRATION}

CMD ${BIN_FILE} -config ${CONFIG_FILE}
56 changes: 56 additions & 0 deletions cmd/banner/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//go:generate protoc --go_out=../../internal/server/pb --proto_path=../../api/ ../../api/Service.proto
//go:generate protoc --go-grpc_out=../../internal/server/pb --proto_path=../../api/ ../../api/Service.proto
package main

import (
"context"
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"

"github.com/cronnoss/banners-rotation/internal/app/banner"
"github.com/cronnoss/banners-rotation/internal/config"
"github.com/pkg/errors"
)

var bannerConfigFile string

func init() {
flag.StringVar(&bannerConfigFile, "config", "banner_config.yaml", "Path to configuration file")
}

func main() {
if err := mainImpl(); err != nil {
log.Fatal(err)
}
}

func mainImpl() error {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()

flag.Parse()

if bannerConfigFile == "" {
return fmt.Errorf("please set: '--config=<Path to configuration file>'")
}

conf := new(config.BannerConfig)
if err := conf.Init(bannerConfigFile); err != nil {
return errors.Wrap(err, "failed to init config")
}

_, err := banner.NewApp(ctx, conf)
if err != nil {
return fmt.Errorf("failed to create app: %w", err)
}

sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
<-sigChan

return nil
}
Loading