mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2025-11-04 05:00:25 +08:00
当迁移了管理平台后,自动跳转到确认API配置页
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
|
||||
17
internal/web/actions/default/setup/confirm/helper.go
Normal file
17
internal/web/actions/default/setup/confirm/helper.go
Normal 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
|
||||
}
|
||||
126
internal/web/actions/default/setup/confirm/index.go
Normal file
126
internal/web/actions/default/setup/confirm/index.go
Normal 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()
|
||||
}
|
||||
13
internal/web/actions/default/setup/confirm/init.go
Normal file
13
internal/web/actions/default/setup/confirm/init.go
Normal 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()
|
||||
})
|
||||
}
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user