feat: Refactor fiber server

This commit is contained in:
Christian Ditaputratama 2024-10-29 21:30:04 +07:00
parent 35d53c8a58
commit 3a45071cd6
No known key found for this signature in database
GPG key ID: 31D3D06D77950979
4 changed files with 36 additions and 21 deletions

View file

@ -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() {

View file

@ -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.",

View file

@ -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)

View file

@ -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
}