diff --git a/app/app.go b/app/app.go index 50523e3..2062ae5 100644 --- a/app/app.go +++ b/app/app.go @@ -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, diff --git a/service/service.go b/service/service.go index 6f24d95..157cf3f 100644 --- a/service/service.go +++ b/service/service.go @@ -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("/quiz/logo", s.MiniPart) return app } @@ -387,3 +403,33 @@ 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()) + } + + fmt.Println("OLOLO", string(shifr)) + + ctx.Cookie(&fiber.Cookie{ + Name: "quizUser", + Value: url.QueryEscape(string(shifr)), + }) + + return ctx.Redirect(s.redirectURl, fiber.StatusFound) +}