tijl.dev-core/modules/web/app.go

40 lines
886 B
Go
Raw Normal View History

2024-08-21 23:43:54 +02:00
package web
2024-08-22 13:47:16 +02:00
import (
2024-08-25 17:43:55 +02:00
"sort"
2024-08-22 15:15:16 +02:00
log "git.tijl.dev/tijl/tijl.dev-core/modules/logger"
2024-08-22 13:47:16 +02:00
"github.com/gofiber/fiber/v2"
)
2024-08-21 23:43:54 +02:00
/*
Basic system for being able to add routes from other packages
*/
2024-08-25 17:43:55 +02:00
type setupFuncWithPriority struct {
function func(*fiber.App)
priority int
}
var setupFuncs []setupFuncWithPriority
2024-08-21 23:43:54 +02:00
2024-08-25 17:43:55 +02:00
func RegisterAppSetupFunc(function func(*fiber.App), priority int) {
2024-08-22 13:47:16 +02:00
log.Debug().Msg("web.RegisterAppSetupFunc: registered a function")
2024-08-25 17:43:55 +02:00
setupFuncs = append(setupFuncs, setupFuncWithPriority{
function: function,
priority: priority,
})
2024-08-21 23:43:54 +02:00
}
2024-08-25 17:43:55 +02:00
// Setup executes all registered functions in order of priority
2024-08-21 23:43:54 +02:00
func Setup(app *fiber.App) {
2024-08-25 17:43:55 +02:00
sort.SliceStable(setupFuncs, func(i, j int) bool {
return setupFuncs[i].priority > setupFuncs[j].priority
})
2024-08-21 23:43:54 +02:00
2024-08-25 17:43:55 +02:00
for _, setupFunc := range setupFuncs {
// log.Debug().Msg("web.Setup: executing a function")
setupFunc.function(app)
2024-08-21 23:43:54 +02:00
}
}