feat: Add homepage data handlers
This commit is contained in:
parent
f0c009eac5
commit
d828c7af24
2 changed files with 29 additions and 20 deletions
37
app.go
37
app.go
|
@ -9,15 +9,15 @@ import (
|
||||||
"github.com/gofiber/template/html"
|
"github.com/gofiber/template/html"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type App struct {
|
type App struct {
|
||||||
Config Config
|
Config Config
|
||||||
modules map[string]Module
|
modules map[string]Module
|
||||||
fiber *fiber.App
|
fiber *fiber.App
|
||||||
sessions *session.Store
|
sessions *session.Store
|
||||||
authHandlers []AuthHandler
|
authHandlers []AuthHandler
|
||||||
viewsFs *utils.JoinedFilesystem
|
viewsFs *utils.JoinedFilesystem
|
||||||
homepage string
|
homepage string
|
||||||
|
homepageHandlers []func(map[string]interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) data(ctx *fiber.Ctx, d fiber.Map) fiber.Map {
|
func (app *App) data(ctx *fiber.Ctx, d fiber.Map) fiber.Map {
|
||||||
|
@ -44,20 +44,27 @@ func New(configs ...Config) *App {
|
||||||
views := html.NewFileSystem(viewsFs, ".html")
|
views := html.NewFileSystem(viewsFs, ".html")
|
||||||
views.Reload(true)
|
views.Reload(true)
|
||||||
app := &App{
|
app := &App{
|
||||||
fiber: fiber.New(fiber.Config{Views: views}),
|
fiber: fiber.New(fiber.Config{Views: views}),
|
||||||
sessions: session.New(),
|
sessions: session.New(),
|
||||||
modules: map[string]Module{},
|
modules: map[string]Module{},
|
||||||
viewsFs: viewsFs,
|
viewsFs: viewsFs,
|
||||||
homepage: "homepage",
|
homepage: "homepage",
|
||||||
|
homepageHandlers: []func(map[string]interface{}){},
|
||||||
}
|
}
|
||||||
if len(configs) > 0 {
|
if len(configs) > 0 {
|
||||||
app.Config = configs[0]
|
app.Config = configs[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
app.fiber.Get("/", func(ctx *fiber.Ctx) error {
|
app.fiber.Get("/", func(ctx *fiber.Ctx) error {
|
||||||
return ctx.Render(app.homepage, app.data(ctx, fiber.Map{
|
data := app.data(ctx, fiber.Map{
|
||||||
"title": "Homepage",
|
"title": "Homepage",
|
||||||
}), "layouts/main")
|
})
|
||||||
|
if app.homepageHandlers != nil && len(app.homepageHandlers) > 0 {
|
||||||
|
for _, fun := range app.homepageHandlers {
|
||||||
|
fun(data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ctx.Render(app.homepage, data, "layouts/main")
|
||||||
})
|
})
|
||||||
|
|
||||||
app.fiber.Get("/logout", func(ctx *fiber.Ctx) error {
|
app.fiber.Get("/logout", func(ctx *fiber.Ctx) error {
|
||||||
|
|
12
manager.go
12
manager.go
|
@ -1,14 +1,15 @@
|
||||||
package modweb
|
package modweb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/fiber/v2/middleware/session"
|
"github.com/gofiber/fiber/v2/middleware/session"
|
||||||
"net/http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ModuleManager struct {
|
type ModuleManager struct {
|
||||||
module Module
|
module Module
|
||||||
app *App
|
app *App
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mm *ModuleManager) RegisterViews(system http.FileSystem) {
|
func (mm *ModuleManager) RegisterViews(system http.FileSystem) {
|
||||||
|
@ -29,11 +30,11 @@ func (mm *ModuleManager) Fiber(handlers ...fiber.Handler) fiber.Router {
|
||||||
return ctx.Next()
|
return ctx.Next()
|
||||||
},
|
},
|
||||||
}, handlers...)
|
}, handlers...)
|
||||||
return mm.app.fiber.Group("/" + mm.module.Name() + "/", handlers...)
|
return mm.app.fiber.Group("/"+mm.module.Name()+"/", handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mm *ModuleManager) Render(ctx *fiber.Ctx, template string, data fiber.Map) error {
|
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")
|
return ctx.Render(mm.module.Name()+"/"+template, mm.app.data(ctx, data), "layouts/main")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mm *ModuleManager) Session(ctx *fiber.Ctx) *session.Session {
|
func (mm *ModuleManager) Session(ctx *fiber.Ctx) *session.Session {
|
||||||
|
@ -51,8 +52,9 @@ func (mm *ModuleManager) User(ctx *fiber.Ctx) *User {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mm *ModuleManager) SetHomepage(template string) {
|
func (mm *ModuleManager) SetHomepage(template string, handlers ...func(map[string]interface{})) {
|
||||||
mm.app.homepage = mm.module.Name() + "/" + template
|
mm.app.homepage = mm.module.Name() + "/" + template
|
||||||
|
mm.app.homepageHandlers = handlers
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mm *ModuleManager) SetSessionConfig(config session.Config) {
|
func (mm *ModuleManager) SetSessionConfig(config session.Config) {
|
||||||
|
|
Loading…
Reference in a new issue