89 lines
2.0 KiB
Go
89 lines
2.0 KiB
Go
// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
|
|
|
package actionutils
|
|
|
|
import (
|
|
"bytes"
|
|
"github.com/TeaOSLab/EdgeUser/internal/configloaders"
|
|
"github.com/iwind/TeaGo/Tea"
|
|
"github.com/iwind/TeaGo/maps"
|
|
"github.com/iwind/TeaGo/types"
|
|
"net/http"
|
|
"regexp"
|
|
)
|
|
|
|
var faviconReg = regexp.MustCompile(`<link\s+rel="icon"[^>]+>`)
|
|
|
|
type PortalAction struct {
|
|
ParentAction
|
|
}
|
|
|
|
func (this *PortalAction) IsFrontRequest() bool {
|
|
return len(this.Request.URL.Query().Get("X_FROM_FRONT")) > 0
|
|
}
|
|
|
|
func (this *PortalAction) Success() {
|
|
if this.IsFrontRequest() {
|
|
this.prepareCORSHeader()
|
|
}
|
|
this.ActionObject.Success()
|
|
}
|
|
|
|
func (this *PortalAction) Show() {
|
|
if this.IsFrontRequest() {
|
|
this.prepareCORSHeader()
|
|
this.ActionObject.Success()
|
|
return
|
|
}
|
|
|
|
// add header
|
|
this.ViewFunc("X_VIEW_DATA", func() string {
|
|
if this.Data == nil {
|
|
return ""
|
|
}
|
|
return string(this.Data.AsJSON())
|
|
})
|
|
this.ViewFunc("X_VIEW_FAVICON", func() string {
|
|
uiConfig, err := configloaders.LoadUIConfig()
|
|
if err != nil || uiConfig == nil {
|
|
return ""
|
|
}
|
|
if uiConfig.FaviconFileId > 0 {
|
|
return `<link rel="icon" href="/ui/image/` + types.String(uiConfig.FaviconFileId) + `"/>`
|
|
}
|
|
return ""
|
|
})
|
|
|
|
this.TemplateFilter(func(body []byte) []byte {
|
|
// head
|
|
body = bytes.ReplaceAll(body, []byte("</head>"), []byte(`
|
|
<script>
|
|
window.X_FROM_SERVER = true;
|
|
window.X_VIEW_DATA = {$ X_VIEW_DATA };
|
|
</script>
|
|
</head>`))
|
|
|
|
// favicon
|
|
body = faviconReg.ReplaceAll(body, []byte(`{$ X_VIEW_FAVICON }`))
|
|
|
|
return body
|
|
})
|
|
|
|
this.ViewDir(Tea.Root)
|
|
this.ActionObject.Show()
|
|
}
|
|
|
|
func (this *PortalAction) Redirect(url string, status int) {
|
|
if this.IsFrontRequest() {
|
|
this.Data["X_FRONT_REDIRECT"] = maps.Map{"url": url}
|
|
this.Success()
|
|
return
|
|
}
|
|
http.Redirect(this.ResponseWriter, this.Request, url, status)
|
|
}
|
|
|
|
func (this *PortalAction) prepareCORSHeader() {
|
|
this.ResponseWriter.Header().Set("Access-Control-Allow-Origin", this.Request.Header.Get("Origin"))
|
|
this.ResponseWriter.Header().Set("Access-Control-Allow-Credentials", "true")
|
|
}
|