Skip to content

Commit

Permalink
Merge pull request #14 from dmitryt/hw12_13_14_15_calendar
Browse files Browse the repository at this point in the history
Hw12 13 14 15 calendar
  • Loading branch information
dmitryt authored Sep 16, 2020
2 parents 126a41d + dc297ce commit 5ca8860
Show file tree
Hide file tree
Showing 48 changed files with 1,671 additions and 477 deletions.
16 changes: 12 additions & 4 deletions hw12_13_14_15_calendar/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build:
go build -o ./bin ./...

test:
go test -race ./...
go test -race -v `go list ./... | grep -v integration-tests`

lint:
golangci-lint run ./...
Expand All @@ -18,8 +18,16 @@ launch:
run:
make build && make launch

migrate:
./scripts/migrations.sh
up:
docker-compose -f deployments/docker-compose.yml up -d

down:
docker-compose -f deployments/docker-compose.yml down

integration-tests:
docker-compose -f deployments/docker-compose.yml -f deployments/docker-compose.test.yml up -d --build && \
API_HOST=http://localhost:8888 ginkgo ./integration-tests && \
docker-compose -f deployments/docker-compose.yml -f deployments/docker-compose.test.yml down

generate:
protoc \
Expand All @@ -29,4 +37,4 @@ generate:
--grpc-gateway_out=logtostderr=true:${cdir}/service/server \
${cdir}/service/schema/calendar.proto

.PHONY: build
.PHONY: build integration-tests
4 changes: 1 addition & 3 deletions hw12_13_14_15_calendar/cmd/calendar_scheduler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package main
import (
"context"
"flag"
"fmt"
"strconv"

"github.com/dmitryt/otus-golang-hw/hw12_13_14_15_calendar/internal/config"
"github.com/dmitryt/otus-golang-hw/hw12_13_14_15_calendar/internal/logger"
Expand Down Expand Up @@ -49,7 +47,7 @@ func main() {
qCfg := cfg.QueueConfig

producer := queue.NewProducer(
fmt.Sprintf("amqp://%s:%s@%s:%s/", qCfg.User, qCfg.Pass, qCfg.Host, strconv.Itoa(qCfg.Port)),
qCfg.URI,
qCfg.QueueName,
qCfg.ExchangeType,
qCfg.MaxReconnectAttempts,
Expand Down
21 changes: 16 additions & 5 deletions hw12_13_14_15_calendar/cmd/calendar_sender/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package main

import (
"context"
"flag"
"fmt"
"strconv"

"github.com/dmitryt/otus-golang-hw/hw12_13_14_15_calendar/internal/config"
"github.com/dmitryt/otus-golang-hw/hw12_13_14_15_calendar/internal/logger"
"github.com/dmitryt/otus-golang-hw/hw12_13_14_15_calendar/internal/queue"
"github.com/dmitryt/otus-golang-hw/hw12_13_14_15_calendar/internal/repository"
"github.com/dmitryt/otus-golang-hw/hw12_13_14_15_calendar/internal/sender"
"github.com/rs/zerolog/log"
)
Expand All @@ -24,6 +24,8 @@ func init() {

func main() {
flag.Parse()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

cfg, err := config.NewSender(cfgPath)
if err != nil {
Expand All @@ -34,17 +36,26 @@ func main() {
fatal(err)
}

qCfg := cfg.QueueConfig
repo := repository.NewStats(cfg.DBConfig.RepoType, cfg.DBConfig.ItemsPerQuery, cfg.DBConfig.MaxConn)
if repo == nil {
fatal(repository.ErrUnSupportedRepoType)
}

if err = repo.Connect(ctx, repository.GetSQLDSN(&cfg.DBConfig)); err != nil {
fatal(err)
}
defer repo.Close()

qCfg := cfg.QueueConfig
consumer := queue.NewConsumer(
fmt.Sprintf("amqp://%s:%s@%s:%s/", qCfg.User, qCfg.Pass, qCfg.Host, strconv.Itoa(qCfg.Port)),
qCfg.URI,
qCfg.QueueName,
qCfg.ExchangeType,
qCfg.QosPrefetchCount,
qCfg.MaxReconnectAttempts,
qCfg.ReconnectTimeoutMs,
)
app := sender.New(consumer, qCfg.ScanTimeoutMs)
app := sender.New(repo, consumer, qCfg.ScanTimeoutMs)
if err := app.Run(); err != nil {
fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion hw12_13_14_15_calendar/configs/calendar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dbConfig:
port: 5432
dbName: "db_calendar"
user: "db_calendar_user"
pass: "test"
password: "test"
itemsPerQuery: 50
repoType: "psql"

Expand Down
5 changes: 1 addition & 4 deletions hw12_13_14_15_calendar/configs/sender.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ logConfig:
filePath: "calendar_sender.log"

queueConfig:
user: "guest"
pass: "guest"
host: "localhost"
port: 5672
uri: "amqp://guest:guest@localhost:5672/"
queueName: "events"
scanTimeout: 10
31 changes: 31 additions & 0 deletions hw12_13_14_15_calendar/deployments/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
FROM golang:1.14-alpine AS builder

RUN apk update

# Set necessary environmet variables needed for our image
ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux

WORKDIR /build

COPY go.mod go.sum ./
RUN go mod download

# Copy the code into the container
COPY . .

ARG MAIN_FILE_PATH

# Build the application
RUN go build -o main $MAIN_FILE_PATH
RUN chmod +x main

# Build a small image
FROM scratch

COPY --from=builder /build/main /
EXPOSE 8081

# Command to run
ENTRYPOINT ["/main"]
2 changes: 2 additions & 0 deletions hw12_13_14_15_calendar/deployments/api.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GRPC_ADDRESS=0.0.0.0:50051
HTTP_ADDRESS=0.0.0.0:50052
7 changes: 7 additions & 0 deletions hw12_13_14_15_calendar/deployments/database.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
DB_HOST=db
DB_PORT=5432
DB_USER=db_calendar_user
DB_PASSWORD=test
DB_NAME=db_calendar
DB_REPO_TYPE=psql
DB_ITEMS_PER_QUERY=50
1 change: 1 addition & 0 deletions hw12_13_14_15_calendar/deployments/database_test.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DB_NAME=db_calendar_test
10 changes: 10 additions & 0 deletions hw12_13_14_15_calendar/deployments/docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "2.4"

services:
db:
volumes:
- pg_volume:/var/lib/postgresql/data

volumes:
pg_volume:
driver: local
20 changes: 20 additions & 0 deletions hw12_13_14_15_calendar/deployments/docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: "2.4"

services:
db:
environment:
- POSTGRES_DB=db_calendar_test
migrator:
env_file:
- database_test.env
calendar:
env_file:
- database_test.env
calendar_scheduler:
env_file:
- database_test.env
- queue_test.env
calendar_sender:
env_file:
- database_test.env
- queue_test.env
94 changes: 94 additions & 0 deletions hw12_13_14_15_calendar/deployments/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
version: "2.4"

services:
db:
image: postgres:12
environment:
- POSTGRES_USER=db_calendar_user
- POSTGRES_PASSWORD=test
- POSTGRES_DB=db_calendar
ports:
- "5432:5432"
healthcheck:
test: ["CMD", "pg_isready", "-U", "$POSTGRES_USER"]
interval: 10s
timeout: 10s
retries: 3
start_period: 20s
migrator:
build:
context: ../
dockerfile: ./deployments/migrator/Dockerfile
env_file:
- database.env
depends_on:
db:
condition: service_healthy
queue:
build:
context: ./rabbitmq
environment:
- RABBITMQ_DEFAULT_USER=guest
- RABBITMQ_DEFAULT_PASS=guest
healthcheck:
test: [ "CMD", "nc", "-z", "localhost", "5672" ]
interval: 10s
timeout: 10s
retries: 3
start_period: 20s
ports:
- "5672:5672"
- "15672:15672"
calendar:
build:
context: ../
dockerfile: ./deployments/Dockerfile
args:
- MAIN_FILE_PATH=cmd/calendar/main.go
env_file:
- database.env
- logger.env
- api.env
environment:
- PORT=8081
volumes:
- ../migrations:/migrations
ports:
- "8888:50052"
depends_on:
db:
condition: service_healthy
calendar_scheduler:
build:
context: ../
dockerfile: ./deployments/Dockerfile
args:
- MAIN_FILE_PATH=cmd/calendar_scheduler/main.go
env_file:
- database.env
- logger.env
- queue.env
volumes:
- ../migrations:/migrations
depends_on:
db:
condition: service_healthy
queue:
condition: service_healthy
calendar_sender:
build:
context: ../
dockerfile: ./deployments/Dockerfile
args:
- MAIN_FILE_PATH=cmd/calendar_sender/main.go
env_file:
- database.env
- logger.env
- queue.env
volumes:
- ../migrations:/migrations
depends_on:
db:
condition: service_healthy
queue:
condition: service_healthy
2 changes: 2 additions & 0 deletions hw12_13_14_15_calendar/deployments/logger.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
LOG_LEVEL=debug
LOG_FILE=calendar.log
19 changes: 19 additions & 0 deletions hw12_13_14_15_calendar/deployments/migrator/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM golang:1.14-alpine

RUN apk update

ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux

WORKDIR /build

# install goose
RUN go get 'github.com/pressly/goose/cmd/goose'

# mount the app
RUN mkdir -p /opt/db
COPY ./migrations /opt/db/migrations

# define goose as the entrypoint
ENTRYPOINT /go/bin/goose -v -dir /opt/db/migrations postgres "host=$DB_HOST port=$DB_PORT user=$DB_USER password=$DB_PASSWORD dbname=$DB_NAME sslmode=disable" up
3 changes: 3 additions & 0 deletions hw12_13_14_15_calendar/deployments/queue.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
QUEUE_URI=amqp://guest:guest@queue:5672/
QUEUE_NAME=events
QUEUE_SCAN_TIMEOUT_MS=10000
1 change: 1 addition & 0 deletions hw12_13_14_15_calendar/deployments/queue_test.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
QUEUE_NAME=events-tests
3 changes: 3 additions & 0 deletions hw12_13_14_15_calendar/deployments/rabbitmq/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM rabbitmq:3-management
RUN apt-get update
RUN apt-get install -y netcat
9 changes: 8 additions & 1 deletion hw12_13_14_15_calendar/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@ require (
github.com/golang/protobuf v1.4.2
github.com/grpc-ecosystem/go-grpc-middleware v1.2.1
github.com/grpc-ecosystem/grpc-gateway v1.14.7
github.com/heetch/confita v0.9.2
github.com/ilyakaznacheev/cleanenv v1.2.5
github.com/imdario/mergo v0.3.11
github.com/jmoiron/sqlx v1.2.0
github.com/kr/text v0.2.0 // indirect
github.com/lib/pq v1.8.0
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/onsi/ginkgo v1.14.1
github.com/onsi/gomega v1.10.1
github.com/pkg/errors v0.9.1 // indirect
github.com/pressly/goose v2.6.0+incompatible
github.com/rs/zerolog v1.15.0
github.com/streadway/amqp v1.0.0
github.com/stretchr/testify v1.6.1
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009 // indirect
golang.org/x/text v0.3.3 // indirect
google.golang.org/appengine v1.6.1 // indirect
google.golang.org/genproto v0.0.0-20200815001618-f69a88009b70
google.golang.org/grpc v1.31.0
google.golang.org/protobuf v1.25.0
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
)
Loading

0 comments on commit 5ca8860

Please sign in to comment.