阶段性提交

This commit is contained in:
GoEdgeLab
2020-08-21 12:32:16 +08:00
parent e6266b7dc3
commit 8f22f2fda1
181 changed files with 4897 additions and 218 deletions

View File

@@ -0,0 +1,35 @@
package common
import (
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/maps"
)
type ChangedClustersAction struct {
actionutils.ParentAction
}
func (this *ChangedClustersAction) Init() {
this.Nav("", "", "")
}
func (this *ChangedClustersAction) RunGet(params struct{}) {
resp, err := this.RPC().NodeClusterRPC().FindAllChangedClusters(this.AdminContext(), &pb.FindAllChangedClustersRequest{})
if err != nil {
this.ErrorPage(err)
return
}
result := []maps.Map{}
for _, cluster := range resp.Clusters {
result = append(result, maps.Map{
"id": cluster.Id,
"name": cluster.Name,
})
}
this.Data["clusters"] = result
this.Success()
}

View File

@@ -0,0 +1,17 @@
package common
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(new(helpers.UserMustAuth)).
Prefix("/common").
Get("/changedClusters", new(ChangedClustersAction)).
Post("/syncClusters", new(SyncClustersAction)).
EndAll()
})
}

View File

@@ -0,0 +1,34 @@
package common
import (
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
)
type SyncClustersAction struct {
actionutils.ParentAction
}
func (this *SyncClustersAction) RunPost(params struct{}) {
// TODO 将来可以单独选择某一个集群进行单独的同步
// 所有有变化的集群
clustersResp, err := this.RPC().NodeClusterRPC().FindAllChangedClusters(this.AdminContext(), &pb.FindAllChangedClustersRequest{})
if err != nil {
this.ErrorPage(err)
return
}
clusters := clustersResp.Clusters
for _, cluster := range clusters {
_, err := this.RPC().NodeRPC().SyncNodesVersionWithCluster(this.AdminContext(), &pb.SyncNodesVersionWithClusterRequest{
ClusterId: cluster.Id,
})
if err != nil {
this.ErrorPage(err)
return
}
}
this.Success()
}

View File

@@ -1,9 +1,14 @@
package nodes
import (
"encoding/json"
"fmt"
"github.com/TeaOSLab/EdgeAdmin/internal/configs/nodes"
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/maps"
"time"
)
type IndexAction struct {
@@ -30,9 +35,29 @@ func (this *IndexAction) RunGet(params struct{}) {
})
nodeMaps := []maps.Map{}
for _, node := range nodesResp.Nodes {
// 状态
status := &nodes.NodeStatus{}
if len(node.Status) > 0 && node.Status != "null" {
err = json.Unmarshal([]byte(node.Status), &status)
if err != nil {
logs.Error(err)
continue
}
status.IsActive = time.Now().Unix()-status.UpdatedAt < 120 // 2分钟之内认为活跃
}
nodeMaps = append(nodeMaps, maps.Map{
"id": node.Id,
"name": node.Name,
"status": maps.Map{
"isActive": status.IsActive,
"updatedAt": status.UpdatedAt,
"hostname": status.Hostname,
"cpuUsage": status.CPUUsage,
"cpuUsageText": fmt.Sprintf("%.2f%%", status.CPUUsage*100),
"memUsage": status.MemoryUsage,
"memUsageText": fmt.Sprintf("%.2f%%", status.MemoryUsage*100),
},
"cluster": maps.Map{
"id": node.Cluster.Id,
"name": node.Cluster.Name,

View File

@@ -0,0 +1,58 @@
package servers
import (
"github.com/TeaOSLab/EdgeAdmin/internal/configs/serverconfigs"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/rands"
"regexp"
"strings"
)
type AddOriginPopupAction struct {
actionutils.ParentAction
}
func (this *AddOriginPopupAction) Init() {
this.Nav("", "", "")
}
func (this *AddOriginPopupAction) RunGet(params struct {
ServerType string
}) {
this.Data["serverType"] = params.ServerType
this.Show()
}
func (this *AddOriginPopupAction) RunPost(params struct {
Protocol string
Addr string
Must *actions.Must
}) {
params.Must.
Field("addr", params.Addr).
Require("请输入源站地址")
addr := regexp.MustCompile(`\s+`).ReplaceAllString(params.Addr, "")
portIndex := strings.LastIndex(params.Addr, ":")
if portIndex < 0 {
this.Fail("地址中需要带有端口")
}
host := addr[:portIndex]
port := addr[portIndex+1:]
origin := &serverconfigs.OriginServerConfig{
Id: rands.HexString(32),
IsOn: true,
Addr: &serverconfigs.NetworkAddressConfig{
Protocol: params.Protocol,
Host: host,
PortRange: port,
},
}
this.Data["origin"] = origin
this.Success()
}

View File

@@ -0,0 +1,61 @@
package servers
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
"regexp"
"strings"
)
type AddPortPopupAction struct {
actionutils.ParentAction
}
func (this *AddPortPopupAction) Init() {
this.Nav("", "", "")
}
func (this *AddPortPopupAction) RunGet(params struct {
ServerType string
}) {
this.Data["protocols"] = serverutils.AllServerProtocolsForType(params.ServerType)
this.Show()
}
func (this *AddPortPopupAction) RunPost(params struct {
Protocol string
Address string
Must *actions.Must
}) {
// 校验地址
addr := maps.Map{
"protocol": params.Protocol,
"host": "",
"portRange": "",
}
// TODO 判断端口不能小于1
// TODO 判断端口号不能大于65535
digitRegexp := regexp.MustCompile(`^\d+$`)
if digitRegexp.MatchString(params.Address) {
addr["portRange"] = params.Address
} else if strings.Contains(params.Address, ":") {
index := strings.LastIndex(params.Address, ":")
addr["host"] = strings.TrimSpace(params.Address[:index])
port := strings.TrimSpace(params.Address[index+1:])
if !digitRegexp.MatchString(port) {
this.Fail("端口只能是一个数字")
}
addr["portRange"] = port
} else {
this.Fail("请输入正确的端口或者网络地址")
}
this.Data["address"] = addr
this.Success()
}

View File

@@ -0,0 +1,35 @@
package servers
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
)
type AddServerNamePopupAction struct {
actionutils.ParentAction
}
func (this *AddServerNamePopupAction) Init() {
this.Nav("", "", "")
}
func (this *AddServerNamePopupAction) RunGet(params struct{}) {
this.Show()
}
func (this *AddServerNamePopupAction) RunPost(params struct {
ServerName string
Must *actions.Must
}) {
params.Must.
Field("serverName", params.ServerName).
Require("请输入域名")
this.Data["serverName"] = maps.Map{
"name": params.ServerName,
"type": "full",
}
this.Success()
}

View File

@@ -2,9 +2,10 @@ package servers
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/configs/nodes"
"github.com/TeaOSLab/EdgeAdmin/internal/configs/serverconfigs"
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
)
@@ -14,7 +15,7 @@ type CreateAction struct {
}
func (this *CreateAction) Init() {
this.Nav("", "", "create")
this.Nav("", "server", "create")
}
func (this *CreateAction) RunGet(params struct{}) {
@@ -36,12 +37,23 @@ func (this *CreateAction) RunGet(params struct{}) {
}
this.Data["clusters"] = clusterMaps
// 服务类型
this.Data["serverTypes"] = serverutils.AllServerTypes()
this.Show()
}
func (this *CreateAction) RunPost(params struct {
Name string
ClusterId int64
Name string
Description string
ClusterId int64
ServerType string
Addresses string
ServerNames string
Origins string
WebRoot string
Must *actions.Must
}) {
@@ -56,9 +68,114 @@ func (this *CreateAction) RunPost(params struct {
// TODO 验证集群ID
// 配置
serverConfig := &nodes.ServerConfig{}
serverConfig := &serverconfigs.ServerConfig{}
serverConfig.IsOn = true
serverConfig.Name = params.Name
serverConfig.Description = params.Description
// 端口地址
switch params.ServerType {
case serverutils.ServerTypeHTTPProxy, serverutils.ServerTypeHTTPWeb:
listen := []*serverconfigs.NetworkAddressConfig{}
err := json.Unmarshal([]byte(params.Addresses), &listen)
if err != nil {
this.Fail("端口地址解析失败:" + err.Error())
}
for _, addr := range listen {
switch addr.Protocol {
case serverconfigs.ProtocolHTTP, serverconfigs.ProtocolHTTP4, serverconfigs.ProtocolHTTP6:
if serverConfig.HTTP == nil {
serverConfig.HTTP = &serverconfigs.HTTPProtocolConfig{
BaseProtocol: serverconfigs.BaseProtocol{
IsOn: true,
},
}
}
serverConfig.HTTP.AddListen(addr)
case serverconfigs.ProtocolHTTPS, serverconfigs.ProtocolHTTPS4, serverconfigs.ProtocolHTTPS6:
if serverConfig.HTTPS == nil {
serverConfig.HTTPS = &serverconfigs.HTTPSProtocolConfig{
BaseProtocol: serverconfigs.BaseProtocol{
IsOn: true,
},
}
}
serverConfig.HTTPS.AddListen(addr)
}
}
case serverutils.ServerTypeTCPProxy:
listen := []*serverconfigs.NetworkAddressConfig{}
err := json.Unmarshal([]byte(params.Addresses), &listen)
if err != nil {
this.Fail("端口地址解析失败:" + err.Error())
}
for _, addr := range listen {
switch addr.Protocol {
case serverconfigs.ProtocolTCP, serverconfigs.ProtocolTCP4, serverconfigs.ProtocolTCP6:
if serverConfig.TCP == nil {
serverConfig.TCP = &serverconfigs.TCPProtocolConfig{
BaseProtocol: serverconfigs.BaseProtocol{
IsOn: true,
},
}
}
serverConfig.TCP.AddListen(addr)
case serverconfigs.ProtocolTLS, serverconfigs.ProtocolTLS4, serverconfigs.ProtocolTLS6:
if serverConfig.TLS == nil {
serverConfig.TLS = &serverconfigs.TLSProtocolConfig{
BaseProtocol: serverconfigs.BaseProtocol{
IsOn: true,
},
}
}
serverConfig.TLS.AddListen(addr)
}
}
default:
this.Fail("请选择正确的服务类型")
}
// TODO 证书
// 域名
serverNames := []*serverconfigs.ServerNameConfig{}
err := json.Unmarshal([]byte(params.ServerNames), &serverNames)
if err != nil {
this.Fail("域名解析失败:" + err.Error())
}
serverConfig.ServerNames = serverNames
// 源站地址
switch params.ServerType {
case serverutils.ServerTypeHTTPProxy, serverutils.ServerTypeTCPProxy:
origins := []*serverconfigs.OriginServerConfig{}
err = json.Unmarshal([]byte(params.Origins), &origins)
if err != nil {
this.Fail("源站地址解析失败:" + err.Error())
}
serverConfig.ReverseProxy = &serverconfigs.ReverseProxyConfig{
IsOn: true,
Origins: origins,
}
}
// Web地址
switch params.ServerType {
case serverutils.ServerTypeHTTPWeb:
serverConfig.Web = &serverconfigs.WebConfig{
IsOn: true,
Root: params.WebRoot,
}
}
// 校验
err = serverConfig.Init()
if err != nil {
this.Fail("配置校验失败:" + err.Error())
}
serverConfigJSON, err := serverConfig.AsJSON()
if err != nil {
this.ErrorPage(err)

View File

@@ -1,6 +1,7 @@
package servers
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/actions"
)
@@ -13,4 +14,10 @@ func NewHelper() *Helper {
func (this *Helper) BeforeAction(action *actions.ActionObject) {
action.Data["teaMenu"] = "servers"
selectedTabbar, _ := action.Data["mainTab"]
tabbar := actionutils.NewTabbar()
tabbar.Add("服务管理", "", "/servers", "", selectedTabbar == "server")
actionutils.SetTabbar(action, tabbar)
}

View File

@@ -2,10 +2,12 @@ package servers
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/configs/nodes"
"github.com/TeaOSLab/EdgeAdmin/internal/configs/serverconfigs"
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/maps"
"strings"
)
type IndexAction struct {
@@ -13,7 +15,7 @@ type IndexAction struct {
}
func (this *IndexAction) Init() {
this.Nav("", "", "index")
this.Nav("", "server", "index")
}
func (this *IndexAction) RunGet(params struct{}) {
@@ -38,12 +40,75 @@ func (this *IndexAction) RunGet(params struct{}) {
serverMaps := []maps.Map{}
for _, server := range serversResp.Servers {
// 服务名
serverConfig := &nodes.ServerConfig{}
serverConfig := &serverconfigs.ServerConfig{}
err = json.Unmarshal(server.Config, &serverConfig)
if err != nil {
this.ErrorPage(err)
return
}
err = serverConfig.Init()
if err != nil {
logs.Println("init server '" + serverConfig.Name + "' error: " + err.Error())
}
serverTypeNames := []string{}
// 端口列表
portMaps := []maps.Map{}
if serverConfig.HTTP != nil && serverConfig.HTTP.IsOn {
serverTypeNames = append(serverTypeNames, "HTTP")
for _, listen := range serverConfig.HTTP.Listen {
portMaps = append(portMaps, maps.Map{
"protocol": listen.Protocol,
"portRange": listen.PortRange,
})
}
}
if serverConfig.HTTPS != nil && serverConfig.HTTPS.IsOn {
serverTypeNames = append(serverTypeNames, "HTTPS")
for _, listen := range serverConfig.HTTPS.Listen {
portMaps = append(portMaps, maps.Map{
"protocol": listen.Protocol,
"portRange": listen.PortRange,
})
}
}
if serverConfig.TCP != nil && serverConfig.TCP.IsOn {
serverTypeNames = append(serverTypeNames, "TCP")
for _, listen := range serverConfig.TCP.Listen {
portMaps = append(portMaps, maps.Map{
"protocol": listen.Protocol,
"portRange": listen.PortRange,
})
}
}
if serverConfig.TLS != nil && serverConfig.TLS.IsOn {
serverTypeNames = append(serverTypeNames, "TLS")
for _, listen := range serverConfig.TLS.Listen {
portMaps = append(portMaps, maps.Map{
"protocol": listen.Protocol,
"portRange": listen.PortRange,
})
}
}
if serverConfig.Unix != nil && serverConfig.Unix.IsOn {
serverTypeNames = append(serverTypeNames, "Unix")
for _, listen := range serverConfig.Unix.Listen {
portMaps = append(portMaps, maps.Map{
"protocol": listen.Protocol,
"portRange": listen.Host,
})
}
}
if serverConfig.UDP != nil && serverConfig.UDP.IsOn {
serverTypeNames = append(serverTypeNames, "UDP")
for _, listen := range serverConfig.UDP.Listen {
portMaps = append(portMaps, maps.Map{
"protocol": listen.Protocol,
"portRange": listen.PortRange,
})
}
}
serverMaps = append(serverMaps, maps.Map{
"id": server.Id,
@@ -52,6 +117,8 @@ func (this *IndexAction) RunGet(params struct{}) {
"id": server.Cluster.Id,
"name": server.Cluster.Name,
},
"ports": portMaps,
"serverTypeName": strings.Join(serverTypeNames, "+"),
})
}
this.Data["servers"] = serverMaps

View File

@@ -13,6 +13,11 @@ func init() {
Prefix("/servers").
Get("", new(IndexAction)).
GetPost("/create", new(CreateAction)).
GetPost("/update", new(UpdateAction)).
GetPost("/addPortPopup", new(AddPortPopupAction)).
GetPost("/addServerNamePopup", new(AddServerNamePopupAction)).
GetPost("/addOriginPopup", new(AddOriginPopupAction)).
EndAll()
})
}

View File

@@ -0,0 +1,15 @@
package board
import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "board", "")
}
func (this *IndexAction) RunGet(params struct{}) {
this.Show()
}

View File

@@ -0,0 +1,18 @@
package board
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth()).
Helper(serverutils.NewServerHelper()).
Prefix("/servers/server/board").
Get("", new(IndexAction)).
EndAll()
})
}

View File

@@ -0,0 +1,15 @@
package delete
import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "delete", "")
}
func (this *IndexAction) RunGet(params struct{}) {
this.Show()
}

View File

@@ -0,0 +1,18 @@
package delete
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth()).
Helper(serverutils.NewServerHelper()).
Prefix("/servers/server/delete").
Get("", new(IndexAction)).
EndAll()
})
}

View File

@@ -0,0 +1,21 @@
package server
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"strconv"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "index", "index")
this.SecondMenu("index")
}
func (this *IndexAction) RunGet(params struct {
ServerId int64
}) {
this.RedirectURL("/servers/server/board?serverId=" + strconv.FormatInt(params.ServerId, 10))
}

View File

@@ -0,0 +1,18 @@
package server
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth()).
Helper(serverutils.NewServerHelper()).
Prefix("/servers/server").
Get("", new(IndexAction)).
EndAll()
})
}

View File

@@ -0,0 +1,15 @@
package log
import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "log", "")
}
func (this *IndexAction) RunGet(params struct{}) {
this.Show()
}

View File

@@ -0,0 +1,18 @@
package log
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth()).
Helper(serverutils.NewServerHelper()).
Prefix("/servers/server/log").
Get("", new(IndexAction)).
EndAll()
})
}

View File

@@ -0,0 +1,20 @@
package server
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "setting", "index")
this.SecondMenu("http")
}
func (this *IndexAction) RunGet(params struct {
ServerId int64
}) {
this.Show()
}

View File

@@ -0,0 +1,18 @@
package server
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth()).
Helper(serverutils.NewServerHelper()).
Prefix("/servers/server/http").
Get("", new(IndexAction)).
EndAll()
})
}

View File

@@ -0,0 +1,16 @@
package settings
import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "setting", "")
this.SecondMenu("basic")
}
func (this *IndexAction) RunGet(params struct{}) {
this.Show()
}

View File

@@ -0,0 +1,18 @@
package settings
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth()).
Helper(serverutils.NewServerHelper()).
Prefix("/servers/server/settings").
Get("", new(IndexAction)).
EndAll()
})
}

View File

@@ -0,0 +1,15 @@
package stat
import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "stat", "")
}
func (this *IndexAction) RunGet(params struct{}) {
this.Show()
}

View File

@@ -0,0 +1,18 @@
package stat
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth()).
Helper(serverutils.NewServerHelper()).
Prefix("/servers/server/stat").
Get("", new(IndexAction)).
EndAll()
})
}

View File

@@ -0,0 +1,212 @@
package serverutils
import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAdmin/internal/configs/serverconfigs"
"github.com/TeaOSLab/EdgeAdmin/internal/rpc"
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
"net/http"
"strconv"
)
type ServerHelper struct {
}
func NewServerHelper() *ServerHelper {
return &ServerHelper{}
}
func (this *ServerHelper) BeforeAction(action *actions.ActionObject) {
if action.Request.Method != http.MethodGet {
return
}
action.Data["teaMenu"] = "servers"
// 左侧菜单
this.createLeftMenu(action)
}
func (this *ServerHelper) createLeftMenu(action *actions.ActionObject) {
// 初始化
action.Data["leftMenuItems"] = []maps.Map{}
mainTab, _ := action.Data["mainTab"]
secondMenuItem, _ := action.Data["secondMenuItem"]
serverId := action.ParamInt64("serverId")
serverIdString := strconv.FormatInt(serverId, 10)
// 读取server信息
rpcClient, err := rpc.SharedRPC()
if err != nil {
logs.Error(err)
return
}
serverResp, err := rpcClient.ServerRPC().FindEnabledServer(rpcClient.Context(action.Context.GetInt64("adminId")), &pb.FindEnabledServerRequest{ServerId: serverId})
if err != nil {
logs.Error(err)
return
}
server := serverResp.Server
if server == nil {
logs.Error(errors.New("can not find the server"))
return
}
// 源站管理
serverConfig := &serverconfigs.ServerConfig{}
err = json.Unmarshal(server.Config, serverConfig)
if err != nil {
logs.Error(err)
return
}
// TABBAR
selectedTabbar, _ := action.Data["mainTab"]
tabbar := actionutils.NewTabbar()
tabbar.Add("当前:"+serverConfig.Name, "", "/servers", "left long alternate arrow", false)
tabbar.Add("看板", "", "/servers/server/board?serverId="+serverIdString, "dashboard", selectedTabbar == "board")
tabbar.Add("日志", "", "/servers/server/log?serverId="+serverIdString, "history", selectedTabbar == "log")
tabbar.Add("统计", "", "/servers/server/stat?serverId="+serverIdString, "chart area", selectedTabbar == "stat")
tabbar.Add("设置", "", "/servers/server/settings?serverId="+serverIdString, "setting", selectedTabbar == "setting")
tabbar.Add("删除", "", "/servers/server/delete?serverId="+serverIdString, "trash", selectedTabbar == "delete")
actionutils.SetTabbar(action, tabbar)
// 左侧操作子菜单
switch types.String(mainTab) {
case "board":
// TODO
case "log":
// TODO
case "stat":
// TODO
case "setting":
action.Data["leftMenuItems"] = this.createSettingsMenu(types.String(secondMenuItem), serverIdString, serverConfig)
case "delete":
// TODO
}
}
func (this *ServerHelper) createSettingsMenu(secondMenuItem string, serverIdString string, serverConfig *serverconfigs.ServerConfig) (items []maps.Map) {
menuItems := []maps.Map{
{
"name": "基本信息",
"url": "/servers/server/settings?serverId=" + serverIdString,
"isActive": secondMenuItem == "basic",
},
}
// HTTP
if serverConfig.IsHTTP() {
menuItems = append(menuItems, maps.Map{
"name": "HTTP",
"url": "/servers/server/http?serverId=" + serverIdString,
"isActive": secondMenuItem == "http",
})
menuItems = append(menuItems, maps.Map{
"name": "HTTPS",
"url": "/servers/server/https?serverId=" + serverIdString,
"isActive": secondMenuItem == "https",
})
menuItems = append(menuItems, maps.Map{
"name": "Web设置",
"url": "/servers/server/web?serverId=" + serverIdString,
"isActive": secondMenuItem == "web",
})
menuItems = append(menuItems, maps.Map{
"name": "字符集",
"url": "/servers/server/charset?serverId=" + serverIdString,
"isActive": secondMenuItem == "charset",
})
menuItems = append(menuItems, maps.Map{
"name": "访问日志",
"url": "/servers/server/accessLog?serverId=" + serverIdString,
"isActive": secondMenuItem == "accessLog",
})
menuItems = append(menuItems, maps.Map{
"name": "统计",
"url": "/servers/server/stat?serverId=" + serverIdString,
"isActive": secondMenuItem == "stat",
})
menuItems = append(menuItems, maps.Map{
"name": "Gzip压缩",
"url": "/servers/server/gzip?serverId=" + serverIdString,
"isActive": secondMenuItem == "gzip",
})
menuItems = append(menuItems, maps.Map{
"name": "特殊页面",
"url": "/servers/server/pages?serverId=" + serverIdString,
"isActive": secondMenuItem == "pages",
})
menuItems = append(menuItems, maps.Map{
"name": "HTTP Header",
"url": "/servers/server/headers?serverId=" + serverIdString,
"isActive": secondMenuItem == "header",
})
menuItems = append(menuItems, maps.Map{
"name": "反向代理",
"url": "/servers/server/reverseProxy?serverId=" + serverIdString,
"isActive": secondMenuItem == "reverseProxy",
})
menuItems = append(menuItems, maps.Map{
"name": "路径规则",
"url": "/servers/server/locations?serverId=" + serverIdString,
"isActive": secondMenuItem == "locations",
})
menuItems = append(menuItems, maps.Map{
"name": "访问控制",
"url": "/servers/server/access?serverId=" + serverIdString,
"isActive": secondMenuItem == "access",
})
menuItems = append(menuItems, maps.Map{
"name": "WAF",
"url": "/servers/server/waf?serverId=" + serverIdString,
"isActive": secondMenuItem == "waf",
})
menuItems = append(menuItems, maps.Map{
"name": "缓存",
"url": "/servers/server/cache?serverId=" + serverIdString,
"isActive": secondMenuItem == "cache",
})
} else if serverConfig.IsTCP() {
menuItems = append(menuItems, maps.Map{
"name": "TCP",
"url": "/servers/server/tcp?serverId=" + serverIdString,
"isActive": secondMenuItem == "tcp",
})
} else if serverConfig.IsUnix() {
menuItems = append(menuItems, maps.Map{
"name": "Unix",
"url": "/servers/server/unix?serverId=" + serverIdString,
"isActive": secondMenuItem == "unix",
})
} else if serverConfig.IsUDP() {
menuItems = append(menuItems, maps.Map{
"name": "UDP",
"url": "/servers/server/udp?serverId=" + serverIdString,
"isActive": secondMenuItem == "udp",
})
}
return menuItems
}

View File

@@ -0,0 +1,97 @@
package serverutils
import (
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
)
type ServerType = string
const (
ServerTypeHTTPProxy ServerType = "httpProxy"
ServerTypeHTTPWeb ServerType = "httpWeb"
ServerTypeTCPProxy ServerType = "tcpProxy"
ServerTypeUnixProxy ServerType = "unixProxy"
ServerTypeUDPProxy ServerType = "udp"
)
// 获取所有的服务类型
func AllServerTypes() []maps.Map {
return []maps.Map{
{
"name": "HTTP反向代理",
"code": ServerTypeHTTPProxy,
},
{
"name": "HTTP Web服务",
"code": ServerTypeHTTPWeb,
},
{
"name": "TCP反向代理",
"code": ServerTypeTCPProxy,
},
/**{
"name": "UNIX协议反向代理",
"code": ServerTypeUnixProxy,
},
{
"name": "UDP反向代理",
"code": ServerTypeUDPProxy,
},**/
}
}
// 查找服务类型
func FindServerType(code string) maps.Map {
for _, m := range AllServerTypes() {
if m.GetString("code") == code {
return m
}
}
return nil
}
// 获取所有协议
func AllServerProtocolsForType(serverType ServerType) []maps.Map {
protocols := []maps.Map{
{
"name": "HTTP",
"code": "http",
"serverTypes": []ServerType{ServerTypeHTTPProxy, ServerTypeHTTPWeb},
},
{
"name": "HTTPS",
"code": "https",
"serverTypes": []ServerType{ServerTypeHTTPProxy, ServerTypeHTTPWeb},
},
{
"name": "TCP",
"code": "tcp",
"serverTypes": []ServerType{ServerTypeTCPProxy},
},
{
"name": "TLS",
"code": "tls",
"serverTypes": []ServerType{ServerTypeTCPProxy},
},
{
"name": "Unix",
"code": "unix",
"serverTypes": []ServerType{ServerTypeUnixProxy},
},
{
"name": "UDP",
"code": "udp",
"serverTypes": []ServerType{ServerTypeUDPProxy},
},
}
result := []maps.Map{}
for _, p := range protocols {
serverTypes := p.GetSlice("serverTypes")
if lists.Contains(serverTypes, serverType) {
result = append(result, p)
}
}
return result
}

View File

@@ -0,0 +1,15 @@
package servers
import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
type UpdateAction struct {
actionutils.ParentAction
}
func (this *UpdateAction) Init() {
this.Nav("", "", "")
}
func (this *UpdateAction) RunGet(params struct{}) {
this.Show()
}