Skip to content

Commit 7584b4c

Browse files
author
Maksim Grafskiy
committed
ref
1 parent fb56539 commit 7584b4c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+2050
-1231
lines changed

.dockerignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
configs
33
deployments
44
migrations
5-
proto
65
.gitignore
76
.gitlab-ci.yml
87
.golangci.yml

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
.idea
12
/configs/.env
23
/configs/.env_localhost

.golangci.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ linters:
4646
- nakedret
4747
- noctx
4848
# - nolintlint
49-
- revive
49+
# - revive
5050
- staticcheck
5151
- stylecheck
5252
- testifylint
@@ -90,7 +90,11 @@ linters-settings:
9090
- dupImport # https://github.com/go-critic/go-critic/issues/845
9191
- ifElseChain
9292
- octalLiteral
93-
- whyNoLint
93+
settings:
94+
hugeParam:
95+
# Size in bytes that makes the warning trigger.
96+
# Default: 80
97+
sizeThreshold: 80
9498
gocyclo:
9599
min-complexity: 15
96100
godox:

Makefile

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,43 @@
11
export
22

3+
## Installation
4+
5+
.PHONY: install
6+
install: ## Install development utils
7+
@echo "* Running migrate-install..."
8+
$(MAKE) migrate-install
9+
@echo "* Running lint-install..."
10+
$(MAKE) openapi-generate
11+
@echo "* Running generate-install..."
12+
$(MAKE) generate-install
13+
14+
## Generation
15+
16+
.PHONY: generate-install
17+
generate-install:
18+
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
19+
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
20+
go install github.com/swaggo/swag/cmd/swag@latest
21+
22+
.PHONY: proto-generate
23+
proto-generate:
24+
protoc --go_out=proto/gen --go-grpc_out=proto/gen --proto_path=proto proto/shortener_v1.proto
25+
26+
.PHONY: openapi-generate
27+
openapi-generate:
28+
swag init --generalInfo ./pkg/http/server.go --parseInternal
29+
30+
.PHONY: generate
31+
generate: ## Generate artifacts
32+
@echo "* Running proto-generate..."
33+
$(MAKE) proto-generate
34+
@echo "* Running openapi-generate..."
35+
$(MAKE) openapi-generate
36+
@echo "* Running go generate..."
37+
go generate ./...
38+
$(MAKE) fmt
39+
40+
341
## Migrations
442

543
DB_MIGRATE_URL = postgres://login:pass@localhost:5432/app-db?sslmode=disable
@@ -21,23 +59,6 @@ migrate-up:
2159
migrate-down:
2260
migrate -database "$(DB_MIGRATE_URL)" -path "$(MIGRATE_PATH)" down -all
2361

24-
## Proto
25-
26-
.PHONY: proto-install
27-
proto-install:
28-
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
29-
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
30-
31-
.PHONY: proto-generate
32-
proto-generate:
33-
protoc --go_out=generated/protobuf --go-grpc_out=generated/protobuf --proto_path=proto proto/shortener_v1.proto
34-
35-
## Openapi
36-
37-
.PHONY: openapi-generate
38-
openapi-generate:
39-
swag init --generalInfo ./pkg/http/server.go --parseInternal
40-
4162
## docker compose
4263

4364
.PHONY: up
@@ -63,7 +84,7 @@ lint: ## 🚨 Run lint checks
6384
.PHONY: fmt
6485
fmt: ## 🎨 Fix code format issues
6586
gofumpt -w .
66-
goimports -w -local github.com/xgmsx/go-url-shortener .
87+
goimports -w -local github.com/xgmsx/go-tiny-url .
6788

6889
## Building, running, escape analysis
6990

@@ -79,13 +100,6 @@ run: ## 🚶 Run the program
79100
rune: ## 🔎 Run the program with escape analysis
80101
go run -gcflags='-m=3' ./cmd/app
81102

82-
.PHONY: generate
83-
generate: ## Generate artifacts
84-
@echo "* Running proto-generate..."
85-
$(MAKE) proto-generate
86-
@echo "* Running openapi-generate..."
87-
$(MAKE) openapi-generate
88-
89103
## Tests, tests, tests...
90104

91105
.PHONY: cov

README.md

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# go-url-shortener-ddd
1+
# go-tiny-url
22

3-
[![Linting](https://github.com/xgmsx/go-url-shortener-ddd/actions/workflows/golangci-lint.yml/badge.svg?branch=main)](https://github.com/xgmsx/go-url-shortener-ddd/actions/workflows/golangci-lint.yml)
4-
[![Tests](https://github.com/xgmsx/go-url-shortener-ddd/actions/workflows/coverage.yml/badge.svg?branch=main)](https://github.com/xgmsx/go-url-shortener-ddd/actions/workflows/coverage.yml)
5-
[![CodeQL](https://github.com/xgmsx/go-url-shortener-ddd/actions/workflows/codeql.yml/badge.svg?branch=main)](https://github.com/xgmsx/go-url-shortener-ddd/actions/workflows/codeql.yml)
6-
[![Coverage_Report](https://img.shields.io/badge/Coverage_Report-30.8%25-yellow)](https://xgmsx.github.io/go-url-shortener-ddd)
3+
[![Linting](https://github.com/xgmsx/go-tiny-url/actions/workflows/golangci-lint.yml/badge.svg?branch=main)](https://github.com/xgmsx/go-tiny-url/actions/workflows/golangci-lint.yml)
4+
[![Tests](https://github.com/xgmsx/go-tiny-url/actions/workflows/coverage.yml/badge.svg?branch=main)](https://github.com/xgmsx/go-tiny-url/actions/workflows/coverage.yml)
5+
[![CodeQL](https://github.com/xgmsx/go-tiny-url/actions/workflows/codeql.yml/badge.svg?branch=main)](https://github.com/xgmsx/go-tiny-url/actions/workflows/codeql.yml)
6+
[![Coverage_Report](https://img.shields.io/badge/Coverage_Report-57.9%25-yellow)](https://xgmsx.github.io/go-tiny-url)
77

8-
**go-url-shortener-ddd** — это сокращатель ссылок на Go, реализованный с использованием архитектурного подхода Domain-Driven Design (DDD).
8+
Проект для демонстрации разработки на Golang с использованием gRPC, REST HTTP, Kafka, Postgres, Observability, Unit-testing.
99

1010
Структура:
1111
* [internal](internal) содержит пакеты относящиеся к проекту - app, config, shortener.
@@ -35,8 +35,8 @@ Table of Contents:
3535
<div class="termy">
3636

3737
```console
38-
git clone https://github.com/xgmsx/go-url-shortener-ddd
39-
cd go-url-shortener-ddd
38+
git clone https://github.com/xgmsx/go-tiny-url
39+
cd go-tiny-url
4040
cp ./configs/.env_example ./configs/.env
4141
cd ./configs/.env_localhost_example ./configs/.env_localhost
4242
```
@@ -45,61 +45,56 @@ cd ./configs/.env_localhost_example ./configs/.env_localhost
4545

4646
## Quick start
4747

48-
#### Запуск сервиса с зависимостями (postgres, redis, kafka, jaeger, prometheus, grafana) в Docker:
48+
#### Установка инструментов для разработки
4949

50-
<div class="termy">
50+
```shell
51+
make install
52+
```
5153

52-
```console
53-
$ docker compose up -d --build
54+
Проверка работы установленных инструментов:
5455

55-
✔ Network go-url-shortener-ddd_default Created 0.1s
56-
✔ Container go-url-shortener-ddd-app-1 Started 0.4s
57-
✔ Container go-url-shortener-ddd-postgres-1 Started 0.2s
58-
✔ Container go-url-shortener-ddd-redis-1 Started 0.2s
59-
✔ Container go-url-shortener-ddd-redis-ui-1 Started 0.2s
60-
✔ Container go-url-shortener-ddd-kafka-1 Started 0.2s
61-
✔ Container go-url-shortener-ddd-kafka-init-1 Started
62-
✔ Container go-url-shortener-ddd-kafka-ui-1 Started 0.3s
56+
```shell
57+
make generate
58+
make fmt
59+
make lint
6360
```
6461

65-
</div>
66-
67-
#### Локальный запуск сервиса в Linux и MacOS:
62+
#### Запуск сервиса с зависимостями (postgres, redis, kafka, jaeger, prometheus, grafana) в Docker
6863

6964
<div class="termy">
7065

7166
```console
72-
$ export $(grep -v '^#' ./configs/.env_localhost | xargs) && go run ./cmd/app
67+
$ docker compose up -d --build
7368

74-
2025/01/01 12:00:00 maxprocs: Leaving GOMAXPROCS=8: CPU quota undefined
75-
12:00:00 INF Logger initialized
76-
12:00:00 INF App starting...
77-
12:00:00 INF Sentry is disabled
78-
12:00:00 INF Tracer is disabled
79-
12:00:00 INF HTTP server started on port: 8000
80-
12:00:00 INF gRPC server started on port: 50051
81-
12:00:00 INF Kafka consumer started
82-
12:00:00 INF App started
69+
✔ Network go-tiny-url_default Created 0.1s
70+
✔ Container go-tiny-url-app-1 Started 0.4s
71+
✔ Container go-tiny-url-postgres-1 Started 0.2s
72+
✔ Container go-tiny-url-redis-1 Started 0.2s
73+
... 0.3s
8374
```
8475

8576
</div>
8677

87-
#### Локальный запуск сервиса в Windows Git Bash:
78+
#### Локальный запуск сервиса в Linux и MacOS
79+
80+
<div class="termy">
8881

8982
```console
83+
# Linux, MacOS
84+
$ export $(grep -v '^#' ./configs/.env_localhost | xargs) && go run ./cmd/app
85+
86+
# Windows Git Bash
9087
$ env $(grep -v '^#' ./configs/.env_localhost | xargs) go run ./cmd/app
9188

9289
2025/01/01 12:00:00 maxprocs: Leaving GOMAXPROCS=8: CPU quota undefined
9390
12:00:00 INF Logger initialized
9491
12:00:00 INF App starting...
95-
12:00:00 INF Sentry is disabled
96-
12:00:00 INF Tracer is disabled
97-
12:00:00 INF HTTP server started on port: 8000
98-
12:00:00 INF gRPC server started on port: 50051
99-
12:00:00 INF Kafka consumer started
92+
...
10093
12:00:00 INF App started
10194
```
10295

96+
</div>
97+
10398
## Usage
10499

105100
#### Migrations
@@ -187,7 +182,7 @@ $ grpcurl -d '{"url": "https://google.com"}' -plaintext localhost:50051 shortene
187182

188183
Получение полной ссылки:
189184
```shell
190-
$ grpcurl -d '{"alias": "IFIYr0OGRKeqF9jPUIbwww"}' -plaintext localhost:50051 shortener_v1.Shortener/GetLink
185+
$ grpcurl -d '{"alias": "IFIYr0OGRKeqF9jPUIbwww"}' -plaintext localhost:50051 shortener_v1.Shortener/FetchLink
191186

192187
# {"url": "https://google.com", "alias": "IFIYr0OGRKeqF9jPUIbwww", "expired_at": "2025-01-02T12:00:00.000000000Z"}
193188
```

cmd/app/contract.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"context"
5+
6+
"github.com/xgmsx/go-tiny-url/internal/config"
7+
)
8+
9+
//go:generate mockgen -source=contract.go -destination=mocks/contract.go
10+
11+
type appRunner interface {
12+
Run(ctx context.Context, c *config.Config) error
13+
}
14+
15+
type configLoader interface {
16+
Load(ctx context.Context) (*config.Config, error)
17+
}

cmd/app/main.go

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,47 @@ import (
66
"github.com/rs/zerolog/log"
77
_ "go.uber.org/automaxprocs"
88

9-
"github.com/xgmsx/go-url-shortener-ddd/internal/app"
10-
"github.com/xgmsx/go-url-shortener-ddd/internal/config"
11-
"github.com/xgmsx/go-url-shortener-ddd/pkg/logger"
12-
"github.com/xgmsx/go-url-shortener-ddd/pkg/observability/otel"
13-
"github.com/xgmsx/go-url-shortener-ddd/pkg/observability/sentry"
9+
"github.com/xgmsx/go-tiny-url/internal/app"
10+
"github.com/xgmsx/go-tiny-url/internal/config"
11+
"github.com/xgmsx/go-tiny-url/pkg/logger"
12+
"github.com/xgmsx/go-tiny-url/pkg/observability/otel"
13+
"github.com/xgmsx/go-tiny-url/pkg/observability/sentry"
1414
)
1515

16-
func run(run func(context.Context, *config.Config) error) {
17-
c, err := config.New()
16+
var (
17+
cl configLoader = config.New()
18+
ar appRunner = app.New()
19+
)
20+
21+
func run(ctx context.Context, cl configLoader, ar appRunner) error {
22+
c, err := cl.Load(ctx)
1823
if err != nil {
19-
log.Fatal().Err(err).Msg("config.New")
24+
log.Error().Err(err).Msg("config.New")
25+
return err
2026
}
2127

2228
logger.Init(c.Logger, c.App.Name, c.App.Version)
2329
log.Info().Msg("App starting...")
2430
defer log.Info().Msg("App stopped")
2531

26-
ctx, cancel := context.WithCancel(context.Background())
27-
defer cancel()
28-
29-
err = sentry.Init(c.Sentry, c.App.Name, c.App.Version, c.App.Env)
30-
if err != nil {
31-
log.Error().Err(err).Msg("sentry.Init")
32-
}
32+
sentry.Init(c.Sentry, c.App.Name, c.App.Version, c.App.Env)
3333
defer sentry.Close()
3434

35-
err = otel.Init(ctx, c.Otel, c.App.Name, c.App.Version)
36-
if err != nil {
37-
log.Error().Err(err).Msg("otel.Init")
38-
}
35+
otel.Init(ctx, c.Otel, c.App.Name, c.App.Version)
3936
defer otel.Close()
4037

41-
err = run(ctx, c)
38+
err = ar.Run(ctx, c)
4239
if err != nil {
4340
log.Error().Err(err).Msg("app.Run")
4441
}
42+
return err
4543
}
4644

4745
func main() {
48-
run(app.Run)
46+
ctx, cancel := context.WithCancel(context.Background())
47+
defer cancel()
48+
49+
if err := run(ctx, cl, ar); err != nil {
50+
panic(err)
51+
}
4952
}

0 commit comments

Comments
 (0)