fix: swagger
This commit is contained in:
@@ -1,78 +0,0 @@
|
||||
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("*"))
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/container"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
"go.ipao.vip/atom/opt"
|
||||
)
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
if err := container.Container.Provide(func() (*WebController, error) {
|
||||
obj := &WebController{}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func(
|
||||
webController *WebController,
|
||||
) (contracts.HttpRoute, error) {
|
||||
obj := &Routes{
|
||||
webController: webController,
|
||||
}
|
||||
if err := obj.Prepare(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
}, atom.GroupRoutes); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
// Code generated by atomctl. DO NOT EDIT.
|
||||
|
||||
// Package web provides HTTP route definitions and registration
|
||||
// for the quyun/v2 application.
|
||||
package web
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
_ "go.ipao.vip/atom"
|
||||
_ "go.ipao.vip/atom/contracts"
|
||||
. "go.ipao.vip/atom/fen"
|
||||
)
|
||||
|
||||
// Routes implements the HttpRoute contract and provides route registration
|
||||
// for all controllers in the web module.
|
||||
//
|
||||
// @provider contracts.HttpRoute atom.GroupRoutes
|
||||
type Routes struct {
|
||||
log *log.Entry `inject:"false"`
|
||||
// Controller instances
|
||||
webController *WebController
|
||||
}
|
||||
|
||||
// Prepare initializes the routes provider with logging configuration.
|
||||
func (r *Routes) Prepare() error {
|
||||
r.log = log.WithField("module", "routes.web")
|
||||
r.log.Info("Initializing routes module")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Name returns the unique identifier for this routes provider.
|
||||
func (r *Routes) Name() string {
|
||||
return "web"
|
||||
}
|
||||
|
||||
// Register registers all HTTP routes with the provided fiber router.
|
||||
// Each route is registered with its corresponding controller action and parameter bindings.
|
||||
func (r *Routes) Register(router fiber.Router) {
|
||||
// Register routes for controller: WebController
|
||||
r.log.Debugf("Registering route: Get / -> webController.UserIndex")
|
||||
router.Get("/", Func0(
|
||||
r.webController.UserIndex,
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /* -> webController.UserWildcard")
|
||||
router.Get("/*", Func0(
|
||||
r.webController.UserWildcard,
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /admin -> webController.AdminIndex")
|
||||
router.Get("/admin", Func0(
|
||||
r.webController.AdminIndex,
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /admin/* -> webController.AdminWildcard")
|
||||
router.Get("/admin/*", Func0(
|
||||
r.webController.AdminWildcard,
|
||||
))
|
||||
|
||||
r.log.Info("Successfully registered all routes")
|
||||
}
|
||||
Reference in New Issue
Block a user