Skip to content

Commit

Permalink
microservices added (#45)
Browse files Browse the repository at this point in the history
* added grpc to delivery for trips

* microservices added

* changed main

* attractions service fixed

* userService

* gRPC for users

* fixing

* added auth routes

* services fixed

* returned token in cookie

* fixed login and search

* scheme changed

* made structure for survey's microservice

* fixed names

* added image path to searh response

* fix

* fixed getPlace

* proto added

* http handler for surveys

* made http handlers for surveys

* grpc server added

* started repo

* started usecase for surveys

* finished repo

* made usecase for surveys

* grpc server added

* SCHEME BD CHANGE

* SCHEME BD CHANGE

* SCHEME BD CHANGE

* fixed mistakes with error handling

* ne znay

* ne znay

* ne znay

* ne znay

* fixed authorization

* fixed trip queries

* added field description to db

* added global search to microservices

* fixed deleting review

---------

Co-authored-by: mevain <xen.bardikina@yandex.ru>
Co-authored-by: AnnHarvard <dana.shakleina@yandex.ru>
  • Loading branch information
3 people authored Nov 25, 2024
1 parent 87118ed commit a07f96e
Show file tree
Hide file tree
Showing 83 changed files with 11,288 additions and 1,786 deletions.
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"]
122 changes: 122 additions & 0 deletions cmd/attractions/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
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/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()

reviewsRepo := reviewRepo.NewReviewRepository(db)
reviewUsecase := reviewUsecase.NewReviewsUsecase(reviewsRepo)
placeRepo := placeRepo.NewPLaceRepository(db)
placeUsecase := placeUsecase.NewPlaceUsecase(placeRepo)
citiesRepo := citiesRepo.NewCitiesRepository(db)
citiesUsecase := citiesUsecase.NewCitiesUsecase(citiesRepo)
categoriesRepo := categoriesRepo.NewCategoriesRepo(db)
categoriesUsecase := categoriesUsecase.NewCategoriesUsecase(categoriesRepo)
searchRepo := searchRepo.NewSearchRepository(db)
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 a07f96e

Please sign in to comment.