package modweb import ( "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/session" "net/http" ) type ModuleManager struct { module Module app *App } func (mm *ModuleManager) RegisterViews(system http.FileSystem) { mm.app.viewsFs.Register(mm.module.Name(), system) } func (mm *ModuleManager) Fiber(handlers ...fiber.Handler) fiber.Router { handlers = append([]fiber.Handler{ func(ctx *fiber.Ctx) error { min := mm.module.MinAccessLevel() if min > 0 { user := mm.User(ctx) if user == nil || user.AccessLevel < min { return ctx.Redirect("/") } } return ctx.Next() }, }, handlers...) return mm.app.fiber.Group("/" + mm.module.Name() + "/", handlers...) } func (mm *ModuleManager) Render(ctx *fiber.Ctx, template string, data fiber.Map) error { return ctx.Render(mm.module.Name() + "/" + template, mm.app.data(ctx, data), "layouts/main") } func (mm *ModuleManager) Session(ctx *fiber.Ctx) *session.Session { sess, _ := mm.app.sessions.Get(ctx) return sess } func (mm *ModuleManager) User(ctx *fiber.Ctx) *User { sess := mm.Session(ctx) if _, ok := sess.Get("user:id").(string); ok { user := &User{} user.Load(sess) return user } return nil } func (mm *ModuleManager) SetHomepage(template string) { mm.app.homepage = mm.module.Name() + "/" + template } func (mm *ModuleManager) SetSessionConfig(config session.Config) { mm.app.sessions = session.New(config) }