Skip to content

Commit

Permalink
Merge pull request #2 from Chystik/transaction_management
Browse files Browse the repository at this point in the history
Transaction management
  • Loading branch information
Chystik authored Nov 2, 2023
2 parents e8253ce + 52baf8b commit 6a32a60
Show file tree
Hide file tree
Showing 48 changed files with 3,099 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Logs
*log

# Binaries
/cmd/gophermart/main
/cmd/gophermart/gophermart
13 changes: 13 additions & 0 deletions .env.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ENVIRONMENT=dev
HOST=127.0.0.1
#API
APP_NAME=gophermart
RUN_ADDRESS=localhost:8080
DATABASE_URI=postgres://postgres:postgres@localhost:5432/praktikum?sslmode=disable
ACCRUAL_SYSTEM_ADDRESS=http://localhost:8787
#POSTGRES
DB_PG_ENDPOINT=postgres
DB_PG_PORT=5432
DB_PG_USER=postgres
DB_PG_PASSWORD=postgres
DB_PG_DBNAME=praktikum
9 changes: 4 additions & 5 deletions .github/workflows/gophermart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ on:
- master

jobs:

build:
runs-on: ubuntu-latest
container: golang:1.19
container: golang:1.21

services:
postgres:
Expand All @@ -31,10 +30,10 @@ jobs:
uses: actions/checkout@v2

- name: Download autotests binaries
uses: robinraju/release-downloader@v1.2
uses: robinraju/release-downloader@v1.8
with:
repository: Yandex-Practicum/go-autotests-bin
latest: true
repository: Yandex-Practicum/go-autotests
tag: "v0.10.1"
fileName: "*"
out-file-path: .tools

Expand Down
9 changes: 4 additions & 5 deletions .github/workflows/statictest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@ on:
- main

jobs:

statictest:
runs-on: ubuntu-latest
container: golang:1.19
container: golang:1.21
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Download statictest binary
uses: robinraju/release-downloader@v1
uses: robinraju/release-downloader@v1.8
with:
repository: Yandex-Practicum/go-autotests-bin
latest: true
repository: Yandex-Practicum/go-autotests
tag: "v0.10.1"
fileName: statictest
out-file-path: .tools

Expand Down
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@
*.dylib

# Test binary, built with `go test -c`
*.test
.tools/

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Binaries
cmd/gophermart/main
cmd/gophermart/gophermart
.tools/

# Dependency directories (remove the comment below to include it)
vendor/

# IDEs directories
.idea
.vscode

# logs
*.log
35 changes: 35 additions & 0 deletions Dockerfile.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
FROM docker.io/golang:1.21

ARG BIN_PATH

WORKDIR $GOPATH/src/gophermart

COPY . .

# Fetch dependencies.
RUN go mod download
RUN go mod verify

COPY ./cmd/accrual/accrual_linux_amd64 $BIN_PATH/

# Build the binary
RUN GOOS=linux GOARCH=amd64 go build -buildvcs=false -o $BIN_PATH/gophermart ./cmd/gophermart

# Get latest praktikum test tools
RUN wget -O $BIN_PATH/gophermarttest https://github.com/Yandex-Practicum/go-autotests/releases/download/v0.10.1/gophermarttest
RUN wget -O $BIN_PATH/random https://github.com/Yandex-Practicum/go-autotests/releases/download/v0.10.1/random
RUN wget -O $BIN_PATH/statictest https://github.com/Yandex-Practicum/go-autotests/releases/download/v0.10.1/statictest
RUN chmod -R +x $BIN_PATH

RUN go vet -vettool=$BIN_PATH/statictest ./...

CMD gophermarttest \
-test.v -test.run=^TestGophermart$ \
-gophermart-binary-path=gophermart \
-gophermart-host=localhost \
-gophermart-port=8080 \
-gophermart-database-uri="postgresql://postgres:postgres@postgres/praktikum?sslmode=disable" \
-accrual-binary-path=accrual_linux_amd64 \
-accrual-host=localhost \
-accrual-port=$(random unused-port) \
-accrual-database-uri="postgresql://postgres:postgres@postgres/praktikum?sslmode=disable"
67 changes: 67 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#SHELL = /bin/bash

.PHONY: dep
dep:
go mod download
go mod tidy

.PHONY: test
test:
go test ./...

.PHONY: race
race:
go test -v -race ./...

.PHONY: init
lint:
/home/user/go/bin/golangci-lint run

accrual-port = $(shell ./.tools/random unused-port)
gophermart-bin = ./cmd/gophermart/gophermart
.PHONY: autotest
autotest:
go build -o $(gophermart-bin) ./cmd/gophermart
./.tools/gophermarttest \
-test.v -test.run=^TestGophermart$ \
-gophermart-binary-path=$(gophermart-bin) \
-gophermart-host=localhost \
-gophermart-port=8080 \
-gophermart-database-uri="postgresql://postgres:postgres@localhost/praktikum?sslmode=disable" \
-accrual-binary-path=./cmd/accrual/accrual_linux_amd64 \
-accrual-host=localhost \
-accrual-port=$(accrual-port) \
-accrual-database-uri="postgresql://postgres:postgres@localhost/praktikum?sslmode=disable"
rm $(gophermart-bin)

.PHONY: gen
gen:
go generate ./...

.PHONY: cover
cover:
go test -short -count=1 -race -coverprofile=coverage.out ./...
go tool cover -html=coverage.out -o=coverage.html
rm coverage.out

.PHONY: statictest
statictest:
go vet -vettool=./.tools/statictest ./...

.PHONY: dev-up
dev-up:
docker-compose -f=docker-compose.dev.yml --env-file=.env.dev up -d

.PHONY: dev-down
dev-down:
docker-compose -f=docker-compose.dev.yml --env-file=.env.dev down --rmi local

.PHONY: dev-autotest
dev-autotest:
docker-compose -f=docker-compose.dev-test.yml --env-file=.env.dev up -d
docker-compose -f=docker-compose.dev-test.yml --env-file=.env.dev logs -f tests
docker-compose -f=docker-compose.dev-test.yml --env-file=.env.dev down --rmi local

.PHONY: accrual
accrual:
./cmd/accrual/accrual_linux_amd64 -a=localhost:8787 -d="postgresql://postgres:postgres@localhost:5432/praktikum?sslmode=disable"
13 changes: 13 additions & 0 deletions cmd/gophermart/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"os"

"github.com/Chystik/gophermart/config"
)

func parseEnv(cfg *config.App) {
cfg.Address = config.Address(os.Getenv("RUN_ADDRESS"))
cfg.DBuri = config.DBuri(os.Getenv("DATABASE_URI"))
cfg.AccrualAddress = config.AccrualAddress(os.Getenv("ACCRUAL_SYSTEM_ADDRESS"))
}
19 changes: 19 additions & 0 deletions cmd/gophermart/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"flag"

"github.com/Chystik/gophermart/config"
)

func parseFlags(cfg *config.App) {
// Check interface implementation
_ = flag.Value(&cfg.Address)
_ = flag.Value(&cfg.DBuri)
_ = flag.Value(&cfg.AccrualAddress)

flag.Var(&cfg.Address, "a", "app address")
flag.Var(&cfg.DBuri, "d", "database uri")
flag.Var(&cfg.AccrualAddress, "r", "accal service address")
flag.Parse()
}
31 changes: 30 additions & 1 deletion cmd/gophermart/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
package main

func main() {}
import (
"os"
"os/signal"
"syscall"

"github.com/Chystik/gophermart/config"
"github.com/Chystik/gophermart/run"

"github.com/joho/godotenv"
)

func main() {
cfg := config.NewAppConfig()

if osEnv := os.Getenv("ENVIRONMENT"); osEnv == "dev" {
err := godotenv.Load(".env.dev")
if err != nil {
panic(err)
}
}

parseEnv(cfg)
parseFlags(cfg)

// channel for Graceful shutdown
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)

run.App(cfg, quit)
}
62 changes: 62 additions & 0 deletions config/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package config

import (
"errors"
"strconv"
"strings"
)

type (
App struct {
Address Address `env:"RUN_ADDRESS"`
DBuri DBuri `env:"DATABASE_URI"`
AccrualAddress AccrualAddress `env:"ACCRUAL_SYSTEM_ADDRESS"`
JWTkey []byte
}

Address string
DBuri string
AccrualAddress string
)

func NewAppConfig() *App {
return &App{
Address: ":8080",
JWTkey: []byte("my_secret_key"),
}
}

func (addr Address) String() string {
return string(addr)
}

func (addr *Address) Set(s string) error {
hp := strings.Split(s, ":")
if len(hp) != 2 {
return errors.New("expect address in a form host:port")
}
_, err := strconv.Atoi(hp[1])
if err != nil {
return errors.New("only digits allowed for port in a form host:port")
}
*addr = Address(s)
return nil
}

func (db DBuri) String() string {
return string(db)
}

func (db *DBuri) Set(s string) error {
*db = DBuri(s)
return nil
}

func (accr AccrualAddress) String() string {
return string(accr)
}

func (accr *AccrualAddress) Set(s string) error {
*accr = AccrualAddress(s)
return nil
}
44 changes: 44 additions & 0 deletions docker-compose.dev-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
version: '3.8'

services:
db:
container_name: ${DB_PG_ENDPOINT}
image: postgres:14.4-alpine
hostname: ${DB_PG_ENDPOINT}
environment:
- POSTGRES_PASSWORD=${DB_PG_PASSWORD}
- POSTGRES_USER=${DB_PG_USER}
restart: unless-stopped
ports:
- ${DB_PG_PORT}:${DB_PG_PORT}
networks:
- db
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 5
logging:
options:
max-size: "10m"
max-file: "3"

tests:
container_name: ${APP_NAME}-tests
build:
context: .
dockerfile: Dockerfile.test
args:
BIN_PATH: /usr/local/bin
depends_on:
db:
condition: service_healthy
networks:
- db

networks:
db:

volumes:
pg:
name: ${APP_NAME}-test-pg
Loading

0 comments on commit 6a32a60

Please sign in to comment.