142 lines
3.9 KiB
Go
142 lines
3.9 KiB
Go
// package router
|
|
// @Link https://github.com/xiujiecn/xiu-admin
|
|
// @Copyright Copyright (c) 2025 LiXiujie
|
|
// @Author Lxj <xxx@example.com>
|
|
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
|
|
package router
|
|
|
|
import (
|
|
"context"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/service"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
)
|
|
|
|
var legacyExportStaticPath = regexp.MustCompile(`(?i)^/(?:app/)?upload/(?:[^/]+/)?user/file/(?:[^/]+/)?export(?:/|$)`)
|
|
|
|
// InitRouter 初始化路由
|
|
func InitRouter(ctx context.Context, s *ghttp.Server) {
|
|
// Run before static serving as well as API routing; legacy export files must
|
|
// never bypass download-center authorization through the upload mount.
|
|
s.BindHookHandler("/*", ghttp.HookBeforeServe, func(r *ghttp.Request) {
|
|
if legacyExportStaticPath.MatchString(path.Clean(strings.ReplaceAll(r.URL.Path, `\`, "/"))) {
|
|
r.Response.WriteStatus(http.StatusNotFound)
|
|
r.ExitAll()
|
|
}
|
|
})
|
|
uploadPath := g.Cfg().MustGet(ctx, "system.upload.local.path").String()
|
|
if uploadPath != "" {
|
|
s.AddStaticPath("/upload", resolveUploadStaticPath(uploadPath))
|
|
}
|
|
s.Group("/api/v1", func(group *ghttp.RouterGroup) {
|
|
group.Middleware(
|
|
service.Middleware().Ctx,
|
|
service.Middleware().ResponseHandler,
|
|
service.Middleware().CORS,
|
|
)
|
|
System(ctx, group)
|
|
Monitor(ctx, group)
|
|
Common(ctx, group)
|
|
GenCodes(ctx, group)
|
|
Gen(ctx, group)
|
|
Iot(ctx, group)
|
|
Agent(ctx, group)
|
|
Home(ctx, group)
|
|
Pay(ctx, group)
|
|
Trade(ctx, group)
|
|
})
|
|
//操作日志
|
|
s.BindHookHandler("/*", ghttp.HookAfterOutput, func(r *ghttp.Request) {
|
|
service.Middleware().OperationLog(r)
|
|
})
|
|
}
|
|
|
|
func resolveUploadStaticPath(uploadPath string) string {
|
|
uploadPath = strings.TrimSpace(uploadPath)
|
|
if uploadPath == "" {
|
|
return ""
|
|
}
|
|
for _, candidate := range uploadStaticPathCandidates(uploadPath) {
|
|
if info, err := os.Stat(candidate); err == nil && info.IsDir() {
|
|
return candidate
|
|
}
|
|
}
|
|
return uploadPath
|
|
}
|
|
|
|
func uploadStaticPathCandidates(uploadPath string) []string {
|
|
var candidates []string
|
|
for _, root := range []string{
|
|
os.Getenv("IOTLCM_SERVER_UPLOAD_ROOT"),
|
|
os.Getenv("IOTLCM_UPLOAD_ROOT"),
|
|
uploadPath,
|
|
} {
|
|
root = strings.TrimSpace(root)
|
|
if root == "" {
|
|
continue
|
|
}
|
|
candidates = appendUploadStaticPathCandidate(candidates, root)
|
|
}
|
|
return candidates
|
|
}
|
|
|
|
func appendUploadStaticPathCandidate(candidates []string, root string) []string {
|
|
for _, equivalent := range uploadStaticEquivalentPaths(root) {
|
|
if filepath.IsAbs(equivalent) {
|
|
candidates = appendUniqueStaticPath(candidates, equivalent)
|
|
continue
|
|
}
|
|
for _, base := range uploadStaticBaseDirs() {
|
|
candidates = appendUniqueStaticPath(candidates, filepath.Join(base, equivalent))
|
|
}
|
|
candidates = appendUniqueStaticPath(candidates, equivalent)
|
|
}
|
|
return candidates
|
|
}
|
|
|
|
func uploadStaticEquivalentPaths(root string) []string {
|
|
root = strings.TrimSpace(root)
|
|
if root == "" {
|
|
return nil
|
|
}
|
|
paths := []string{root}
|
|
normalized := filepath.ToSlash(filepath.Clean(root))
|
|
if normalized == "resource/upload" || strings.HasSuffix(normalized, "/resource/upload") {
|
|
prefix := strings.TrimSuffix(normalized, "resource/upload")
|
|
paths = appendUniqueStaticPath(paths, filepath.FromSlash(prefix+"upload"))
|
|
}
|
|
return paths
|
|
}
|
|
|
|
func uploadStaticBaseDirs() []string {
|
|
var dirs []string
|
|
if wd, err := os.Getwd(); err == nil && strings.TrimSpace(wd) != "" {
|
|
dirs = appendUniqueStaticPath(dirs, wd)
|
|
}
|
|
if exe, err := os.Executable(); err == nil && strings.TrimSpace(exe) != "" {
|
|
dirs = appendUniqueStaticPath(dirs, filepath.Dir(exe))
|
|
}
|
|
return dirs
|
|
}
|
|
|
|
func appendUniqueStaticPath(values []string, value string) []string {
|
|
value = strings.TrimSpace(value)
|
|
if value == "" {
|
|
return values
|
|
}
|
|
cleaned := filepath.Clean(value)
|
|
for _, existing := range values {
|
|
if filepath.Clean(strings.TrimSpace(existing)) == cleaned {
|
|
return values
|
|
}
|
|
}
|
|
return append(values, value)
|
|
}
|