Skip to content

Commit 06902ec

Browse files
authored
Merge pull request #6 from skantay/new
LGTM
2 parents 359be53 + 5ccd09e commit 06902ec

File tree

35 files changed

+440
-54
lines changed

35 files changed

+440
-54
lines changed

.github/workflows/github-actions.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
- main
8+
- dev
9+
pull_request:
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- uses: actions/checkout@v4
17+
18+
- name: Setup Go
19+
uses: actions/setup-go@v5
20+
with:
21+
go-version: '1.20'
22+
23+
- name: Install dependencies
24+
run: go get -v -t -d ./...
25+
26+
- name: Run tests
27+
run: go test -v ./...
28+
29+
- name: Run golangci-lint
30+
uses: golangci/golangci-lint-action@v4
31+
with:
32+
version: v1.54
33+
args: --timeout=30m

.gitignore

Whitespace-only changes.

Dockerfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM golang:alpine as modules
2+
COPY go.mod go.sum /modules/
3+
WORKDIR /modules
4+
RUN go mod download
5+
6+
7+
FROM golang:alpine as builder
8+
COPY --from=modules /go/pkg /go/pkg
9+
COPY . /app
10+
WORKDIR /app
11+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
12+
go build -tags migrate -o /bin/app ./cmd/app
13+
14+
FROM scratch
15+
COPY --from=builder /app/config /config
16+
COPY --from=builder /app/migrations /migrations
17+
COPY --from=builder /bin/app /app
18+
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
19+
CMD ["/app"]

Makefile

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
include .env
2+
export
3+
4+
compose-up: ### Run docker-compose
5+
docker-compose up --build -d && docker-compose logs -f
6+
.PHONY: compose-up
7+
8+
compose-down: ### Down docker-compose
9+
docker-compose down --remove-orphans
10+
.PHONY: compose-down
11+
12+
linter-golangci: ### check by golangci linter
13+
golangci-lint run
14+
.PHONY: linter-golangci
15+
16+
migrate-create: ### create new migration
17+
migrate create -ext sql -dir migrations '$(PROJECT_NAME)'
18+
.PHONY: migrate-create
19+
20+
migrate-up: ### migration up
21+
migrate -path migrations -database '$(PG_URL_LOCALHOST)?sslmode=disable' up
22+
.PHONY: migrate-up
23+
24+
migrate-down: ### migration down
25+
echo "y" | migrate -path migrations -database '$(PG_URL_LOCALHOST)?sslmode=disable' down
26+
.PHONY: migrate-down
27+
28+
test: ### run test
29+
go test -v ./...
30+
31+
coverage-html: ### run test with coverage and open html report
32+
go test -coverprofile=cvr.out ./...
33+
go tool cover -html=cvr.out
34+
rm cvr.out
35+
.PHONY: coverage-html
36+
37+
coverage: ### run test with coverage
38+
go test -coverprofile=cvr.out ./...
39+
go tool cover -func=cvr.out
40+
rm cvr.out
41+
.PHONY: coverage

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1-
# web-1-clean
1+
# {PROJECT NAME}
22

3-
## On development stage
3+
- description
4+
- used technologies
5+
- a little more precise description
6+
7+
# Getting Started
8+
9+
- instruction on how to start
10+
11+
# Usage
12+
[ba](https://en.wikipedia.org/wiki/Brainfuck)
13+
- list/show available commands
14+
15+
# Docs
16+
17+
- swagger
18+
- erm
19+
- etc...

cmd/.keep

Whitespace-only changes.

cmd/app/main.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

config/.keep

Whitespace-only changes.

config/config.example.yaml

Whitespace-only changes.

config/config.yaml

Whitespace-only changes.

docker-compose.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: '3'
2+
services:
3+
app:
4+
container_name: "{$PROJECT_NAME}"
5+
build: .
6+
env_file:
7+
- .env
8+
restart: always

docs/.keep

Whitespace-only changes.

go.mod

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1-
module github.com/skantay/service
1+
module github.com/skantay/rest-template
22

33
go 1.20
4+
5+
require github.com/jackc/pgconn v1.14.3
6+
7+
require (
8+
github.com/jackc/puddle/v2 v2.2.1 // indirect
9+
golang.org/x/sync v0.6.0 // indirect
10+
)
11+
12+
require (
13+
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
14+
github.com/jackc/pgio v1.0.0 // indirect
15+
github.com/jackc/pgpassfile v1.0.0 // indirect
16+
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
17+
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
18+
github.com/jackc/pgx/v5 v5.5.5
19+
golang.org/x/crypto v0.21.0 // indirect
20+
golang.org/x/text v0.14.0 // indirect
21+
)

go.sum

Lines changed: 186 additions & 0 deletions
Large diffs are not rendered by default.

internal/adapter/repository/.keep

Whitespace-only changes.

internal/adapter/webapi/.keep

Whitespace-only changes.

internal/app/.keep

Whitespace-only changes.

internal/apps/README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

internal/apps/app/app.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

internal/controller/.keep

Whitespace-only changes.

internal/controllers/README.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

internal/controllers/amqp/v1/amqp.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

internal/controllers/grpc/v1/grpc.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

internal/controllers/http/v1/http.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

internal/entities/entity.go

Lines changed: 0 additions & 3 deletions
This file was deleted.

internal/entity/.keep

Whitespace-only changes.

internal/infrastructure/README.md

Lines changed: 0 additions & 6 deletions
This file was deleted.

internal/infrastructure/api_calls/v1/v1.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

internal/infrastructure/database/postgres/postgres.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

internal/service/README.md

Lines changed: 0 additions & 6 deletions
This file was deleted.

internal/usecase/.keep

Whitespace-only changes.

internal/usecase/README.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

migrations/.keep

Whitespace-only changes.

pkg/httpserver/server.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package httpserver
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"time"
7+
)
8+
9+
type Server struct {
10+
server *http.Server
11+
notify chan error
12+
shutdownTimeout time.Duration
13+
}
14+
15+
func New(handler http.Handler, timeout map[string]time.Duration, addr string) *Server {
16+
httpServer := &http.Server{
17+
Handler: handler,
18+
ReadTimeout: timeout["ReadTimeout"],
19+
WriteTimeout: timeout["WriteTimeout"],
20+
IdleTimeout: timeout["IdleTimeout"],
21+
Addr: addr,
22+
}
23+
24+
s := &Server{
25+
server: httpServer,
26+
notify: make(chan error, 1),
27+
shutdownTimeout: timeout["shutdownTimeout"],
28+
}
29+
30+
s.start()
31+
32+
return s
33+
}
34+
35+
func (s *Server) start() {
36+
go func() {
37+
s.notify <- s.server.ListenAndServe()
38+
close(s.notify)
39+
}()
40+
}
41+
42+
func (s *Server) Notify() <-chan error {
43+
return s.notify
44+
}
45+
46+
func (s *Server) Shutdown() error {
47+
ctx, cancel := context.WithTimeout(context.Background(), s.shutdownTimeout)
48+
defer cancel()
49+
50+
return s.server.Shutdown(ctx)
51+
}

pkg/postgres/postgres.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package postgres
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"time"
8+
9+
"github.com/jackc/pgx/v5"
10+
"github.com/jackc/pgx/v5/pgconn"
11+
"github.com/jackc/pgx/v5/pgxpool"
12+
)
13+
14+
type PgxPool interface {
15+
Close()
16+
Acquire(ctx context.Context) (*pgxpool.Conn, error)
17+
Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)
18+
Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
19+
QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
20+
SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults
21+
Begin(ctx context.Context) (pgx.Tx, error)
22+
BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error)
23+
CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)
24+
Ping(ctx context.Context) error
25+
}
26+
27+
type Postgres struct {
28+
MaxPoolSize int
29+
ConnAttempts int
30+
ConnTimeout time.Duration
31+
32+
Pool PgxPool
33+
}
34+
35+
func New(url string, pg *Postgres) (*Postgres, error) {
36+
poolConfig, err := pgxpool.ParseConfig(url)
37+
if err != nil {
38+
return nil, fmt.Errorf("failed to parse config: %w", err)
39+
}
40+
41+
poolConfig.MaxConns = int32(pg.MaxPoolSize)
42+
43+
for pg.ConnAttempts > 0 {
44+
pg.Pool, err = pgxpool.NewWithConfig(context.Background(), poolConfig)
45+
if err == nil {
46+
break
47+
}
48+
49+
log.Printf("trying to connect to postgres, attempts left: %d", pg.ConnAttempts)
50+
time.Sleep(pg.ConnTimeout)
51+
pg.ConnAttempts--
52+
}
53+
54+
if err != nil {
55+
return nil, fmt.Errorf("failed to connect to postgres: %w", err)
56+
}
57+
58+
return pg, nil
59+
}
60+
61+
func (p *Postgres) Close() {
62+
if p.Pool != nil {
63+
p.Pool.Close()
64+
}
65+
}

0 commit comments

Comments
 (0)