2 Commits 3bfd8fd50b ... 65b4387a0b

Author SHA1 Message Date
  Dmitriy Gnatenko 65b4387a0b Update fiber app 3 days ago
  Dmitriy Gnatenko 3bfd8fd50b Update fiber app 3 days ago

+ 1 - 1
internal/fiber/admin_handlers.go

@@ -8,7 +8,7 @@ import (
 	adminHandler "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/handler/admin"
 	adminHandler "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/handler/admin"
 )
 )
 
 
-func addAdminHandlers(app *fiber.App, sp ServiceProvider) {
+func initAdminHandlers(app *fiber.App, sp ServiceProvider) {
 	admin := app.Group("/admin", jwt.New(getJWTConfig(sp)))
 	admin := app.Group("/admin", jwt.New(getJWTConfig(sp)))
 
 
 	admin.Get(
 	admin.Get(

+ 5 - 26
internal/fiber/fiber.go

@@ -9,11 +9,8 @@ import (
 	cache "git.dmitriygnatenko.ru/dima/go-common/cache/ttl_memory_cache"
 	cache "git.dmitriygnatenko.ru/dima/go-common/cache/ttl_memory_cache"
 	"git.dmitriygnatenko.ru/dima/go-common/db"
 	"git.dmitriygnatenko.ru/dima/go-common/db"
 	"github.com/gofiber/fiber/v2"
 	"github.com/gofiber/fiber/v2"
-	"github.com/gofiber/fiber/v2/middleware/cors"
-	"github.com/gofiber/fiber/v2/middleware/recover"
 	"github.com/gofiber/template/html/v2"
 	"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/repositories"
 	"git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/auth"
 	"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/config"
@@ -41,26 +38,15 @@ type ServiceProvider interface {
 func Init(sp ServiceProvider) (*fiber.App, error) {
 func Init(sp ServiceProvider) (*fiber.App, error) {
 	fiberApp := fiber.New(getConfig(sp))
 	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)
 
 
-	// Metrics
-	addMetrics(fiberApp, sp)
-
-	// Public handlers
-	addPublicHandlers(fiberApp, sp)
-
-	// Protected handlers
-	addAdminHandlers(fiberApp, sp)
+	initAdminHandlers(fiberApp, sp)
 
 
 	return fiberApp, nil
 	return fiberApp, nil
 }
 }
@@ -74,13 +60,6 @@ func getConfig(sp ServiceProvider) fiber.Config {
 	}
 	}
 }
 }
 
 
-func getCORSConfig(sp ServiceProvider) cors.Config {
-	return cors.Config{
-		AllowOrigins: sp.ConfigService().CORSAllowOrigins(),
-		AllowMethods: sp.ConfigService().CORSAllowMethods(),
-	}
-}
-
 func getViewsEngine(sp ServiceProvider) *html.Engine {
 func getViewsEngine(sp ServiceProvider) *html.Engine {
 	engine := html.New(templatesPath, ".html")
 	engine := html.New(templatesPath, ".html")
 
 

+ 4 - 2
internal/fiber/metrics.go

@@ -1,12 +1,14 @@
 package fiber
 package fiber
 
 
 import (
 import (
+	"fmt"
+
 	"github.com/gofiber/fiber/v2"
 	"github.com/gofiber/fiber/v2"
 	"github.com/gofiber/fiber/v2/middleware/basicauth"
 	"github.com/gofiber/fiber/v2/middleware/basicauth"
 	"github.com/gofiber/fiber/v2/middleware/monitor"
 	"github.com/gofiber/fiber/v2/middleware/monitor"
 )
 )
 
 
-func addMetrics(app *fiber.App, sp ServiceProvider) {
+func initMetrics(app *fiber.App, sp ServiceProvider) {
 	basicAuth := basicauth.New(basicauth.Config{
 	basicAuth := basicauth.New(basicauth.Config{
 		Users: map[string]string{
 		Users: map[string]string{
 			sp.ConfigService().BasicAuthUser(): sp.ConfigService().BasicAuthPassword(),
 			sp.ConfigService().BasicAuthUser(): sp.ConfigService().BasicAuthPassword(),
@@ -18,6 +20,6 @@ func addMetrics(app *fiber.App, sp ServiceProvider) {
 
 
 func getMetricsConfig() monitor.Config {
 func getMetricsConfig() monitor.Config {
 	return monitor.Config{
 	return monitor.Config{
-		Title: "dmitriygnatenko.ru metrics",
+		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(),
+	}
+}

+ 1 - 1
internal/fiber/public_handlers.go

@@ -6,7 +6,7 @@ import (
 	"git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/handler"
 	"git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/handler"
 )
 )
 
 
-func addPublicHandlers(app *fiber.App, sp ServiceProvider) {
+func initPublicHandlers(app *fiber.App, sp ServiceProvider) {
 	app.Get(
 	app.Get(
 		"/",
 		"/",
 		handler.MainPageHandler(
 		handler.MainPageHandler(

+ 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)
+}