当迁移了管理平台后,自动跳转到确认API配置页

This commit is contained in:
GoEdgeLab
2021-11-21 15:57:13 +08:00
parent b155fcedcb
commit 7108a446f6
11 changed files with 406 additions and 0 deletions

View File

@@ -2,6 +2,8 @@ package setup
import (
"github.com/TeaOSLab/EdgeAdmin/internal/configs"
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
"os"
)
var isConfigured bool
@@ -16,3 +18,16 @@ func IsConfigured() bool {
isConfigured = err == nil
return isConfigured
}
// IsNewInstalled IsNew 检查是否新安装
func IsNewInstalled() bool {
homeDir, err := os.UserHomeDir()
if err != nil {
return false
}
_, err = os.Stat(homeDir + "/." + teaconst.ProcessName + "/api.yaml")
if err != nil {
return true
}
return false
}

View File

@@ -43,6 +43,12 @@ func (this *IndexAction) RunGet(params struct {
return
}
//// 是否新安装
if setup.IsNewInstalled() {
this.RedirectURL("/setup/confirm")
return
}
// 已登录跳转到dashboard
if params.Auth.IsUser() {
this.RedirectURL("/dashboard")

View File

@@ -0,0 +1,17 @@
package confirm
import (
"github.com/TeaOSLab/EdgeAdmin/internal/setup"
"github.com/iwind/TeaGo/actions"
)
type Helper struct {
}
func (this *Helper) BeforeAction(actionPtr actions.ActionWrapper) (goNext bool) {
if !setup.IsNewInstalled() {
actionPtr.Object().RedirectURL("/")
return false
}
return true
}

View File

@@ -0,0 +1,126 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package confirm
import (
"github.com/TeaOSLab/EdgeAdmin/internal/configs"
"github.com/TeaOSLab/EdgeAdmin/internal/rpc"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/actions"
"net/url"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "", "")
}
func (this *IndexAction) RunGet(params struct{}) {
var endpoints = []string{}
config, err := configs.LoadAPIConfig()
if err == nil {
endpoints = config.RPC.Endpoints
}
this.Data["nodeId"] = config.NodeId
this.Data["secret"] = config.Secret
if len(endpoints) == 0 {
endpoints = []string{""} // 初始化一个空的
}
this.Data["endpoints"] = endpoints
this.Show()
}
func (this *IndexAction) RunPost(params struct {
Endpoints []string
NodeId string
Secret string
Must *actions.Must
}) {
var endpoints = []string{}
for _, endpoint := range params.Endpoints {
if len(endpoint) > 0 {
u, err := url.Parse(endpoint)
if err != nil {
this.Fail("API节点地址'" + endpoint + "'格式错误")
}
endpoint = u.Scheme + "://" + u.Host
if u.Scheme != "http" && u.Scheme != "https" {
this.Fail("API节点地址'" + endpoint + "'中的协议错误目前只支持http或者https")
}
switch u.Scheme {
case "http":
if len(u.Port()) == 0 {
endpoint += ":80"
}
case "https":
if len(u.Port()) == 0 {
endpoint += ":443"
}
}
// 检测是否连接
var config = &configs.APIConfig{}
config.NodeId = params.NodeId
config.Secret = params.Secret
config.RPC.Endpoints = []string{endpoint}
client, err := rpc.NewRPCClient(config, false)
if err != nil {
this.Fail("无法连接到API节点地址'" + endpoint + "'" + err.Error())
}
_, err = client.APINodeRPC().FindCurrentAPINodeVersion(client.Context(0), &pb.FindCurrentAPINodeVersionRequest{})
if err != nil {
_ = client.Close()
this.Fail("无法连接到API节点地址'" + endpoint + "'" + err.Error())
}
_ = client.Close()
endpoints = append(endpoints, endpoint)
}
}
if len(endpoints) == 0 {
this.Fail("请输入至少一个API节点地址")
}
if len(params.NodeId) == 0 {
this.Fail("请输入NodeId")
}
if len(params.Secret) == 0 {
this.Fail("请输入Secret")
}
// 创建配置文件
config, err := configs.LoadAPIConfig()
if err != nil {
config = &configs.APIConfig{}
}
config.NodeId = params.NodeId
config.Secret = params.Secret
config.RPC.Endpoints = endpoints
err = config.WriteFile(Tea.ConfigFile("api.yaml"))
if err != nil {
this.Fail("配置保存失败:" + err.Error())
}
rpcClient, err := rpc.SharedRPC()
if err != nil {
this.Fail("RPC配置无法读取" + err.Error())
}
err = rpcClient.UpdateConfig(config)
if err != nil {
this.Fail("重载RPC配置失败" + err.Error())
}
this.Success()
}

View File

@@ -0,0 +1,13 @@
package confirm
import "github.com/iwind/TeaGo"
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(new(Helper)).
Prefix("/setup/confirm").
GetPost("", new(IndexAction)).
EndAll()
})
}

View File

@@ -127,6 +127,7 @@ import (
// 安装
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/setup"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/setup/confirm"
// 平台用户
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/users"