浏览代码

Update fiber app

Dmitriy Gnatenko 3 天之前
父节点
当前提交
98c8ac3f3f

+ 3 - 3
Makefile

@@ -21,12 +21,12 @@ lint:
 migration-status:
 	goose -dir migrations ${DB_DRIVER} ${GOOSE_DB_STRING} status
 
-migration-up-one:
-	goose -dir migrations ${DB_DRIVER} ${GOOSE_DB_STRING} up-by-one
-
 migration-up:
 	goose -dir migrations ${DB_DRIVER} ${GOOSE_DB_STRING} up
 
+migration-up-one:
+	goose -dir migrations ${DB_DRIVER} ${GOOSE_DB_STRING} up-by-one
+
 migration-down-one:
 	goose -dir migrations ${DB_DRIVER} ${GOOSE_DB_STRING} down
 

+ 125 - 0
internal/fiber/admin_handlers.go

@@ -0,0 +1,125 @@
+package fiber
+
+import (
+	"github.com/gofiber/fiber/v2"
+	"github.com/gofiber/fiber/v2/middleware/limiter"
+	jwt "github.com/gofiber/jwt/v3"
+
+	adminHandler "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/handler/admin"
+)
+
+func initAdminHandlers(app *fiber.App, sp ServiceProvider) {
+	admin := app.Group("/admin", jwt.New(getJWTConfig(sp)))
+
+	admin.Get(
+		"/",
+		adminHandler.ArticleHandler(sp.ArticleRepository()),
+	)
+
+	admin.All("/login", limiter.New(limiter.Config{
+		Max:        int(sp.ConfigService().LoginRateLimiterMaxRequests()),
+		Expiration: sp.ConfigService().LoginRateLimiterExpiration(),
+	}), adminHandler.LoginHandler(
+		sp.ConfigService(),
+		sp.AuthService(),
+		sp.UserRepository(),
+	))
+
+	admin.All(
+		"/logout",
+		adminHandler.LogoutHandler(sp.ConfigService()),
+	)
+
+	admin.All(
+		"/user/change-password",
+		adminHandler.ChangePassword(
+			sp.AuthService(),
+			sp.UserRepository(),
+		),
+	)
+
+	admin.All(
+		"/article/add",
+		adminHandler.AddArticleHandler(
+			sp.TransactionManager(),
+			sp.ArticleRepository(),
+			sp.TagRepository(),
+			sp.ArticleTagRepository(),
+		),
+	)
+
+	admin.All(
+		"/article/edit/:id<int>",
+		adminHandler.EditArticleHandler(
+			sp.TransactionManager(),
+			sp.ArticleRepository(),
+			sp.TagRepository(),
+			sp.ArticleTagRepository(),
+			sp.CacheService(),
+		),
+	)
+
+	admin.All(
+		"/article/delete/:id<int>",
+		adminHandler.DeleteArticleHandler(
+			sp.TransactionManager(),
+			sp.ArticleRepository(),
+			sp.ArticleTagRepository(),
+			sp.CacheService(),
+		),
+	)
+
+	admin.Get(
+		"/tag",
+		adminHandler.TagHandler(sp.TagRepository()),
+	)
+
+	admin.All(
+		"/tag/add",
+		adminHandler.AddTagHandler(
+			sp.TagRepository(),
+		),
+	)
+
+	admin.All(
+		"/tag/edit/:id<int>",
+		adminHandler.EditTagHandler(
+			sp.TagRepository(),
+			sp.CacheService(),
+		),
+	)
+
+	admin.All(
+		"/tag/delete/:id<int>",
+		adminHandler.DeleteTagHandler(
+			sp.TagRepository(),
+			sp.CacheService(),
+		),
+	)
+}
+
+// nolint
+func getJWTConfig(sp ServiceProvider) jwt.Config {
+	return jwt.Config{
+		SigningKey:  []byte(sp.ConfigService().JWTSecretKey()),
+		TokenLookup: "cookie:" + sp.ConfigService().JWTCookie(),
+		ErrorHandler: func(fctx *fiber.Ctx, err error) error {
+			return fctx.Redirect("/admin/login")
+		},
+		Filter: func(fctx *fiber.Ctx) bool {
+			method := fctx.Method()
+			path := fctx.Path()
+
+			if method != fiber.MethodGet && method != fiber.MethodPost &&
+				method != fiber.MethodPut && method != fiber.MethodDelete {
+				return true
+			}
+
+			if path == "/admin/login" {
+				return true
+			}
+
+			return false
+		},
+	}
+}

+ 5 - 183
internal/fiber/fiber.go

@@ -9,20 +9,11 @@ import (
 	cache "git.dmitriygnatenko.ru/dima/go-common/cache/ttl_memory_cache"
 	"git.dmitriygnatenko.ru/dima/go-common/db"
 	"github.com/gofiber/fiber/v2"
-	"github.com/gofiber/fiber/v2/middleware/basicauth"
-	"github.com/gofiber/fiber/v2/middleware/cors"
-	"github.com/gofiber/fiber/v2/middleware/limiter"
-	"github.com/gofiber/fiber/v2/middleware/monitor"
-	"github.com/gofiber/fiber/v2/middleware/recover"
-	jwt "github.com/gofiber/jwt/v3"
 	"github.com/gofiber/template/html/v2"
 
-	"git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/middleware/language"
 	"git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/repositories"
 	"git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/auth"
 	"git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/config"
-	"git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/handler"
-	adminHandler "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/handler/admin"
 	"git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/i18n"
 )
 
@@ -47,145 +38,15 @@ type ServiceProvider interface {
 func Init(sp ServiceProvider) (*fiber.App, error) {
 	fiberApp := fiber.New(getConfig(sp))
 
-	// Configure web root
-	fiberApp.Static("/", staticPath)
+	initStatic(fiberApp)
 
-	// Configure CORS middleware
-	fiberApp.Use(cors.New(getCORSConfig(sp)))
+	initMiddleware(fiberApp, sp)
 
-	// Configure recover middleware
-	fiberApp.Use(recover.New())
+	initMetrics(fiberApp, sp)
 
-	// Configure language middleware
-	fiberApp.Use(language.New())
+	initPublicHandlers(fiberApp, sp)
 
-	// Configure JWT auth
-	jwtAuth := jwt.New(getJWTConfig(sp))
-
-	// Configure Basic auth
-	basicAuth := basicauth.New(basicauth.Config{
-		Users: map[string]string{
-			sp.ConfigService().BasicAuthUser(): sp.ConfigService().BasicAuthPassword(),
-		},
-	})
-
-	// Metrics
-	fiberApp.Get(metricsURI, basicAuth, monitor.New(getMetricsConfig()))
-
-	// Public handlers
-	fiberApp.Get(
-		"/",
-		handler.MainPageHandler(
-			sp.CacheService(),
-			sp.ArticleRepository(),
-		),
-	)
-
-	fiberApp.Get(
-		"/tag/:tag", handler.TagHandler(
-			sp.CacheService(),
-			sp.ArticleRepository(),
-			sp.TagRepository(),
-		),
-	)
-
-	fiberApp.Get(
-		"/article/:article",
-		handler.ArticleHandler(
-			sp.CacheService(),
-			sp.ArticleRepository(),
-			sp.TagRepository(),
-		),
-	)
-
-	// Protected handlers
-	admin := fiberApp.Group("/admin", jwtAuth)
-
-	admin.Get(
-		"/",
-		adminHandler.ArticleHandler(sp.ArticleRepository()),
-	)
-
-	admin.All("/login", limiter.New(limiter.Config{
-		Max:        int(sp.ConfigService().LoginRateLimiterMaxRequests()),
-		Expiration: sp.ConfigService().LoginRateLimiterExpiration(),
-	}), adminHandler.LoginHandler(
-		sp.ConfigService(),
-		sp.AuthService(),
-		sp.UserRepository(),
-	))
-
-	admin.All(
-		"/logout",
-		adminHandler.LogoutHandler(sp.ConfigService()),
-	)
-
-	admin.All(
-		"/user/change-password",
-		adminHandler.ChangePassword(
-			sp.AuthService(),
-			sp.UserRepository(),
-		),
-	)
-
-	admin.All(
-		"/article/add",
-		adminHandler.AddArticleHandler(
-			sp.TransactionManager(),
-			sp.ArticleRepository(),
-			sp.TagRepository(),
-			sp.ArticleTagRepository(),
-		),
-	)
-
-	admin.All(
-		"/article/edit/:id<int>",
-		adminHandler.EditArticleHandler(
-			sp.TransactionManager(),
-			sp.ArticleRepository(),
-			sp.TagRepository(),
-			sp.ArticleTagRepository(),
-			sp.CacheService(),
-		),
-	)
-
-	admin.All(
-		"/article/delete/:id<int>",
-		adminHandler.DeleteArticleHandler(
-			sp.TransactionManager(),
-			sp.ArticleRepository(),
-			sp.ArticleTagRepository(),
-			sp.CacheService(),
-		),
-	)
-
-	admin.Get(
-		"/tag",
-		adminHandler.TagHandler(sp.TagRepository()),
-	)
-
-	admin.All(
-		"/tag/add",
-		adminHandler.AddTagHandler(
-			sp.TagRepository(),
-		),
-	)
-
-	admin.All(
-		"/tag/edit/:id<int>",
-		adminHandler.EditTagHandler(
-			sp.TagRepository(),
-			sp.CacheService(),
-		),
-	)
-
-	admin.All(
-		"/tag/delete/:id<int>",
-		adminHandler.DeleteTagHandler(
-			sp.TagRepository(),
-			sp.CacheService(),
-		),
-	)
+	initAdminHandlers(fiberApp, sp)
 
 	return fiberApp, nil
 }
@@ -199,45 +60,6 @@ func getConfig(sp ServiceProvider) fiber.Config {
 	}
 }
 
-// nolint
-func getJWTConfig(sp ServiceProvider) jwt.Config {
-	return jwt.Config{
-		SigningKey:  []byte(sp.ConfigService().JWTSecretKey()),
-		TokenLookup: "cookie:" + sp.ConfigService().JWTCookie(),
-		ErrorHandler: func(fctx *fiber.Ctx, err error) error {
-			return fctx.Redirect("/admin/login")
-		},
-		Filter: func(fctx *fiber.Ctx) bool {
-			method := fctx.Method()
-			path := fctx.Path()
-
-			if method != fiber.MethodGet && method != fiber.MethodPost &&
-				method != fiber.MethodPut && method != fiber.MethodDelete {
-				return true
-			}
-
-			if path == "/admin/login" {
-				return true
-			}
-
-			return false
-		},
-	}
-}
-
-func getMetricsConfig() monitor.Config {
-	return monitor.Config{
-		Title: "dmitriygnatenko.ru metrics",
-	}
-}
-
-func getCORSConfig(sp ServiceProvider) cors.Config {
-	return cors.Config{
-		AllowOrigins: sp.ConfigService().CORSAllowOrigins(),
-		AllowMethods: sp.ConfigService().CORSAllowMethods(),
-	}
-}
-
 func getViewsEngine(sp ServiceProvider) *html.Engine {
 	engine := html.New(templatesPath, ".html")
 

+ 25 - 0
internal/fiber/metrics.go

@@ -0,0 +1,25 @@
+package fiber
+
+import (
+	"fmt"
+
+	"github.com/gofiber/fiber/v2"
+	"github.com/gofiber/fiber/v2/middleware/basicauth"
+	"github.com/gofiber/fiber/v2/middleware/monitor"
+)
+
+func initMetrics(app *fiber.App, sp ServiceProvider) {
+	basicAuth := basicauth.New(basicauth.Config{
+		Users: map[string]string{
+			sp.ConfigService().BasicAuthUser(): sp.ConfigService().BasicAuthPassword(),
+		},
+	})
+
+	app.Get(metricsURI, basicAuth, monitor.New(getMetricsConfig()))
+}
+
+func getMetricsConfig() monitor.Config {
+	return monitor.Config{
+		Title: fmt.Sprintf("%s metrics", appName),
+	}
+}

+ 24 - 0
internal/fiber/middleware.go

@@ -0,0 +1,24 @@
+package fiber
+
+import (
+	"github.com/gofiber/fiber/v2"
+	"github.com/gofiber/fiber/v2/middleware/cors"
+	"github.com/gofiber/fiber/v2/middleware/recover"
+
+	"git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/middleware/language"
+)
+
+func initMiddleware(app *fiber.App, sp ServiceProvider) {
+	app.Use(cors.New(getCORSConfig(sp)))
+
+	app.Use(recover.New())
+
+	app.Use(language.New())
+}
+
+func getCORSConfig(sp ServiceProvider) cors.Config {
+	return cors.Config{
+		AllowOrigins: sp.ConfigService().CORSAllowOrigins(),
+		AllowMethods: sp.ConfigService().CORSAllowMethods(),
+	}
+}

+ 34 - 0
internal/fiber/public_handlers.go

@@ -0,0 +1,34 @@
+package fiber
+
+import (
+	"github.com/gofiber/fiber/v2"
+
+	"git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/handler"
+)
+
+func initPublicHandlers(app *fiber.App, sp ServiceProvider) {
+	app.Get(
+		"/",
+		handler.MainPageHandler(
+			sp.CacheService(),
+			sp.ArticleRepository(),
+		),
+	)
+
+	app.Get(
+		"/tag/:tag", handler.TagHandler(
+			sp.CacheService(),
+			sp.ArticleRepository(),
+			sp.TagRepository(),
+		),
+	)
+
+	app.Get(
+		"/article/:article",
+		handler.ArticleHandler(
+			sp.CacheService(),
+			sp.ArticleRepository(),
+			sp.TagRepository(),
+		),
+	)
+}

+ 9 - 0
internal/fiber/static.go

@@ -0,0 +1,9 @@
+package fiber
+
+import (
+	"github.com/gofiber/fiber/v2"
+)
+
+func initStatic(app *fiber.App) {
+	app.Static("/", staticPath)
+}