Files
EdgeAdmin/internal/web/actions/default/ui/components.go

86 lines
2.2 KiB
Go
Raw Normal View History

2020-07-22 22:19:39 +08:00
package ui
import (
2021-01-28 17:04:36 +08:00
"bytes"
2020-09-29 11:28:39 +08:00
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/conds/condutils"
2021-06-09 17:14:31 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
2020-07-22 22:19:39 +08:00
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/files"
"github.com/iwind/TeaGo/logs"
)
type ComponentsAction actions.Action
2021-01-28 17:04:36 +08:00
var componentsData = []byte{}
2020-07-22 22:19:39 +08:00
func (this *ComponentsAction) RunGet(params struct{}) {
this.AddHeader("Content-Type", "text/javascript; charset=utf-8")
2021-01-28 17:04:36 +08:00
if !Tea.IsTesting() && len(componentsData) > 0 {
this.AddHeader("Last-Modified", "Fri, 06 Sep 2019 08:29:50 GMT")
2021-01-28 17:04:36 +08:00
this.Write(componentsData)
return
}
var buffer = bytes.NewBuffer([]byte{})
2020-07-22 22:19:39 +08:00
var webRoot string
if Tea.IsTesting() {
webRoot = Tea.Root + "/../web/public/js/components/"
} else {
webRoot = Tea.Root + "/web/public/js/components/"
}
f := files.NewFile(webRoot)
f.Range(func(file *files.File) {
if !file.IsFile() {
return
}
if file.Ext() != ".js" {
return
}
data, err := file.ReadAll()
if err != nil {
logs.Error(err)
return
}
2021-01-28 17:04:36 +08:00
buffer.Write(data)
buffer.Write([]byte{'\n', '\n'})
2020-07-22 22:19:39 +08:00
})
2020-09-29 11:28:39 +08:00
// 条件组件
typesJSON, err := json.Marshal(condutils.ReadAllAvailableCondTypes())
if err != nil {
2021-06-09 17:14:31 +08:00
logs.Println("ComponentsAction marshal request cond types failed: " + err.Error())
2020-09-29 11:28:39 +08:00
} else {
2021-01-28 17:04:36 +08:00
buffer.WriteString("window.REQUEST_COND_COMPONENTS = ")
buffer.Write(typesJSON)
buffer.Write([]byte{'\n', '\n'})
2020-09-29 11:28:39 +08:00
}
2021-01-28 17:04:36 +08:00
2021-06-09 17:14:31 +08:00
// 条件操作符
requestOperatorsJSON, err := json.Marshal(shared.AllRequestOperators())
if err != nil {
logs.Println("ComponentsAction marshal request operators failed: " + err.Error())
} else {
buffer.WriteString("window.REQUEST_COND_OPERATORS = ")
buffer.Write(requestOperatorsJSON)
buffer.Write([]byte{'\n', '\n'})
}
// 请求变量
requestVariablesJSON, err := json.Marshal(shared.DefaultRequestVariables())
if err != nil {
logs.Println("ComponentsAction marshal request variables failed: " + err.Error())
} else {
buffer.WriteString("window.REQUEST_VARIABLES = ")
buffer.Write(requestVariablesJSON)
buffer.Write([]byte{'\n', '\n'})
}
2021-01-28 17:04:36 +08:00
componentsData = buffer.Bytes()
this.Write(componentsData)
2020-07-22 22:19:39 +08:00
}