Skip to content

Commit

Permalink
Merge pull request #50 from go-park-mail-ru/dev
Browse files Browse the repository at this point in the history
РК3
  • Loading branch information
mevain authored Nov 27, 2024
2 parents 80a40a0 + b611abc commit 5390cf3
Show file tree
Hide file tree
Showing 134 changed files with 83,806 additions and 4,468 deletions.
2 changes: 0 additions & 2 deletions .env

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
go.mod
2024-2-Zdes-budet-nazvanie-UykwHnIE.crt
data/
.env
20 changes: 11 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
PKG=./...
MOCKGEN=mockgen
COVERAGE_FILE=coverage.out
MOCK_SRC_PLACES=internal/pkg/places/interfaces.go
MOCK_DST_PLACES=internal/pkg/places/mocks/mock.go
MOCK_SRC_PLACES=internal/pkg/attractions/interfaces.go
MOCK_DST_PLACES=internal/pkg/attractions/mocks/mock.go
MOCK_SRC_USER=internal/pkg/user/interfaces.go
MOCK_DST_USER=internal/pkg/user/mocks/mock.go
MOCK_SRC_TRIPS=internal/pkg/trips/interfaces.go
MOCK_DST_TRIPS=internal/pkg/trips/mocks/mock_trips.go
PACKAGE_NAME=mocks
PACKAGE_NAME_USER=user
PACKAGE_NAME_PLACES=places

PACKAGE_NAME_PLACES=attractions
PACKAGE_NAME_trips=

all: test

mocks:
$(MOCKGEN) -source=$(MOCK_SRC_PLACES) -destination=$(MOCK_DST_PLACES) -package=$(PACKAGE_NAME_PLACES)
$(MOCKGEN) -source=$(MOCK_SRC_USER) -destination=$(MOCK_DST_USER) -package=$(PACKAGE_NAME_USER)
$(MOCKGEN) -source=$(MOCK_SRC_PLACES) -destination=$(MOCK_DST_PLACES) -package=$(PACKAGE_NAME)
$(MOCKGEN) -source=$(MOCK_SRC_USER) -destination=$(MOCK_DST_USER) -package=$(PACKAGE_NAME)
$(MOCKGEN) -source=$(MOCK_SRC_TRIPS) -destination=$(MOCK_DST_TRIPS) -package=$(PACKAGE_NAME)

test: mocks
go test $(PKG) -coverprofile=$(COVERAGE_FILE)
Expand All @@ -38,6 +42,4 @@ run: build_

.PHONY: lint
lint:
golangci-lint run --config=.golangci.yaml


golangci-lint run --config=.golangci.yaml
11 changes: 11 additions & 0 deletions build/attraction.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM golang:1.23.1-alpine AS builder
COPY . /github.com/go-park-mail-ru/2024_2_ThereWillBeName/attractions
WORKDIR /github.com/go-park-mail-ru/2024_2_ThereWillBeName/attractions
RUN go mod download
RUN go clean --modcache
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -o ./.bin ./cmd/attractions/main.go
FROM scratch AS runner
WORKDIR /build
COPY --from=builder /github.com/go-park-mail-ru/2024_2_ThereWillBeName/attractions/.bin .
EXPOSE 8081
ENTRYPOINT ["./.bin"]
11 changes: 11 additions & 0 deletions build/gateway.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM golang:1.23.1-alpine AS builder
COPY . /github.com/go-park-mail-ru/2024_2_ThereWillBeName/gateway
WORKDIR /github.com/go-park-mail-ru/2024_2_ThereWillBeName/gateway
RUN go mod download
RUN go clean --modcache
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -o ./.bin ./cmd/gateway/main.go
FROM scratch AS runner
WORKDIR /build
COPY --from=builder /github.com/go-park-mail-ru/2024_2_ThereWillBeName/gateway/.bin .
EXPOSE 8080
ENTRYPOINT ["./.bin"]
11 changes: 11 additions & 0 deletions build/survey.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM golang:1.23.1-alpine AS builder
COPY . /github.com/go-park-mail-ru/2024_2_ThereWillBeName/survey
WORKDIR /github.com/go-park-mail-ru/2024_2_ThereWillBeName/survey
RUN go mod download
RUN go clean --modcache
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -o ./.bin ./cmd/survey/main.go
FROM scratch AS runner
WORKDIR /build
COPY --from=builder /github.com/go-park-mail-ru/2024_2_ThereWillBeName/survey/.bin .
EXPOSE 50054
ENTRYPOINT ["./.bin"]
11 changes: 11 additions & 0 deletions build/trips.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM golang:1.23.1-alpine AS builder
COPY . /github.com/go-park-mail-ru/2024_2_ThereWillBeName/trips
WORKDIR /github.com/go-park-mail-ru/2024_2_ThereWillBeName/trips
RUN go mod download
RUN go clean --modcache
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -o ./.bin ./cmd/trips/main.go
FROM scratch AS runner
WORKDIR /build
COPY --from=builder /github.com/go-park-mail-ru/2024_2_ThereWillBeName/trips/.bin .
EXPOSE 50053
ENTRYPOINT ["./.bin"]
11 changes: 11 additions & 0 deletions build/users.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM golang:1.23.1-alpine AS builder
COPY . /github.com/go-park-mail-ru/2024_2_ThereWillBeName/users
WORKDIR /github.com/go-park-mail-ru/2024_2_ThereWillBeName/users
RUN go mod download
RUN go clean --modcache
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -o ./.bin ./cmd/users/main.go
FROM scratch AS runner
WORKDIR /build
COPY --from=builder /github.com/go-park-mail-ru/2024_2_ThereWillBeName/users/.bin .
EXPOSE 50052
ENTRYPOINT ["./.bin"]
125 changes: 125 additions & 0 deletions cmd/attractions/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package main

import (
"2024_2_ThereWillBeName/internal/models"
grpcAttractions "2024_2_ThereWillBeName/internal/pkg/attractions/delivery/grpc"
genPlaces "2024_2_ThereWillBeName/internal/pkg/attractions/delivery/grpc/gen"
placeRepo "2024_2_ThereWillBeName/internal/pkg/attractions/repo"
placeUsecase "2024_2_ThereWillBeName/internal/pkg/attractions/usecase"
grpcCategories "2024_2_ThereWillBeName/internal/pkg/categories/delivery/grpc"
genCategories "2024_2_ThereWillBeName/internal/pkg/categories/delivery/grpc/gen"
categoriesRepo "2024_2_ThereWillBeName/internal/pkg/categories/repo"
categoriesUsecase "2024_2_ThereWillBeName/internal/pkg/categories/usecase"
grpcCities "2024_2_ThereWillBeName/internal/pkg/cities/delivery/grpc"
genCities "2024_2_ThereWillBeName/internal/pkg/cities/delivery/grpc/gen"
citiesRepo "2024_2_ThereWillBeName/internal/pkg/cities/repo"
citiesUsecase "2024_2_ThereWillBeName/internal/pkg/cities/usecase"
"2024_2_ThereWillBeName/internal/pkg/dblogger"
"2024_2_ThereWillBeName/internal/pkg/logger"
grpcReviews "2024_2_ThereWillBeName/internal/pkg/reviews/delivery/grpc"
genReviews "2024_2_ThereWillBeName/internal/pkg/reviews/delivery/grpc/gen"
reviewRepo "2024_2_ThereWillBeName/internal/pkg/reviews/repo"
reviewUsecase "2024_2_ThereWillBeName/internal/pkg/reviews/usecase"
grpcSearch "2024_2_ThereWillBeName/internal/pkg/search/delivery/grpc"
genSearch "2024_2_ThereWillBeName/internal/pkg/search/delivery/grpc/gen"
searchRepo "2024_2_ThereWillBeName/internal/pkg/search/repo"
searchUsecase "2024_2_ThereWillBeName/internal/pkg/search/usecase"
"database/sql"
"flag"
"log"
"log/slog"
"net"
"os"
"os/signal"
"strconv"
"syscall"

_ "github.com/lib/pq"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)

func main() {
var cfg models.ConfigGrpc
flag.IntVar(&cfg.Port, "grpc-port", 50051, "gRPC server port")
flag.StringVar(&cfg.ConnStr, "connStr", "host=tripdb port=5432 user=service password=test dbname=trip sslmode=disable", "PostgreSQL connection string")
flag.Parse()

logger := setupLogger()

db, err := sql.Open("postgres", cfg.ConnStr)
if err != nil {
log.Fatalf("failed to connect to database: %v", err)
}
defer db.Close()

wrappedDB := dblogger.NewDB(db, logger)

reviewsRepo := reviewRepo.NewReviewRepository(wrappedDB)
reviewUsecase := reviewUsecase.NewReviewsUsecase(reviewsRepo)
placeRepo := placeRepo.NewPLaceRepository(wrappedDB)
placeUsecase := placeUsecase.NewPlaceUsecase(placeRepo)
citiesRepo := citiesRepo.NewCitiesRepository(wrappedDB)
citiesUsecase := citiesUsecase.NewCitiesUsecase(citiesRepo)
categoriesRepo := categoriesRepo.NewCategoriesRepo(wrappedDB)
categoriesUsecase := categoriesUsecase.NewCategoriesUsecase(categoriesRepo)
searchRepo := searchRepo.NewSearchRepository(wrappedDB)
searchUsecase := searchUsecase.NewSearchUsecase(searchRepo)

grpcAttractionsServer := grpc.NewServer()

attractionsHandler := grpcAttractions.NewGrpcAttractionsHandler(placeUsecase)
genPlaces.RegisterAttractionsServer(grpcAttractionsServer, attractionsHandler)

citiesHandler := grpcCities.NewGrpcCitiesHandler(citiesUsecase)
genCities.RegisterCitiesServer(grpcAttractionsServer, citiesHandler)

reviewsHandler := grpcReviews.NewGrpcReviewsHandler(reviewUsecase)
genReviews.RegisterReviewsServer(grpcAttractionsServer, reviewsHandler)

categoriesHandler := grpcCategories.NewGrpcCategoriesHandler(categoriesUsecase)
genCategories.RegisterCategoriesServer(grpcAttractionsServer, categoriesHandler)

searchHandler := grpcSearch.NewGrpcSearchHandler(searchUsecase, logger)
genSearch.RegisterSearchServer(grpcAttractionsServer, searchHandler)

reflection.Register(grpcAttractionsServer)

go func() {
listener, err := net.Listen("tcp", ":8081")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
log.Printf("gRPC server listening on :%d", cfg.Port)
if err := grpcAttractionsServer.Serve(listener); err != nil {
log.Fatalf("failed to serve gRPC: %v", err)
}
}()

stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-stop

log.Println("Shutting down gRPC server...")
grpcAttractionsServer.GracefulStop()
log.Println("gRPC server gracefully stopped")
}

func setupLogger() *slog.Logger {

levelEnv := os.Getenv("LOG_LEVEL")
logLevel := slog.LevelDebug
if level, err := strconv.Atoi(levelEnv); err == nil {
logLevel = slog.Level(level)
}

opts := logger.PrettyHandlerOptions{
SlogOpts: slog.HandlerOptions{
Level: logLevel,
},
}

handler := logger.NewPrettyHandler(os.Stdout, opts)

return slog.New(handler)
}
Loading

0 comments on commit 5390cf3

Please sign in to comment.