This is an automated email from the ASF dual-hosted git repository.

hsluoyz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/casbin-gateway.git


The following commit(s) were added to refs/heads/master by this push:
     new b3e95a8  fix: support Vite build layout in CDN upload
b3e95a8 is described below

commit b3e95a863f4beef5bd1029a932af748fb7ebf3d2
Author: Yang Luo <[email protected]>
AuthorDate: Wed Aug 19 01:39:06 2026 +0800

    fix: support Vite build layout in CDN upload
---
 main.go      |  2 --
 run/cdn.go   | 62 ++++++++++++++++++++++++++++++++++++++++++++----------------
 util/path.go |  6 +++---
 3 files changed, 49 insertions(+), 21 deletions(-)

diff --git a/main.go b/main.go
index 7a6f887..496486c 100644
--- a/main.go
+++ b/main.go
@@ -74,8 +74,6 @@ func main() {
                AllowCredentials: true,
        }))
 
-       //beego.DelStaticPath("/static")
-       beego.SetStaticPath("/static", "web/build/static")
        // https://studygolang.com/articles/2303
        beego.InsertFilter("/", beego.BeforeRouter, routers.TransparentStatic) 
// must has this for default page
        beego.InsertFilter("/*", beego.BeforeRouter, routers.TransparentStatic)
diff --git a/run/cdn.go b/run/cdn.go
index f333098..6cb5997 100644
--- a/run/cdn.go
+++ b/run/cdn.go
@@ -25,10 +25,10 @@ import (
        "github.com/apache/casbin-gateway/util"
 )
 
-func filterFiles(filenames []string, folder string, siteName string) []string {
+func filterFiles(filenames []string, ext string, siteName string) []string {
        res := []string{}
        for _, filename := range filenames {
-               if !strings.HasSuffix(filename, folder) {
+               if !strings.HasSuffix(filename, ext) {
                        continue
                }
 
@@ -43,42 +43,67 @@ func filterFiles(filenames []string, folder string, 
siteName string) []string {
        return res
 }
 
-func uploadFolder(provider storage.StorageProvider, buildDir string, folder 
string, siteName string) (string, error) {
+// assetDirs tells where a web build keeps its hashed assets. Create React App
+// splits them into "static/js" and "static/css", Vite puts both in "assets".
+type assetDirs struct {
+       root string
+       js   string
+       css  string
+}
+
+func getAssetDirs(buildDir string) (*assetDirs, error) {
+       if util.FileExist(filepath.Join(buildDir, "static")) {
+               return &assetDirs{root: "static", js: "static/js", css: 
"static/css"}, nil
+       }
+
+       if util.FileExist(filepath.Join(buildDir, "assets")) {
+               return &assetDirs{root: "assets", js: "assets", css: "assets"}, 
nil
+       }
+
+       return nil, fmt.Errorf("getAssetDirs() error: no \"static\" or 
\"assets\" folder in buildDir = %s", buildDir)
+}
+
+func uploadFolder(provider storage.StorageProvider, buildDir string, root 
string, dir string, ext string, siteName string) (string, error) {
        domainUrl := ""
 
-       path := filepath.Join(buildDir, "static", folder)
-       filenames := util.ListFiles(path)
-       filteredFilenames := filterFiles(filenames, folder, siteName)
+       urlRoot := "/" + root
+       dirPath := filepath.Join(buildDir, filepath.FromSlash(dir))
+       filenames, err := util.ListFiles(dirPath)
+       if err != nil {
+               return "", err
+       }
+
+       filteredFilenames := filterFiles(filenames, ext, siteName)
        for _, filename := range filteredFilenames {
-               data, err := os.ReadFile(filepath.Join(path, filename))
+               data, err := os.ReadFile(filepath.Join(dirPath, filename))
                if err != nil {
                        return "", err
                }
                fileBuffer := bytes.NewBuffer(data)
 
-               objectKey := strings.ReplaceAll(filepath.Join("static", folder, 
filename), "\\", "/")
+               objectKey := fmt.Sprintf("%s/%s", dir, filename)
                fileUrl, err := provider.PutObject("Built-in-Untracked", "", 
objectKey, fileBuffer)
                if err != nil {
                        return "", err
                }
 
-               index := strings.Index(fileUrl, "/static")
+               index := strings.Index(fileUrl, urlRoot)
                if index == -1 {
-                       return "", fmt.Errorf("uploadFolder() error, fileUrl 
should contain \"/static/\", fileUrl = %s", fileUrl)
+                       return "", fmt.Errorf("uploadFolder() error, fileUrl 
should contain \"%s/\", fileUrl = %s", urlRoot, fileUrl)
                }
 
-               domainUrl = fileUrl[:index+len("/static")] + "/"
+               domainUrl = fileUrl[:index+len(urlRoot)] + "/"
                fmt.Printf("uploadFolder(): [/%s] -> [%s]\n", objectKey, 
fileUrl)
        }
 
        return domainUrl, nil
 }
 
-func updateHtml(domainUrl string, buildDir string) {
+func updateHtml(domainUrl string, buildDir string, root string) {
        htmlPath := filepath.Join(buildDir, "index.html")
        html := util.ReadStringFromPath(htmlPath)
 
-       html = strings.Replace(html, "\"/static/", fmt.Sprintf("\"%s", 
domainUrl), -1)
+       html = strings.Replace(html, fmt.Sprintf("\"/%s/", root), 
fmt.Sprintf("\"%s", domainUrl), -1)
        util.WriteStringToPath(html, htmlPath)
 
        fmt.Printf("updateHtml(): index.html content:\n%s\n%s\n%s\n", 
strings.Repeat("=", 80), html, strings.Repeat("=", 80))
@@ -94,22 +119,27 @@ func gitUploadCdn(providerName string, siteName string) 
error {
        path := GetRepoPath(siteName)
        buildDir := filepath.Join(path, "web/build")
 
+       dirs, err := getAssetDirs(buildDir)
+       if err != nil {
+               return err
+       }
+
        provider, err := storage.GetStorageProvider(providerName)
        if err != nil {
                return err
        }
 
        var domainUrl string
-       domainUrl, err = uploadFolder(provider, buildDir, "js", siteName)
+       domainUrl, err = uploadFolder(provider, buildDir, dirs.root, dirs.js, 
"js", siteName)
        if err != nil {
                return err
        }
 
-       _, err = uploadFolder(provider, buildDir, "css", siteName)
+       _, err = uploadFolder(provider, buildDir, dirs.root, dirs.css, "css", 
siteName)
        if err != nil {
                return err
        }
 
-       updateHtml(domainUrl, buildDir)
+       updateHtml(domainUrl, buildDir, dirs.root)
        return nil
 }
diff --git a/util/path.go b/util/path.go
index e10df06..d2ddcbf 100644
--- a/util/path.go
+++ b/util/path.go
@@ -26,12 +26,12 @@ func FileExist(path string) bool {
        return true
 }
 
-func ListFiles(path string) []string {
+func ListFiles(path string) ([]string, error) {
        res := []string{}
 
        files, err := ioutil.ReadDir(path)
        if err != nil {
-               panic(err)
+               return nil, err
        }
 
        for _, f := range files {
@@ -40,5 +40,5 @@ func ListFiles(path string) []string {
                }
        }
 
-       return res
+       return res, nil
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to