diff --git a/cmd/server/serve.go b/cmd/server/serve.go index 645fc23..073d838 100644 --- a/cmd/server/serve.go +++ b/cmd/server/serve.go @@ -8,7 +8,6 @@ import ( "syscall" "time" - "github.com/ditatompel/xmr-remote-nodes/frontend" "github.com/ditatompel/xmr-remote-nodes/internal/config" "github.com/ditatompel/xmr-remote-nodes/internal/cron" "github.com/ditatompel/xmr-remote-nodes/internal/database" @@ -17,7 +16,6 @@ import ( "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cors" - "github.com/gofiber/fiber/v2/middleware/filesystem" "github.com/gofiber/fiber/v2/middleware/logger" "github.com/gofiber/fiber/v2/middleware/recover" "github.com/spf13/cobra" @@ -57,11 +55,7 @@ func serve() { } // Define Fiber config & app. - app := fiber.New(fiber.Config{ - Prefork: appCfg.Prefork, - ProxyHeader: appCfg.ProxyHeader, - AppName: "XMR Nodes Aggregator", - }) + app := handler.NewServer() // recover app.Use(recover.New(recover.Config{EnableStackTrace: true})) @@ -81,12 +75,7 @@ func serve() { })) app.Use("/assets", views.EmbedAssets()) - - handler.V1Api(app) - app.Use("/", filesystem.New(filesystem.Config{ - Root: frontend.SvelteKitHandler(), - // NotFoundFile: "index.html", - })) + app.Routes() // go routine to capture system calls go func() { diff --git a/internal/handler/response.go b/internal/handler/response.go index 2c4c124..5799285 100644 --- a/internal/handler/response.go +++ b/internal/handler/response.go @@ -13,7 +13,7 @@ import ( ) // Render Home Page -func homeHandler(c *fiber.Ctx) error { +func (s *fiberServer) homeHandler(c *fiber.Ctx) error { p := views.Meta{ Title: "Monero Remote Node", Description: "A website that helps you monitor your favourite Monero remote nodes, but YOU BETTER RUN AND USE YOUR OWN NODE.", diff --git a/internal/handler/routes.go b/internal/handler/routes.go index ff84ef3..eb61fe2 100644 --- a/internal/handler/routes.go +++ b/internal/handler/routes.go @@ -1,13 +1,10 @@ package handler -import ( - "github.com/gofiber/fiber/v2" -) +func (s *fiberServer) Routes() { + s.App.Get("/", s.homeHandler) -// V1 API routes -func V1Api(app *fiber.App) { - app.Get("/", homeHandler) - v1 := app.Group("/api/v1") + // V1 API routes + v1 := s.App.Group("/api/v1") // these routes are public, they don't require a prober api key v1.Get("/nodes", Nodes) diff --git a/internal/handler/server.go b/internal/handler/server.go new file mode 100644 index 0000000..acc7452 --- /dev/null +++ b/internal/handler/server.go @@ -0,0 +1,29 @@ +package handler + +import ( + "github.com/ditatompel/xmr-remote-nodes/internal/config" + "github.com/ditatompel/xmr-remote-nodes/internal/database" + "github.com/gofiber/fiber/v2" +) + +type fiberServer struct { + *fiber.App + db *database.DB +} + +// NewServer returns a new fiber server +func NewServer() *fiberServer { + if database.ConnectDB() != nil { + panic("Failed to connect to database") + } + server := &fiberServer{ + App: fiber.New(fiber.Config{ + Prefork: config.AppCfg().Prefork, + ProxyHeader: config.AppCfg().ProxyHeader, + AppName: "XMR Nodes Aggregator " + config.Version, + }), + db: database.GetDB(), + } + + return server +}