aggiunto e testato quasar apps

This commit is contained in:
fabio
2026-03-01 20:42:27 +01:00
parent cdcacadb5f
commit 66a3cc7cdb
73 changed files with 1559 additions and 389 deletions

View File

@@ -1,49 +1,27 @@
package controllers
import (
"bytes"
"html/template"
"path/filepath"
"github.com/gofiber/fiber/v2"
)
type AdminController struct{}
type AdminController struct {
spaDir string
}
func NewAdminController() *AdminController {
return &AdminController{}
func NewAdminController(spaDir string) *AdminController {
return &AdminController{spaDir: spaDir}
}
func (ac *AdminController) Dashboard(c *fiber.Ctx) error {
return renderAdminPage(c, "Admin")
return c.SendFile(filepath.Join(ac.spaDir, "index.html"))
}
func renderAdminPage(c *fiber.Ctx, title string) error {
viewData := map[string]any{
"Title": title,
"NavSection": "admin",
}
for k, v := range localsTemplateData(c) {
viewData[k] = v
}
files := []string{
"web/templates/layout.html",
"web/templates/public/_navbar.html",
"web/templates/partials/language_dropdown.html",
"web/templates/public/_flash.html",
"web/templates/admin/admin.html",
}
tmpl, err := template.ParseFiles(files...)
if err != nil {
return err
}
var out bytes.Buffer
if err := tmpl.ExecuteTemplate(&out, "layout.html", viewData); err != nil {
return err
}
c.Type("html", "utf-8")
return c.Send(out.Bytes())
func (ac *AdminController) Fallback(c *fiber.Ctx) error {
return c.SendFile(filepath.Join(ac.spaDir, "index.html"))
}
func (ac *AdminController) Favicon(c *fiber.Ctx) error {
return c.SendFile(filepath.Join(ac.spaDir, "favicon.ico"))
}

View File

@@ -25,13 +25,6 @@ func (ac *AuthController) ShowHome(c *fiber.Ctx) error {
})
}
func (ac *AuthController) ShowWelcome(c *fiber.Ctx) error {
return renderPrivate(c, "welcome.html", map[string]any{
"Title": "Welcome",
"NavSection": "private",
})
}
func (ac *AuthController) ShowSignup(c *fiber.Ctx) error {
return renderPublic(c, "signup.html", map[string]any{
"Title": "Sign up",
@@ -97,7 +90,7 @@ func (ac *AuthController) Login(c *fiber.Ctx) error {
if err := httpmw.SetFlashSuccess(c, "Login effettuato"); err != nil {
return err
}
return c.Redirect("/welcome")
return c.Redirect("/private")
}
func (ac *AuthController) Logout(c *fiber.Ctx) error {

View File

@@ -0,0 +1,27 @@
package controllers
import (
"path/filepath"
"github.com/gofiber/fiber/v2"
)
type PrivateController struct {
spaDir string
}
func NewPrivateController(spaDir string) *PrivateController {
return &PrivateController{spaDir: spaDir}
}
func (ac *PrivateController) Dashboard(c *fiber.Ctx) error {
return c.SendFile(filepath.Join(ac.spaDir, "index.html"))
}
func (ac *PrivateController) Fallback(c *fiber.Ctx) error {
return c.SendFile(filepath.Join(ac.spaDir, "index.html"))
}
func (ac *PrivateController) Favicon(c *fiber.Ctx) error {
return c.SendFile(filepath.Join(ac.spaDir, "favicon.ico"))
}

View File

@@ -1,27 +0,0 @@
package controllers
import (
"trustcontact/internal/services"
"github.com/gofiber/fiber/v2"
)
type UsersController struct {
usersService *services.UsersService
}
func NewUsersController(usersService *services.UsersService) *UsersController {
return &UsersController{usersService: usersService}
}
func (uc *UsersController) Index(c *fiber.Ctx) error {
return renderAdminPage(c, "Admin")
}
func (uc *UsersController) Table(c *fiber.Ctx) error {
return renderAdminPage(c, "Admin")
}
func (uc *UsersController) Modal(c *fiber.Ctx) error {
return renderAdminPage(c, "Admin")
}

View File

@@ -2,6 +2,7 @@ package http
import (
"fmt"
"path/filepath"
"trustcontact/internal/config"
"trustcontact/internal/controllers"
httpmw "trustcontact/internal/http/middleware"
@@ -28,9 +29,10 @@ func RegisterRoutes(app *fiber.App, store *session.Store, database *gorm.DB, cfg
return fmt.Errorf("init auth service: %w", err)
}
authController := controllers.NewAuthController(authService)
usersService := services.NewUsersService(database)
usersController := controllers.NewUsersController(usersService)
adminController := controllers.NewAdminController()
privateSPADir := filepath.FromSlash("quasar/private_section/dist/spa")
privateController := controllers.NewPrivateController(privateSPADir)
adminSPADir := filepath.FromSlash("quasar/admin_section/dist/spa")
adminController := controllers.NewAdminController(adminSPADir)
app.Get("/healthz", func(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusOK)
@@ -49,20 +51,32 @@ func RegisterRoutes(app *fiber.App, store *session.Store, database *gorm.DB, cfg
app.Get("/reset-password", authController.ShowResetPassword)
app.Post("/reset-password", authController.ResetPassword)
app.Get("/forbidden", authController.ShowForbidden)
app.Get("/welcome", httpmw.RequireAuth(), authController.ShowWelcome)
app.Post("/preferences/lang", httpmw.RequireAuth(), authController.UpdateLanguage)
app.Post("/preferences/theme", httpmw.RequireAuth(), authController.UpdateTheme)
// Quasar admin SPA assets are emitted with absolute paths (/assets, /icons, /favicon.ico).
// Protect them with the same auth/admin middleware used by /admin.
app.Use("/assets", httpmw.RequireAuth(), httpmw.RequireAdmin())
app.Use("/icons", httpmw.RequireAuth(), httpmw.RequireAdmin())
app.Get("/favicon.ico", httpmw.RequireAuth(), httpmw.RequireAdmin(), privateController.Favicon)
app.Static("/assets", filepath.Join(privateSPADir, "assets"))
app.Static("/icons", filepath.Join(privateSPADir, "icons"))
private := app.Group("/private", httpmw.RequireAuth(), httpmw.RequireAdmin())
private.Get("/", func(c *fiber.Ctx) error {
return c.Redirect("/admin/users")
})
private.Get("/", privateController.Dashboard)
private.Get("/*", privateController.Fallback)
// Quasar admin SPA assets are emitted with absolute paths (/assets, /icons, /favicon.ico).
// Protect them with the same auth/admin middleware used by /admin.
app.Use("/assets", httpmw.RequireAuth(), httpmw.RequireAdmin())
app.Use("/icons", httpmw.RequireAuth(), httpmw.RequireAdmin())
app.Get("/favicon.ico", httpmw.RequireAuth(), httpmw.RequireAdmin(), adminController.Favicon)
app.Static("/assets", filepath.Join(adminSPADir, "assets"))
app.Static("/icons", filepath.Join(adminSPADir, "icons"))
admin := app.Group("/admin", httpmw.RequireAuth(), httpmw.RequireAdmin())
admin.Get("/", adminController.Dashboard)
admin.Get("/users", usersController.Index)
admin.Get("/users/table", usersController.Table)
admin.Get("/users/:id/modal", usersController.Modal)
admin.Get("/*", adminController.Fallback)
return nil
}