阶段性提交

This commit is contained in:
GoEdgeLab
2020-07-22 22:19:39 +08:00
parent cc971be504
commit b984b68089
143 changed files with 22667 additions and 37 deletions

View File

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

View File

@@ -0,0 +1,15 @@
package dashboard
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.Prefix("/dashboard").
Helper(new(helpers.UserMustAuth)).
GetPost("", new(IndexAction)).
EndAll()
})
}

View File

@@ -1,8 +1,17 @@
package index
import (
"fmt"
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
"github.com/TeaOSLab/EdgeAdmin/internal/rpc"
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/admin"
"github.com/TeaOSLab/EdgeAdmin/internal/utils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/types"
stringutil "github.com/iwind/TeaGo/utils/string"
"time"
)
type IndexAction actions.Action
@@ -13,6 +22,107 @@ var TokenSalt = stringutil.Rand(32)
func (this *IndexAction) RunGet(params struct {
From string
Auth *helpers.UserShouldAuth
}) {
this.WriteString("Hello, i am index")
// 已登录跳转到dashboard
if params.Auth.IsUser() {
this.RedirectURL("/dashboard")
return
}
this.Data["isUser"] = false
this.Data["menu"] = "signIn"
timestamp := fmt.Sprintf("%d", time.Now().Unix())
this.Data["token"] = stringutil.Md5(TokenSalt+timestamp) + timestamp
this.Data["from"] = params.From
this.Show()
}
// 提交
func (this *IndexAction) RunPost(params struct {
Token string
Username string
Password string
Remember bool
Must *actions.Must
Auth *helpers.UserShouldAuth
}) {
params.Must.
Field("username", params.Username).
Require("请输入用户名").
Field("password", params.Password).
Require("请输入密码")
if params.Password == stringutil.Md5("") {
this.FailField("password", "请输入密码")
}
// 检查token
if len(params.Token) <= 32 {
this.Fail("请通过登录页面登录")
}
timestampString := params.Token[32:]
if stringutil.Md5(TokenSalt+timestampString) != params.Token[:32] {
this.FailField("refresh", "登录页面已过期,请刷新后重试")
}
timestamp := types.Int64(timestampString)
if timestamp < time.Now().Unix()-1800 {
this.FailField("refresh", "登录页面已过期,请刷新后重试")
}
rpcClient, err := rpc.SharedRPC()
if err != nil {
this.Fail("服务器出了点小问题:" + err.Error())
}
resp, err := rpcClient.AdminRPC().Login(rpcClient.Context(0), &admin.LoginRequest{
Username: params.Username,
Password: params.Password,
})
if err != nil {
_, err = rpcClient.AdminRPC().CreateLog(rpcClient.Context(0), &admin.CreateLogRequest{
Level: oplogs.LevelError,
Description: "登录时发生系统错误:" + err.Error(),
Action: this.Request.URL.Path,
Ip: this.RequestRemoteIP(),
})
if err != nil {
utils.PrintError(err)
}
actionutils.Fail(this, err)
}
if !resp.IsOk {
_, err = rpcClient.AdminRPC().CreateLog(rpcClient.Context(0), &admin.CreateLogRequest{
Level: oplogs.LevelWarn,
Description: "登录失败,用户名:" + params.Username,
Action: this.Request.URL.Path,
Ip: this.RequestRemoteIP(),
})
if err != nil {
utils.PrintError(err)
}
this.Fail("请输入正确的用户名密码")
}
adminId := int(resp.AdminId)
params.Auth.StoreAdmin(adminId, params.Remember)
// 记录日志
_, err = rpcClient.AdminRPC().CreateLog(rpcClient.Context(0), &admin.CreateLogRequest{
Level: oplogs.LevelInfo,
Description: "成功登录系统,用户名:" + params.Username,
Action: this.Request.URL.Path,
Ip: this.RequestRemoteIP(),
})
if err != nil {
utils.PrintError(err)
}
this.Success()
}

View File

@@ -0,0 +1,16 @@
package logout
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo/actions"
)
type IndexAction actions.Action
// 退出登录
func (this *IndexAction) Run(params struct {
Auth *helpers.UserShouldAuth
}) {
params.Auth.Logout()
this.RedirectURL("/")
}

View File

@@ -0,0 +1,12 @@
package logout
import "github.com/iwind/TeaGo"
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Prefix("/logout").
Get("", new(IndexAction)).
EndAll()
})
}

View File

@@ -0,0 +1,51 @@
package nodes
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/actions"
)
type CreateAction struct {
actionutils.ParentAction
}
func (this *CreateAction) Init() {
this.Nav("", "", "create")
}
func (this *CreateAction) RunGet(params struct{}) {
// 所有集群
/**clusters, err := models.SharedNodeClusterDAO.FindAllEnableClusters()
if err != nil {
this.ErrorPage(err)
return
}
clusterMaps := []maps.Map{}
for _, cluster := range clusters {
clusterMaps = append(clusterMaps, maps.Map{
"id": cluster.Id,
"name": cluster.Name,
})
}
this.Data["clusters"] = clusterMaps**/
this.Show()
}
func (this *CreateAction) RunPost(params struct {
Name string
ClusterId int
Must *actions.Must
}) {
params.Must.
Field("name", params.Name).
Require("请输入节点名称")
// TODO 检查cluster
if params.ClusterId <= 0 {
this.Fail("请选择所在集群")
}
// TODO 检查SSH授权
}

View File

@@ -0,0 +1,10 @@
package nodes
import "github.com/iwind/TeaGo/actions"
type Helper struct {
}
func (this *Helper) BeforeAction(action *actions.ActionObject) {
action.Data["teaMenu"] = "nodes"
}

View File

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

View File

@@ -0,0 +1,18 @@
package nodes
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)).
Helper(new(Helper)).
Prefix("/nodes").
Get("", new(IndexAction)).
GetPost("/create", new(CreateAction)).
EndAll()
})
}

View File

@@ -0,0 +1,14 @@
package servers
import "github.com/iwind/TeaGo/actions"
type Helper struct {
}
func NewHelper() *Helper {
return &Helper{}
}
func (this *Helper) BeforeAction(action *actions.ActionObject) {
action.Data["teaMenu"] = "servers"
}

View File

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

View File

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

View File

@@ -0,0 +1,14 @@
package settings
import "github.com/iwind/TeaGo/actions"
type Helper struct {
}
func NewHelper() *Helper {
return &Helper{}
}
func (this *Helper) BeforeAction(action *actions.ActionObject) {
action.Data["teaMenu"] = "settings"
}

View File

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

View File

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

View File

@@ -0,0 +1,42 @@
package ui
import (
"bytes"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/files"
"github.com/iwind/TeaGo/logs"
)
type ComponentsAction actions.Action
func (this *ComponentsAction) RunGet(params struct{}) {
this.AddHeader("Content-Type", "text/javascript; charset=utf-8")
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)
buf := bytes.NewBuffer([]byte{})
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
}
buf.Write(data)
buf.WriteByte('\n')
buf.WriteByte('\n')
})
this.Write(buf.Bytes())
}

View File

@@ -0,0 +1,16 @@
package ui
import (
"github.com/iwind/TeaGo"
"github.com/iwind/TeaGo/actions"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(new(actions.Gzip)).
Prefix("/ui").
Get("/components.js", new(ComponentsAction)).
EndAll()
})
}