Skip to content

Commit

Permalink
chat fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Qaleka committed Dec 17, 2024
1 parent c09c789 commit fc1f899
Show file tree
Hide file tree
Showing 11 changed files with 875 additions and 58 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name: Build, Lint, Test, and Push Services
name: Build, Test, and Push Services

on:
push:
branches:
- dev

jobs:
build-lint-test-and-push:
build-test-and-push:
runs-on: ubuntu-latest

steps:
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ services:
image: minio/minio
ports:
- "9000:9000"
- "8070:9001"
- "9001:9001"
environment:
MINIO_ROOT_USER: ${MINIO_ROOT_USER}
MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD}
Expand Down
1 change: 0 additions & 1 deletion domain/ads.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ type AdRepository interface {
GetAllPlaces(ctx context.Context, filter AdFilter, userId string) ([]GetAllAdsResponse, error)
GetPlaceById(ctx context.Context, adId string) (GetAllAdsResponse, error)
CreatePlace(ctx context.Context, ad *Ad, newAd CreateAdRequest, userId string) error
SavePlace(ctx context.Context, ad *Ad) error
UpdatePlace(ctx context.Context, ad *Ad, adId string, userId string, updatedAd UpdateAdRequest) error
DeletePlace(ctx context.Context, adId string, userId string) error
GetPlacesPerCity(ctx context.Context, city string) ([]GetAllAdsResponse, error)
Expand Down
6 changes: 6 additions & 0 deletions internal/chat/controller/chat_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ func (cc *ChatHandler) SetConnection(w http.ResponseWriter, r *http.Request) {
return
}

if _, ok := w.(http.Hijacker); !ok {
http.Error(w, "WebSocket upgrade not supported", http.StatusInternalServerError)
logger.AccessLogger.Error("ResponseWriter does not support Hijacker")
return
}

socket, err := upgrader.Upgrade(w, r, nil)
if err != nil {
cc.handleError(w, errors.New("failed to upgrade connection"), requestID)
Expand Down
9 changes: 9 additions & 0 deletions internal/service/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"2024_2_FIGHT-CLUB/internal/service/dsn"
"2024_2_FIGHT-CLUB/internal/service/images"
"2024_2_FIGHT-CLUB/microservices/ads_service/controller/gen"
"bufio"
"context"
"errors"
"fmt"
Expand All @@ -17,6 +18,7 @@ import (
"gorm.io/driver/postgres"
"gorm.io/gorm"
"log"
"net"
"net/http"
"os"
"runtime/debug"
Expand Down Expand Up @@ -168,6 +170,13 @@ func (w *responseWriterWrapper) WriteHeader(statusCode int) {
}
}

func (w *responseWriterWrapper) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if hijacker, ok := w.ResponseWriter.(http.Hijacker); ok {
return hijacker.Hijack()
}
return nil, nil, errors.New("Hijack not supported")
}

func RecoverWrap(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
wrappedWriter := &responseWriterWrapper{ResponseWriter: w}
Expand Down
3 changes: 2 additions & 1 deletion microservices/ads_service/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"net"
"net/http"
"os"
"time"
)

func main() {
Expand Down Expand Up @@ -61,7 +62,7 @@ func main() {
sessionService := session.NewSessionService(redisStore)
adsUseCase := adUseCase.NewAdUseCase(adsRepository, minioService)
adsServer := grpcAd.NewGrpcAdHandler(sessionService, adsUseCase, jwtToken)
adsUseCase.StartPriorityResetWorker(ctx)
adsUseCase.StartPriorityResetWorker(ctx, 24*time.Hour)
grpcServer := grpc.NewServer(
grpc.UnaryInterceptor(middleware.ChainUnaryInterceptors(
middleware.RecoveryInterceptor, // интерсептор для обработки паники
Expand Down
41 changes: 33 additions & 8 deletions microservices/ads_service/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,19 @@ func (m *MockServiceSession) GetSession(r *http.Request) (*sessions.Session, err
}

type MockAdUseCase struct {
MockGetAllPlaces func(ctx context.Context, filter domain.AdFilter) ([]domain.GetAllAdsResponse, error)
MockGetOnePlace func(ctx context.Context, adId string, isAuthorized bool) (domain.GetAllAdsResponse, error)
MockCreatePlace func(ctx context.Context, place *domain.Ad, fileHeader [][]byte, newPlace domain.CreateAdRequest, userId string) error
MockUpdatePlace func(ctx context.Context, place *domain.Ad, adId string, userId string, fileHeader [][]byte, updatedPlace domain.UpdateAdRequest) error
MockDeletePlace func(ctx context.Context, adId string, userId string) error
MockGetPlacesPerCity func(ctx context.Context, city string) ([]domain.GetAllAdsResponse, error)
MockGetUserPlaces func(ctx context.Context, userId string) ([]domain.GetAllAdsResponse, error)
MockDeleteAdImage func(ctx context.Context, adId string, imageId string, userId string) error
MockGetAllPlaces func(ctx context.Context, filter domain.AdFilter) ([]domain.GetAllAdsResponse, error)
MockGetOnePlace func(ctx context.Context, adId string, isAuthorized bool) (domain.GetAllAdsResponse, error)
MockCreatePlace func(ctx context.Context, place *domain.Ad, fileHeader [][]byte, newPlace domain.CreateAdRequest, userId string) error
MockUpdatePlace func(ctx context.Context, place *domain.Ad, adId string, userId string, fileHeader [][]byte, updatedPlace domain.UpdateAdRequest) error
MockDeletePlace func(ctx context.Context, adId string, userId string) error
MockGetPlacesPerCity func(ctx context.Context, city string) ([]domain.GetAllAdsResponse, error)
MockGetUserPlaces func(ctx context.Context, userId string) ([]domain.GetAllAdsResponse, error)
MockDeleteAdImage func(ctx context.Context, adId string, imageId string, userId string) error
MockAddToFavorites func(ctx context.Context, adId string, userId string) error
MockDeleteFromFavorites func(ctx context.Context, adId string, userId string) error
MockGetUserFavorites func(ctx context.Context, userId string) ([]domain.GetAllAdsResponse, error)
MockUpdatePriority func(ctx context.Context, adId string, userId string, amount int) error
MockStartPriorityResetWorker func(ctx context.Context)
}

func (m *MockAdUseCase) DeleteAdImage(ctx context.Context, adId string, imageId string, userId string) error {
Expand Down Expand Up @@ -98,6 +103,26 @@ func (m *MockAdUseCase) GetUserPlaces(ctx context.Context, userId string) ([]dom
return m.MockGetUserPlaces(ctx, userId)
}

func (m *MockAdUseCase) AddToFavorites(ctx context.Context, adId string, userId string) error {
return m.MockAddToFavorites(ctx, adId, userId)
}

func (m *MockAdUseCase) DeleteFromFavorites(ctx context.Context, adId string, userId string) error {
return m.MockDeleteFromFavorites(ctx, adId, userId)
}

func (m *MockAdUseCase) GetUserFavorites(ctx context.Context, userId string) ([]domain.GetAllAdsResponse, error) {
return m.MockGetUserFavorites(ctx, userId)
}

func (m *MockAdUseCase) UpdatePriority(ctx context.Context, adId string, userId string, amount int) error {
return m.MockUpdatePriority(ctx, adId, userId, amount)
}

func (m *MockAdUseCase) StartPriorityResetWorker(ctx context.Context) {
m.MockStartPriorityResetWorker(ctx)
}

type MockAdRepository struct {
MockGetAllPlaces func(ctx context.Context, filter domain.AdFilter, userId string) ([]domain.GetAllAdsResponse, error)
MockGetPlaceById func(ctx context.Context, adId string) (domain.GetAllAdsResponse, error)
Expand Down
22 changes: 0 additions & 22 deletions microservices/ads_service/repository/ads_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,28 +372,6 @@ func (r *adRepository) CreatePlace(ctx context.Context, ad *domain.Ad, newAd dom
return nil
}

func (r *adRepository) SavePlace(ctx context.Context, ad *domain.Ad) error {
start := time.Now()
requestID := middleware.GetRequestID(ctx)
logger.DBLogger.Info("SavePlace called", zap.String("adId", ad.UUID), zap.String("request_id", requestID))
var err error
defer func() {
if err != nil {
metrics.RepoErrorsTotal.WithLabelValues("SavePlace", "error", err.Error()).Inc()
} else {
metrics.RepoRequestTotal.WithLabelValues("SavePlace", "success").Inc()
}
duration := time.Since(start).Seconds()
metrics.RepoRequestDuration.WithLabelValues("SavePlace").Observe(duration)
}()
if err := r.db.Save(ad).Error; err != nil {
logger.DBLogger.Error("Error saving place", zap.String("adId", ad.UUID), zap.String("request_id", requestID), zap.Error(err))
return errors.New("error saving place")
}
logger.DBLogger.Info("Successfully saved place", zap.String("adId", ad.UUID), zap.String("request_id", requestID))
return nil
}

func (r *adRepository) UpdatePlace(ctx context.Context, ad *domain.Ad, adId string, userId string, updatedPlace domain.UpdateAdRequest) error {
start := time.Now()
requestID := middleware.GetRequestID(ctx)
Expand Down
Loading

0 comments on commit fc1f899

Please sign in to comment.