Merge branch 'quizLogo' into 'staging'

Перенос обработчика мини партнёрки /logo в сервис, который отвечает за обработку действий респондентов

See merge request backend/quiz/answerer!3
This commit is contained in:
Mikhail 2024-06-02 13:44:24 +00:00
commit b02599e180
3 changed files with 68 additions and 9 deletions

@ -18,6 +18,7 @@ import (
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/healthchecks"
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/middleware"
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/utils"
)
type App struct {
@ -58,6 +59,9 @@ type Options struct {
RedisHost string `env:"REDIS_HOST"`
RedisPassword string `env:"REDIS_PASSWORD"`
RedisDB uint64 `env:"REDIS_DB"`
RedirectURL string `env:"REDIRECT_URL" default:"https://squiz.pena.digital"`
PubKey string `env:"PUBLIC_KEY"`
PrivKey string `env:"PRIVATE_KEY"`
}
func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.CommonApp, error) {
@ -108,7 +112,6 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co
return nil, err
}
zapLogger.Info("config", zap.Any("options", options))
//init redis
redisClient := redis.NewClient(&redis.Options{
@ -117,13 +120,23 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co
DB: int(options.RedisDB),
})
encrypt := utils.NewEncrypt(options.PubKey, options.PrivKey)
workerSendClientCh := make(chan model.Answer, 50)
workerRespondentCh := make(chan []model.Answer, 50)
blobstore, err := dalBS.New(ctx, minioClient)
if err != nil {
return nil, err
}
svc := service.New(blobstore, pgdal, workerRespondentCh, workerSendClientCh)
//svc := service.New(blobstore, pgdal, workerRespondentCh, workerSendClientCh)
svc := service.New(service.ServiceDeps{
Store: blobstore,
Dal: pgdal,
WorkerSendClientCh: workerSendClientCh,
WorkerRespondentCh: workerRespondentCh,
Encrypt: encrypt,
RedirectURl: options.RedirectURL,
})
saveRespWcData := savewc.DepsForResp{
WorkerRespondentCh: workerRespondentCh,

@ -16,6 +16,9 @@ services:
PG_CRED: 'host=10.8.0.5 port=5433 user=squiz password=Redalert2 dbname=squiz sslmode=disable'
REDIS_HOST: '10.8.0.5:6379'
REDIS_PASSWORD: 'Redalert2'
REDIS_DB: 2
REDIS_DB: 2
PUBLIC_KEY: $PUBLIC_KEY
PRIVATE_KEY: $PRIVATE_KEY
ports:
- 10.8.0.5:1491:1490

@ -4,10 +4,12 @@ import (
"encoding/json"
"fmt"
"github.com/gofiber/fiber/v2"
"net/url"
"penahub.gitlab.yandexcloud.net/backend/quiz/answerer.git/dal"
quizdal "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal"
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/middleware"
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/utils"
"strings"
"sync"
"time"
@ -27,22 +29,36 @@ type Service struct {
m sync.Mutex
workerRespondentCh chan<- []model.Answer
workerSendClientCh chan<- model.Answer
encrypt *utils.Encrypt
redirectURl string
}
func New(s *dal.Storer, q *quizdal.DAL, workerRespondentCh chan<- []model.Answer, workerSendClientCh chan<- model.Answer) *Service {
type ServiceDeps struct {
Store *dal.Storer
Dal *quizdal.DAL
WorkerRespondentCh chan<- []model.Answer
WorkerSendClientCh chan<- model.Answer
Encrypt *utils.Encrypt
RedirectURl string
}
func New(deps ServiceDeps) *Service {
return &Service{
store: s,
dal: q,
store: deps.Store,
dal: deps.Dal,
m: sync.Mutex{},
batch: []model.Answer{},
workerRespondentCh: workerRespondentCh,
workerSendClientCh: workerSendClientCh,
workerRespondentCh: deps.WorkerRespondentCh,
workerSendClientCh: deps.WorkerSendClientCh,
encrypt: deps.Encrypt,
redirectURl: deps.RedirectURl,
}
}
func (s *Service) Register(app *fiber.App) *fiber.App {
app.Post("/answer", s.PutAnswersOnePiece)
app.Post("/settings", s.GetQuizData)
app.Get("/logo", s.MiniPart)
return app
}
@ -134,7 +150,6 @@ func (s *Service) GetQuizData(c *fiber.Ctx) error {
showBadge := true
fmt.Println("PRIVRRRR", account.ID, account.Privileges)
if priv, ok := account.Privileges["squizHideBadge"]; ok {
expiration := priv.CreatedAt.Add(time.Duration(priv.Amount) * 24 * time.Hour)
@ -387,3 +402,31 @@ func (s *Service) PutAnswersOnePiece(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).JSON(response)
}
func (s *Service) MiniPart(ctx *fiber.Ctx) error {
qid := ctx.Query("q")
if qid == "" {
return ctx.Status(fiber.StatusBadRequest).SendString("qid is nil")
}
ctx.Cookie(&fiber.Cookie{
Name: "quizFrom",
Value: qid,
})
userID, err := s.dal.AccountRepo.GetQidOwner(ctx.Context(), qid)
if err != nil {
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
shifr, err := s.encrypt.EncryptStr(userID)
if err != nil {
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
ctx.Cookie(&fiber.Cookie{
Name: "quizUser",
Value: url.QueryEscape(string(shifr)),
})
return ctx.Redirect(s.redirectURl, fiber.StatusFound)
}