mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 02:35:49 +08:00
feat: can modify static resource url by app.conf (#1045)
* feat: can modify static resource url by app.conf Signed-off-by: magicwind <2814461814@qq.com> * Update static_filter.go Signed-off-by: magicwind <2814461814@qq.com> Co-authored-by: Yang Luo <hsluoyz@qq.com>
This commit is contained in:
parent
39c6bd5850
commit
64f787fab5
3
main.go
3
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")
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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"
|
||||
/>
|
||||
<link rel="apple-touch-icon" href="https://cdn.casdoor.com/static/favicon.png" />
|
||||
<link rel="apple-touch-icon" href="https://cdn.casbin.org/img/favicon.png" />
|
||||
<!--
|
||||
manifest.json provides metadata used when your web app is installed on a
|
||||
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
|
||||
|
@ -13,7 +13,7 @@ code {
|
||||
}
|
||||
|
||||
.logo {
|
||||
background: url("https://cdn.casdoor.com/logo/casdoor-logo_1185x256.png");
|
||||
background: url("https://cdn.casbin.org/img/casdoor-logo_1185x256.png");
|
||||
background-size: 130px, 27px;
|
||||
width: 130px;
|
||||
height: 27px;
|
||||
|
Loading…
x
Reference in New Issue
Block a user