refactor: wrap fiber.App in custom Server struct

This commit is contained in:
2025-11-30 23:14:09 +00:00
parent 3e96c42c4a
commit e8a558d652
2 changed files with 35 additions and 5 deletions

View File

@@ -29,6 +29,5 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
log.Printf("starting server on :%d", config.Server.Port) log.Fatal(server.Start())
log.Fatal(server.Listen(fmt.Sprintf(":%d", config.Server.Port)))
} }

View File

@@ -14,10 +14,24 @@ import (
"github.com/get-drexa/drexa/internal/virtualfs" "github.com/get-drexa/drexa/internal/virtualfs"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger" "github.com/gofiber/fiber/v2/middleware/logger"
"github.com/uptrace/bun"
"github.com/uptrace/bun/extra/bundebug" "github.com/uptrace/bun/extra/bundebug"
) )
func NewServer(c Config) (*fiber.App, error) { type Server struct {
config Config
app *fiber.App
db *bun.DB
blobStore blob.Store
vfs *virtualfs.VirtualFS
userService *user.Service
authService *auth.Service
accountService *account.Service
uploadService *upload.Service
authMiddleware fiber.Handler
}
func NewServer(c Config) (*Server, error) {
app := fiber.New(fiber.Config{ app := fiber.New(fiber.Config{
ErrorHandler: httperr.ErrorHandler, ErrorHandler: httperr.ErrorHandler,
StreamRequestBody: true, StreamRequestBody: true,
@@ -68,7 +82,7 @@ func NewServer(c Config) (*fiber.App, error) {
SecretKey: c.JWT.SecretKey, SecretKey: c.JWT.SecretKey,
}) })
uploadService := upload.NewService(vfs, blobStore) uploadService := upload.NewService(vfs, blobStore)
accountService := account.NewService(userService, vfs) accountService := account.NewService(userService)
authMiddleware := auth.NewBearerAuthMiddleware(authService, db) authMiddleware := auth.NewBearerAuthMiddleware(authService, db)
@@ -79,5 +93,22 @@ func NewServer(c Config) (*fiber.App, error) {
auth.NewHTTPHandler(authService, db).RegisterRoutes(api) auth.NewHTTPHandler(authService, db).RegisterRoutes(api)
upload.NewHTTPHandler(uploadService, db).RegisterRoutes(accRouter) upload.NewHTTPHandler(uploadService, db).RegisterRoutes(accRouter)
return app, nil s := &Server{
config: c,
app: app,
db: db,
blobStore: blobStore,
vfs: vfs,
userService: userService,
authService: authService,
accountService: accountService,
uploadService: uploadService,
authMiddleware: authMiddleware,
}
return s, nil
}
func (s *Server) Start() error {
return s.app.Listen(fmt.Sprintf(":%d", s.config.Server.Port))
} }