diff --git a/main.go b/main.go index f565663f..d1c409aa 100644 --- a/main.go +++ b/main.go @@ -45,7 +45,8 @@ func main() { util.SafeGoroutine(func() { object.RunSyncUsersJob() }) // beego.DelStaticPath("/static") - beego.SetStaticPath("/static", "web/build/static") + // beego.SetStaticPath("/static", "web/build/static") + beego.BConfig.WebConfig.DirectoryIndex = true beego.SetStaticPath("/swagger", "swagger") beego.SetStaticPath("/files", "files") diff --git a/routers/static_filter.go b/routers/static_filter.go index 2e7bc094..7c1ad5c5 100644 --- a/routers/static_filter.go +++ b/routers/static_filter.go @@ -16,12 +16,19 @@ package routers import ( "net/http" + "os" "strings" + "github.com/astaxie/beego" "github.com/astaxie/beego/context" "github.com/casdoor/casdoor/util" ) +var ( + oldStaticBaseUrl = "https://cdn.casbin.org" + newStaticBaseUrl = beego.AppConfig.String("staticBaseUrl") +) + func StaticFilter(ctx *context.Context) { urlPath := ctx.Request.URL.Path if strings.HasPrefix(urlPath, "/api/") || strings.HasPrefix(urlPath, "/.well-known/") { @@ -38,9 +45,35 @@ func StaticFilter(ctx *context.Context) { path += urlPath } - if util.FileExist(path) { + if !util.FileExist(path) { + path = "web/build/index.html" + } + + if oldStaticBaseUrl == newStaticBaseUrl { http.ServeFile(ctx.ResponseWriter, ctx.Request, path) } else { - http.ServeFile(ctx.ResponseWriter, ctx.Request, "web/build/index.html") + serveFileWithReplace(ctx.ResponseWriter, ctx.Request, path, oldStaticBaseUrl, newStaticBaseUrl) + } +} + +func serveFileWithReplace(w http.ResponseWriter, r *http.Request, name string, old string, new string) { + f, err := os.Open(name) + if err != nil { + panic(err) + } + defer f.Close() + + d, err := f.Stat() + if err != nil { + panic(err) + } + + oldContent := util.ReadStringFromPath(name) + newContent := strings.ReplaceAll(oldContent, old, new) + + http.ServeContent(w, r, d.Name(), d.ModTime(), strings.NewReader(newContent)) + _, err = w.Write([]byte(newContent)) + if err != nil { + panic(err) } } diff --git a/web/public/index.html b/web/public/index.html index 238eb6a2..07ed7f1e 100644 --- a/web/public/index.html +++ b/web/public/index.html @@ -19,7 +19,7 @@ name="description" content="Casdoor - An Identity and Access Management (IAM) / Single-Sign-On (SSO) platform with web UI supporting OAuth 2.0, OIDC, SAML and CAS" /> - +