ソースを参照

Update handlers

Dima 1 年間 前
コミット
092da7c6ae

+ 3 - 0
go.mod

@@ -17,6 +17,7 @@ require (
 )
 
 require (
+	github.com/Masterminds/squirrel v1.5.3 // indirect
 	github.com/andybalholm/brotli v1.0.4 // indirect
 	github.com/davecgh/go-spew v1.1.1 // indirect
 	github.com/fsnotify/fsnotify v1.5.1 // indirect
@@ -24,6 +25,8 @@ require (
 	github.com/google/uuid v1.3.0 // indirect
 	github.com/hashicorp/hcl v1.0.0 // indirect
 	github.com/klauspost/compress v1.15.9 // indirect
+	github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
+	github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
 	github.com/leodido/go-urn v1.2.1 // indirect
 	github.com/magiconair/properties v1.8.5 // indirect
 	github.com/mattn/go-colorable v0.1.13 // indirect

+ 6 - 0
go.sum

@@ -53,6 +53,8 @@ github.com/CloudyKit/jet/v6 v6.1.0/go.mod h1:d3ypHeIRNo2+XyqnGA8s+aphtcVpjP5hPwP
 github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
 github.com/Joker/hpp v1.0.0/go.mod h1:8x5n+M1Hp5hC0g8okX3sR3vFQwynaX/UgSOM9MeBKzY=
 github.com/Joker/jade v1.1.3/go.mod h1:T+2WLyt7VH6Lp0TRxQrUYEs64nRc83wkMQrfeIQKduM=
+github.com/Masterminds/squirrel v1.5.3 h1:YPpoceAcxuzIljlr5iWpNKaql7hLeG1KLSrhvdHpkZc=
+github.com/Masterminds/squirrel v1.5.3/go.mod h1:NNaOrjSoIDfDA40n7sr2tPNZRfjzjA400rg+riTZj10=
 github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
 github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
 github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
@@ -287,6 +289,10 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
 github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
 github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
 github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
+github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw=
+github.com/lann/builder v0.0.0-20180802200727-47ae307949d0/go.mod h1:dXGbAdH5GtBTC4WfIxhKZfyBF/HBFgRZSWwZ9g/He9o=
+github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhRWSsG5rVo6hYhAB/ADZrk=
+github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0/go.mod h1:vmVJ0l/dxyfGW6FmdpVm2joNMFikkuWg0EoCKLGUMNw=
 github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
 github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
 github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=

+ 7 - 10
internal/services/handler/test/helper.go → internal/helpers/fiber.go

@@ -1,19 +1,16 @@
-package test
+package helpers
 
 import (
 	"html/template"
+	"strconv"
 	"time"
 
+	"github.com/brianvoe/gofakeit/v6"
 	"github.com/gofiber/fiber/v2"
 	"github.com/gofiber/template/html"
 )
 
-const (
-	testVersion = "1"
-	testGA      = false
-)
-
-func GetFiberConfig() fiber.Config {
+func GetFiberTestConfig() fiber.Config {
 	engine := html.New("./../../templates", ".html")
 
 	// nolint:gocritic
@@ -32,11 +29,11 @@ func GetFiberConfig() fiber.Config {
 	})
 
 	engine.AddFunc("version", func() string {
-		return testVersion
+		return strconv.Itoa(gofakeit.Number(1, 1000))
 	})
 
-	engine.AddFunc("ga", func() bool {
-		return testGA
+	engine.AddFunc("ga", func() string {
+		return ""
 	})
 
 	return fiber.Config{

+ 4 - 4
internal/services/handler/admin/article.go

@@ -13,7 +13,7 @@ import (
 
 const errArticleExists = "Статья с данным URL уже существует"
 
-func ArticleHandler(sp interfaces.IServiceProvider) fiber.Handler {
+func ArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
 	return func(ctx *fiber.Ctx) error {
 		articles, err := sp.GetArticleRepository().GetAll(ctx.Context())
 		if err != nil {
@@ -32,7 +32,7 @@ func ArticleHandler(sp interfaces.IServiceProvider) fiber.Handler {
 	}
 }
 
-func AddArticleHandler(sp interfaces.IServiceProvider) fiber.Handler {
+func AddArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
 	return func(ctx *fiber.Ctx) error {
 		var validate = validator.New()
 		validateErrors := make(map[string]string)
@@ -111,7 +111,7 @@ func AddArticleHandler(sp interfaces.IServiceProvider) fiber.Handler {
 	}
 }
 
-func EditArticleHandler(sp interfaces.IServiceProvider) fiber.Handler {
+func EditArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
 	return func(ctx *fiber.Ctx) error {
 		var validate = validator.New()
 		validateErrors := make(map[string]string)
@@ -241,7 +241,7 @@ func EditArticleHandler(sp interfaces.IServiceProvider) fiber.Handler {
 	}
 }
 
-func DeleteArticleHandler(sp interfaces.IServiceProvider) fiber.Handler {
+func DeleteArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
 	return func(ctx *fiber.Ctx) error {
 		ID, err := strconv.Atoi(ctx.Params("id"))
 		if err != nil {

+ 4 - 4
internal/services/handler/admin/tag.go

@@ -13,7 +13,7 @@ import (
 
 const errTagExists = "Тег с данным URL уже существует"
 
-func TagHandler(sp interfaces.IServiceProvider) fiber.Handler {
+func TagHandler(sp interfaces.ServiceProvider) fiber.Handler {
 	return func(ctx *fiber.Ctx) error {
 		tags, err := sp.GetTagRepository().GetAll(ctx.Context())
 		if err != nil {
@@ -29,7 +29,7 @@ func TagHandler(sp interfaces.IServiceProvider) fiber.Handler {
 	}
 }
 
-func AddTagHandler(sp interfaces.IServiceProvider) fiber.Handler {
+func AddTagHandler(sp interfaces.ServiceProvider) fiber.Handler {
 	return func(ctx *fiber.Ctx) error {
 		var form models.TagForm
 		var validate = validator.New()
@@ -76,7 +76,7 @@ func AddTagHandler(sp interfaces.IServiceProvider) fiber.Handler {
 	}
 }
 
-func EditTagHandler(sp interfaces.IServiceProvider) fiber.Handler {
+func EditTagHandler(sp interfaces.ServiceProvider) fiber.Handler {
 	return func(ctx *fiber.Ctx) error {
 		var validate = validator.New()
 		validateErrors := make(map[string]string)
@@ -140,7 +140,7 @@ func EditTagHandler(sp interfaces.IServiceProvider) fiber.Handler {
 	}
 }
 
-func DeleteTagHandler(sp interfaces.IServiceProvider) fiber.Handler {
+func DeleteTagHandler(sp interfaces.ServiceProvider) fiber.Handler {
 	return func(ctx *fiber.Ctx) error {
 		ID, err := strconv.Atoi(ctx.Params("id"))
 		if err != nil {

+ 3 - 2
internal/services/handler/article.go

@@ -10,12 +10,13 @@ import (
 
 const (
 	maxArticlesCount = 3
+	articleParam     = "article"
 	articleCacheKey  = "article"
 )
 
-func ArticleHandler(sp interfaces.IServiceProvider) fiber.Handler {
+func ArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
 	return func(ctx *fiber.Ctx) error {
-		articleReq := ctx.Params("article")
+		articleReq := ctx.Params(articleParam)
 
 		renderData, found := sp.GetCacheService().Get(articleCacheKey + articleReq)
 

+ 1 - 1
internal/services/handler/main_page.go

@@ -8,7 +8,7 @@ import (
 
 const allPreviewArticlesCacheKey = "all-preview-articles"
 
-func MainPageHandler(sp interfaces.IServiceProvider) fiber.Handler {
+func MainPageHandler(sp interfaces.ServiceProvider) fiber.Handler {
 	return func(ctx *fiber.Ctx) error {
 		renderData, found := sp.GetCacheService().Get(allPreviewArticlesCacheKey)
 

+ 3 - 2
internal/services/handler/tag.go

@@ -8,11 +8,12 @@ import (
 	"github.com/gofiber/fiber/v2"
 )
 
+const tagParam = "tag"
 const tagCacheKey = "tag"
 
-func TagHandler(sp interfaces.IServiceProvider) fiber.Handler {
+func TagHandler(sp interfaces.ServiceProvider) fiber.Handler {
 	return func(ctx *fiber.Ctx) error {
-		tagReq := ctx.Params("tag")
+		tagReq := ctx.Params(tagParam)
 
 		renderData, found := sp.GetCacheService().Get(tagCacheKey + tagReq)
 

+ 4 - 0
readme.md

@@ -51,3 +51,7 @@ GA_KEY=UA-1111111-1 (empty for disable)
 - make docker-down (остановка контейнеров)
 - make install-deps (установка зависимостей)
 - make app-build (компиляция приложения)
+
+### Метрики (защищены Basic auth)
+
+/metrics/