init
This commit is contained in:
78
backend/app/http/web/controller.go
Normal file
78
backend/app/http/web/controller.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type WebController struct{}
|
||||
|
||||
func resolveDistDir(primary, fallback string) string {
|
||||
if st, err := os.Stat(primary); err == nil && st.IsDir() {
|
||||
return primary
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func sendIndex(ctx fiber.Ctx, distDir string) error {
|
||||
indexPath := filepath.Join(distDir, "index.html")
|
||||
if st, err := os.Stat(indexPath); err == nil && !st.IsDir() {
|
||||
return ctx.SendFile(indexPath)
|
||||
}
|
||||
return fiber.ErrNotFound
|
||||
}
|
||||
|
||||
func sendAssetOrIndex(ctx fiber.Ctx, distDir, rel string) error {
|
||||
rel = filepath.Clean(strings.TrimSpace(rel))
|
||||
if rel == "." || rel == "/" {
|
||||
rel = ""
|
||||
}
|
||||
if strings.HasPrefix(rel, "..") {
|
||||
return fiber.ErrBadRequest
|
||||
}
|
||||
|
||||
if rel != "" {
|
||||
assetPath := filepath.Join(distDir, rel)
|
||||
if st, err := os.Stat(assetPath); err == nil && !st.IsDir() {
|
||||
return ctx.SendFile(assetPath)
|
||||
}
|
||||
}
|
||||
|
||||
return sendIndex(ctx, distDir)
|
||||
}
|
||||
|
||||
// AdminIndex
|
||||
//
|
||||
// @Router /admin [get]
|
||||
func (w *WebController) AdminIndex(ctx fiber.Ctx) error {
|
||||
adminDist := resolveDistDir("frontend/admin/dist", "../frontend/admin/dist")
|
||||
return sendIndex(ctx, adminDist)
|
||||
}
|
||||
|
||||
// AdminWildcard
|
||||
//
|
||||
// @Router /admin/* [get]
|
||||
func (w *WebController) AdminWildcard(ctx fiber.Ctx) error {
|
||||
adminDist := resolveDistDir("frontend/admin/dist", "../frontend/admin/dist")
|
||||
return sendAssetOrIndex(ctx, adminDist, ctx.Params("*"))
|
||||
}
|
||||
|
||||
// UserIndex
|
||||
//
|
||||
// @Router / [get]
|
||||
func (w *WebController) UserIndex(ctx fiber.Ctx) error {
|
||||
userDist := resolveDistDir("frontend/user/dist", "../frontend/user/dist")
|
||||
return sendIndex(ctx, userDist)
|
||||
}
|
||||
|
||||
// UserWildcard
|
||||
//
|
||||
// @Router /* [get]
|
||||
func (w *WebController) UserWildcard(ctx fiber.Ctx) error {
|
||||
userDist := resolveDistDir("frontend/user/dist", "../frontend/user/dist")
|
||||
return sendAssetOrIndex(ctx, userDist, ctx.Params("*"))
|
||||
}
|
||||
Reference in New Issue
Block a user