Big Refacotr - part2

This commit is contained in:
PB
2025-10-27 18:38:33 +01:00
parent 5a304f11ac
commit 988479dc5d
13 changed files with 94 additions and 159 deletions

View File

@@ -6,12 +6,10 @@ import (
"net"
"time"
"github.com/go-redis/redis/v8"
jwt "github.com/gofiber/contrib/jwt"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgxpool"
commonDTO "git.ego.freeddns.org/egommerce/api-entities/common/dto"
cnf "git.ego.freeddns.org/egommerce/go-api-pkg/config"
@@ -35,32 +33,26 @@ var defaultCORS = cors.New(
type (
Server struct {
*fiber.App
*PluginManager
ID string
addr string // e.g. "127.0.0.1:443"
plugins map[string]any
ID string
addr string // e.g. "127.0.0.1:443"
}
// HeaderRequestID struct {
// RequestID string `reqHeader:"x-request-id"`
// }
)
func NewServer(c *Config) *Server {
srv := &Server{
return &Server{
ID: c.ID,
App: fiber.New(fiber.Config{
AppName: c.ID,
ServerHeader: c.Name + ":" + c.ID,
ServerHeader: c.getAppFullName(),
ReadTimeout: c.ReadTimeout * time.Millisecond,
WriteTimeout: c.WriteTimeout * time.Millisecond,
IdleTimeout: c.IdleTimeout * time.Millisecond,
}),
addr: c.NetAddr,
plugins: make(map[string]any),
PluginManager: NewPluginManager(),
addr: c.NetAddr,
}
return srv
}
func (s *Server) Start() error {
@@ -88,28 +80,6 @@ func (s *Server) OnShutdown() {
s.Shutdown()
}
func (s *Server) addPlugin(name string, fn PluginFn) {
s.plugins[name] = fn()
}
// Plugin helper functions - using hardcoded keys because we rely on a specific implementation here...
func (s *Server) getCache() *redis.Client {
return (s.plugins["cache"]).(*redis.Client)
}
func (s *Server) getDatabase() *pgxpool.Pool {
return (s.plugins["database"]).(*pgxpool.Pool)
}
// func GetRequestID(c *fiber.Ctx) (string, error) {
// var hdr = new(HeaderRequestID)
// if err := c.ReqHeaderParser(hdr); err != nil {
// return "", err
// }
// return hdr.RequestID, nil
// }
func (s *Server) setupRouter() {
s.Options("*", defaultCORS)
s.Use(defaultCORS)
@@ -159,7 +129,3 @@ func JWTProtected() func(c *fiber.Ctx) error {
},
})
}
// func (s *Server) Error(c *fiber.Ctx, code int, msg string) error {
// return c.Status(code).JSON(dto.ErrorResponseDTO{Error: msg})
// }