mirror of
https://github.com/ditatompel/xmr-remote-nodes.git
synced 2025-01-08 05:52:10 +07:00
feat: Refactor fiber server
This commit is contained in:
parent
35d53c8a58
commit
3a45071cd6
4 changed files with 36 additions and 21 deletions
|
@ -8,7 +8,6 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ditatompel/xmr-remote-nodes/frontend"
|
|
||||||
"github.com/ditatompel/xmr-remote-nodes/internal/config"
|
"github.com/ditatompel/xmr-remote-nodes/internal/config"
|
||||||
"github.com/ditatompel/xmr-remote-nodes/internal/cron"
|
"github.com/ditatompel/xmr-remote-nodes/internal/cron"
|
||||||
"github.com/ditatompel/xmr-remote-nodes/internal/database"
|
"github.com/ditatompel/xmr-remote-nodes/internal/database"
|
||||||
|
@ -17,7 +16,6 @@ import (
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/fiber/v2/middleware/cors"
|
"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/logger"
|
||||||
"github.com/gofiber/fiber/v2/middleware/recover"
|
"github.com/gofiber/fiber/v2/middleware/recover"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
@ -57,11 +55,7 @@ func serve() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define Fiber config & app.
|
// Define Fiber config & app.
|
||||||
app := fiber.New(fiber.Config{
|
app := handler.NewServer()
|
||||||
Prefork: appCfg.Prefork,
|
|
||||||
ProxyHeader: appCfg.ProxyHeader,
|
|
||||||
AppName: "XMR Nodes Aggregator",
|
|
||||||
})
|
|
||||||
|
|
||||||
// recover
|
// recover
|
||||||
app.Use(recover.New(recover.Config{EnableStackTrace: true}))
|
app.Use(recover.New(recover.Config{EnableStackTrace: true}))
|
||||||
|
@ -81,12 +75,7 @@ func serve() {
|
||||||
}))
|
}))
|
||||||
|
|
||||||
app.Use("/assets", views.EmbedAssets())
|
app.Use("/assets", views.EmbedAssets())
|
||||||
|
app.Routes()
|
||||||
handler.V1Api(app)
|
|
||||||
app.Use("/", filesystem.New(filesystem.Config{
|
|
||||||
Root: frontend.SvelteKitHandler(),
|
|
||||||
// NotFoundFile: "index.html",
|
|
||||||
}))
|
|
||||||
|
|
||||||
// go routine to capture system calls
|
// go routine to capture system calls
|
||||||
go func() {
|
go func() {
|
||||||
|
|
|
@ -13,7 +13,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Render Home Page
|
// Render Home Page
|
||||||
func homeHandler(c *fiber.Ctx) error {
|
func (s *fiberServer) homeHandler(c *fiber.Ctx) error {
|
||||||
p := views.Meta{
|
p := views.Meta{
|
||||||
Title: "Monero Remote Node",
|
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.",
|
Description: "A website that helps you monitor your favourite Monero remote nodes, but YOU BETTER RUN AND USE YOUR OWN NODE.",
|
||||||
|
|
|
@ -1,13 +1,10 @@
|
||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
func (s *fiberServer) Routes() {
|
||||||
"github.com/gofiber/fiber/v2"
|
s.App.Get("/", s.homeHandler)
|
||||||
)
|
|
||||||
|
|
||||||
// V1 API routes
|
// V1 API routes
|
||||||
func V1Api(app *fiber.App) {
|
v1 := s.App.Group("/api/v1")
|
||||||
app.Get("/", homeHandler)
|
|
||||||
v1 := app.Group("/api/v1")
|
|
||||||
|
|
||||||
// these routes are public, they don't require a prober api key
|
// these routes are public, they don't require a prober api key
|
||||||
v1.Get("/nodes", Nodes)
|
v1.Get("/nodes", Nodes)
|
||||||
|
|
29
internal/handler/server.go
Normal file
29
internal/handler/server.go
Normal 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
|
||||||
|
}
|
Loading…
Reference in a new issue