package handler //go:generate mkdir -p mocks //go:generate rm -rf ./mocks/*_minimock.go //go:generate minimock -i CacheService,ArticleRepository,TagRepository -o ./mocks/ -s "_minimock.go" import ( "context" "time" "git.dmitriygnatenko.ru/dima/go-common/logger" "github.com/gofiber/fiber/v2" "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/mapper" "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/models" ) type ( CacheService interface { Get(key string) (interface{}, bool) Set(key string, value interface{}, expiration *time.Duration) } ArticleRepository interface { GetByURL(ctx context.Context, url string) (*models.Article, error) GetAllPreview(ctx context.Context) ([]models.ArticlePreview, error) GetPreviewByTagID(ctx context.Context, tagID uint64) ([]models.ArticlePreview, error) } TagRepository interface { GetAllUsed(ctx context.Context) ([]models.Tag, error) GetByURL(ctx context.Context, tag string) (*models.Tag, error) } ) const allPreviewArticlesCacheKey = "all-preview-articles" func MainPageHandler( cacheService CacheService, articleRepository ArticleRepository, ) fiber.Handler { return func(fctx *fiber.Ctx) error { ctx := fctx.Context() renderData, found := cacheService.Get(allPreviewArticlesCacheKey) if !found { articles, err := articleRepository.GetAllPreview(fctx.Context()) if err != nil { logger.Error(ctx, err.Error()) return err } renderData = fiber.Map{ "headTitle": "От слона к суслику - статьи про PHP, Go, алгоритмы", "headDescription": "список статей", "headKeywords": "Дмитрий Гнатенко, программист, PHP, Go, Golang, программирование, статьи, блог", "pageTitle": "Список статей", "articles": mapper.ToArticlePreviewDTOList(articles), } cacheService.Set(allPreviewArticlesCacheKey, renderData, nil) } return fctx.Render("index", renderData, "_layout") } }