-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Chystik/transaction_management
Transaction management
- Loading branch information
Showing
48 changed files
with
3,099 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Logs | ||
*log | ||
|
||
# Binaries | ||
/cmd/gophermart/main | ||
/cmd/gophermart/gophermart |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.