U Optimized code

This commit is contained in:
张臻 2020-08-19 10:22:43 +08:00
parent 9843bc4fd4
commit ea845df302
11 changed files with 44 additions and 99 deletions

View File

@ -283,19 +283,11 @@ func (crontab Crontab) Remove(w http.ResponseWriter, gp *core.Goploy) *core.Resp
} }
} }
err := model.Crontab{ if err := (model.Crontab{ID: reqData.ID}).DeleteRow(); err != nil {
ID: reqData.ID,
}.DeleteRow()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
err = model.CrontabServer{ if err := (model.CrontabServer{CrontabID: reqData.ID}).DeleteByCrontabID(); err != nil {
CrontabID: reqData.ID,
}.DeleteByCrontabID()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
@ -353,11 +345,7 @@ func (crontab Crontab) RemoveCrontabServer(w http.ResponseWriter, gp *core.Goplo
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
err = model.CrontabServer{ if err := (model.CrontabServer{ID: reqData.CrontabServerID}).DeleteRow(); err != nil {
ID: reqData.CrontabServerID,
}.DeleteRow()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
@ -394,9 +382,7 @@ func addCrontab(serverID int64, command string) {
} }
func deleteCrontab(serverID int64, command string) { func deleteCrontab(serverID int64, command string) {
server, err := model.Server{ server, err := model.Server{ID: serverID}.GetData()
ID: serverID,
}.GetData()
if err != nil { if err != nil {
core.Log(core.TRACE, "serverID:"+strconv.FormatUint(uint64(serverID), 10)+" get server fail, detail:"+err.Error()) core.Log(core.TRACE, "serverID:"+strconv.FormatUint(uint64(serverID), 10)+" get server fail, detail:"+err.Error())

View File

@ -2,7 +2,6 @@ package controller
import ( import (
"database/sql" "database/sql"
"encoding/json"
"github.com/zhenorzz/goploy/core" "github.com/zhenorzz/goploy/core"
"github.com/zhenorzz/goploy/model" "github.com/zhenorzz/goploy/model"
"github.com/zhenorzz/goploy/service" "github.com/zhenorzz/goploy/service"
@ -37,7 +36,6 @@ func (deploy Deploy) GetList(w http.ResponseWriter, gp *core.Goploy) *core.Respo
// GetPreview deploy detail // GetPreview deploy detail
func (deploy Deploy) GetPreview(w http.ResponseWriter, gp *core.Goploy) *core.Response { func (deploy Deploy) GetPreview(w http.ResponseWriter, gp *core.Goploy) *core.Response {
type RespData struct { type RespData struct {
GitTraceList model.PublishTraces `json:"gitTraceList"` GitTraceList model.PublishTraces `json:"gitTraceList"`
Pagination model.Pagination `json:"pagination"` Pagination model.Pagination `json:"pagination"`
@ -74,7 +72,6 @@ func (deploy Deploy) GetPreview(w http.ResponseWriter, gp *core.Goploy) *core.Re
// GetDetail deploy detail // GetDetail deploy detail
func (deploy Deploy) GetDetail(w http.ResponseWriter, gp *core.Goploy) *core.Response { func (deploy Deploy) GetDetail(w http.ResponseWriter, gp *core.Goploy) *core.Response {
type RespData struct { type RespData struct {
PublishTraceList model.PublishTraces `json:"publishTraceList"` PublishTraceList model.PublishTraces `json:"publishTraceList"`
} }
@ -131,11 +128,11 @@ func (deploy Deploy) GetCommitList(w http.ResponseWriter, gp *core.Goploy) *core
// Publish the project // Publish the project
func (deploy Deploy) Publish(w http.ResponseWriter, gp *core.Goploy) *core.Response { func (deploy Deploy) Publish(w http.ResponseWriter, gp *core.Goploy) *core.Response {
type ReqData struct { type ReqData struct {
ProjectID int64 `json:"projectId"` ProjectID int64 `json:"projectId" validate:"gt=0"`
Commit string `json:"commit"` Commit string `json:"commit"`
} }
var reqData ReqData var reqData ReqData
if err := json.Unmarshal(gp.Body, &reqData); err != nil { if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
@ -178,11 +175,10 @@ func (deploy Deploy) Webhook(w http.ResponseWriter, gp *core.Goploy) *core.Respo
projectName := gp.URLQuery.Get("project_name") projectName := gp.URLQuery.Get("project_name")
// other event is blocked in deployMiddleware // other event is blocked in deployMiddleware
type ReqData struct { type ReqData struct {
Ref string `json:"ref"` Ref string `json:"ref" validate:"required"`
} }
var reqData ReqData var reqData ReqData
if err := json.Unmarshal(gp.Body, &reqData); err != nil { if err := verify(gp.Body, &reqData); err != nil {
core.Log(core.ERROR, "json unmarshal error, err:"+err.Error())
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
branch := strings.Split(reqData.Ref, "/")[2] branch := strings.Split(reqData.Ref, "/")[2]

View File

@ -86,9 +86,7 @@ func (namespace Namespace) Add(_ http.ResponseWriter, gp *core.Goploy) *core.Res
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
err = model.NamespaceUser{NamespaceID: id}.AddAdminByNamespaceID() if err := (model.NamespaceUser{NamespaceID: id}).AddAdminByNamespaceID(); err != nil {
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
@ -161,11 +159,8 @@ func (namespace Namespace) RemoveUser(_ http.ResponseWriter, gp *core.Goploy) *c
if err := verify(gp.Body, &reqData); err != nil { if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
err := model.NamespaceUser{
ID: reqData.NamespaceUserID,
}.DeleteRow()
if err != nil { if err := (model.NamespaceUser{ID: reqData.NamespaceUserID}).DeleteRow(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
return &core.Response{} return &core.Response{}

View File

@ -3,7 +3,6 @@ package controller
import ( import (
"bytes" "bytes"
"database/sql" "database/sql"
"encoding/json"
"github.com/zhenorzz/goploy/core" "github.com/zhenorzz/goploy/core"
"github.com/zhenorzz/goploy/model" "github.com/zhenorzz/goploy/model"
"github.com/zhenorzz/goploy/utils" "github.com/zhenorzz/goploy/utils"
@ -113,12 +112,12 @@ func (project Project) GetBindUserList(w http.ResponseWriter, gp *core.Goploy) *
// Add project // Add project
func (project Project) Add(w http.ResponseWriter, gp *core.Goploy) *core.Response { func (project Project) Add(w http.ResponseWriter, gp *core.Goploy) *core.Response {
type ReqData struct { type ReqData struct {
Name string `json:"name"` Name string `json:"name" validate:"required"`
URL string `json:"url"` URL string `json:"url" validate:"required"`
Path string `json:"path"` Path string `json:"path" validate:"required"`
Environment string `json:"Environment" validate:"required"`
Branch string `json:"branch" validate:"required"`
SymlinkPath string `json:"symlinkPath"` SymlinkPath string `json:"symlinkPath"`
Environment string `json:"Environment"`
Branch string `json:"branch"`
AfterPullScriptMode string `json:"afterPullScriptMode"` AfterPullScriptMode string `json:"afterPullScriptMode"`
AfterPullScript string `json:"afterPullScript"` AfterPullScript string `json:"afterPullScript"`
AfterDeployScriptMode string `json:"afterDeployScriptMode"` AfterDeployScriptMode string `json:"afterDeployScriptMode"`
@ -131,8 +130,7 @@ func (project Project) Add(w http.ResponseWriter, gp *core.Goploy) *core.Respons
NotifyTarget string `json:"notifyTarget"` NotifyTarget string `json:"notifyTarget"`
} }
var reqData ReqData var reqData ReqData
err := json.Unmarshal(gp.Body, &reqData) if err := verify(gp.Body, &reqData); err != nil {
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
@ -140,7 +138,7 @@ func (project Project) Add(w http.ResponseWriter, gp *core.Goploy) *core.Respons
return &core.Response{Code: core.Error, Message: "Invalid rsync option format"} return &core.Response{Code: core.Error, Message: "Invalid rsync option format"}
} }
_, err = model.Project{Name: reqData.Name}.GetDataByName() _, err := model.Project{Name: reqData.Name}.GetDataByName()
if err != sql.ErrNoRows { if err != sql.ErrNoRows {
return &core.Response{Code: core.Error, Message: "The project name is already exist"} return &core.Response{Code: core.Error, Message: "The project name is already exist"}
} }
@ -210,7 +208,7 @@ func (project Project) Add(w http.ResponseWriter, gp *core.Goploy) *core.Respons
// Edit project // Edit project
func (project Project) Edit(w http.ResponseWriter, gp *core.Goploy) *core.Response { func (project Project) Edit(w http.ResponseWriter, gp *core.Goploy) *core.Response {
type ReqData struct { type ReqData struct {
ID int64 `json:"id"` ID int64 `json:"id" validate:"gt=0"`
Name string `json:"name"` Name string `json:"name"`
URL string `json:"url"` URL string `json:"url"`
Path string `json:"path"` Path string `json:"path"`
@ -227,8 +225,7 @@ func (project Project) Edit(w http.ResponseWriter, gp *core.Goploy) *core.Respon
NotifyTarget string `json:"notifyTarget"` NotifyTarget string `json:"notifyTarget"`
} }
var reqData ReqData var reqData ReqData
err := json.Unmarshal(gp.Body, &reqData) if err := verify(gp.Body, &reqData); err != nil {
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
@ -319,11 +316,10 @@ func (project Project) Edit(w http.ResponseWriter, gp *core.Goploy) *core.Respon
// RemoveRow Project // RemoveRow Project
func (project Project) Remove(w http.ResponseWriter, gp *core.Goploy) *core.Response { func (project Project) Remove(w http.ResponseWriter, gp *core.Goploy) *core.Response {
type ReqData struct { type ReqData struct {
ID int64 `json:"id"` ID int64 `json:"id" validate:"gt=0"`
} }
var reqData ReqData var reqData ReqData
err := json.Unmarshal(gp.Body, &reqData) if err := verify(gp.Body, &reqData); err != nil {
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
@ -332,11 +328,7 @@ func (project Project) Remove(w http.ResponseWriter, gp *core.Goploy) *core.Resp
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
err = model.Project{ if err := (model.Project{ID: reqData.ID}).RemoveRow(); err != nil {
ID: reqData.ID,
}.RemoveRow()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
@ -355,8 +347,7 @@ func (project Project) AddServer(w http.ResponseWriter, gp *core.Goploy) *core.R
ServerIDs []int64 `json:"serverIds" validate:"required"` ServerIDs []int64 `json:"serverIds" validate:"required"`
} }
var reqData ReqData var reqData ReqData
err := json.Unmarshal(gp.Body, &reqData) if err := verify(gp.Body, &reqData); err != nil {
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
projectID := reqData.ProjectID projectID := reqData.ProjectID
@ -410,15 +401,11 @@ func (project Project) RemoveServer(w http.ResponseWriter, gp *core.Goploy) *cor
ProjectServerID int64 `json:"projectServerId" validate:"gt=0"` ProjectServerID int64 `json:"projectServerId" validate:"gt=0"`
} }
var reqData ReqData var reqData ReqData
err := json.Unmarshal(gp.Body, &reqData) if err := verify(gp.Body, &reqData); err != nil {
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
err = model.ProjectServer{
ID: reqData.ProjectServerID,
}.DeleteRow()
if err != nil { if err := (model.ProjectServer{ID: reqData.ProjectServerID}).DeleteRow(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
return &core.Response{} return &core.Response{}
@ -430,16 +417,12 @@ func (project Project) RemoveUser(w http.ResponseWriter, gp *core.Goploy) *core.
ProjectUserID int64 `json:"projectUserId" validate:"gt=0"` ProjectUserID int64 `json:"projectUserId" validate:"gt=0"`
} }
var reqData ReqData var reqData ReqData
err := json.Unmarshal(gp.Body, &reqData) if err := verify(gp.Body, &reqData); err != nil {
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
err = model.ProjectUser{
ID: reqData.ProjectUserID,
}.DeleteRow()
if err != nil { if err := (model.ProjectUser{ID: reqData.ProjectUserID}).DeleteRow(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
return &core.Response{} return &core.Response{}
@ -535,8 +518,7 @@ func (project Project) RemoveTask(w http.ResponseWriter, gp *core.Goploy) *core.
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
err := model.ProjectTask{ID: reqData.ID}.RemoveRow() if err := (model.ProjectTask{ID: reqData.ID}).RemoveRow(); err != nil {
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
@ -547,13 +529,13 @@ func (project Project) RemoveTask(w http.ResponseWriter, gp *core.Goploy) *core.
func repoCreate(projectID int64) { func repoCreate(projectID int64) {
project, err := model.Project{ID: projectID}.GetData() project, err := model.Project{ID: projectID}.GetData()
if err != nil { if err != nil {
core.Log(core.TRACE, "projectID:"+strconv.FormatInt(projectID, 10)+" 无此项目") core.Log(core.TRACE, "The project does not exist, projectID:"+strconv.FormatInt(projectID, 10))
return return
} }
srcPath := core.RepositoryPath + project.Name srcPath := core.RepositoryPath + project.Name
if _, err := os.Stat(srcPath); err != nil { if _, err := os.Stat(srcPath); err != nil {
if err := os.RemoveAll(srcPath); err != nil { if err := os.RemoveAll(srcPath); err != nil {
core.Log(core.TRACE, "projectID:"+strconv.FormatInt(project.ID, 10)+" 项目移除失败") core.Log(core.TRACE, "The project fail to remove, projectID:"+strconv.FormatInt(project.ID, 10))
return return
} }
repo := project.URL repo := project.URL
@ -562,7 +544,7 @@ func repoCreate(projectID int64) {
cmd.Stdout = &out cmd.Stdout = &out
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
core.Log(core.ERROR, "projectID:"+strconv.FormatInt(project.ID, 10)+" 项目初始化失败:"+err.Error()) core.Log(core.ERROR, "The project fail to initialize, projectID:"+strconv.FormatInt(project.ID, 10)+err.Error())
return return
} }
@ -579,7 +561,7 @@ func repoCreate(projectID int64) {
} }
} }
core.Log(core.TRACE, "projectID:"+strconv.FormatInt(project.ID, 10)+" 项目初始化成功") core.Log(core.TRACE, "The project success to initialize, projectID:"+strconv.FormatInt(project.ID, 10))
} }
return return

View File

@ -106,8 +106,7 @@ func (server Server) Check(w http.ResponseWriter, gp *core.Goploy) *core.Respons
if err := verify(gp.Body, &reqData); err != nil { if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
_, err := utils.ConnectSSH(reqData.Owner, "", reqData.IP, reqData.Port) if _, err := utils.ConnectSSH(reqData.Owner, "", reqData.IP, reqData.Port); err != nil {
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
return &core.Response{Message: "Connected"} return &core.Response{Message: "Connected"}

View File

@ -1,7 +1,6 @@
package controller package controller
import ( import (
"encoding/json"
"net/http" "net/http"
"github.com/zhenorzz/goploy/core" "github.com/zhenorzz/goploy/core"
@ -33,7 +32,6 @@ func (Template) GetTotal(_ http.ResponseWriter, gp *core.Goploy) *core.Response
Total int64 `json:"total"` Total int64 `json:"total"`
} }
total, err := model.Template{}.GetTotal() total, err := model.Template{}.GetTotal()
if err != nil { if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
@ -117,15 +115,11 @@ func (Template) Remove(w http.ResponseWriter, gp *core.Goploy) *core.Response {
ID int64 `json:"id" validate:"gt=0"` ID int64 `json:"id" validate:"gt=0"`
} }
var reqData ReqData var reqData ReqData
err := json.Unmarshal(gp.Body, &reqData) if err := verify(gp.Body, &reqData); err != nil {
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
err = model.Template{
ID: reqData.ID,
}.DeleteRow()
if err != nil { if err := (model.Template{ID: reqData.ID}).DeleteRow(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
return &core.Response{Message: "删除成功"} return &core.Response{Message: "删除成功"}

View File

@ -2,7 +2,6 @@ package controller
import ( import (
"database/sql" "database/sql"
"encoding/json"
"github.com/patrickmn/go-cache" "github.com/patrickmn/go-cache"
"net/http" "net/http"
"strconv" "strconv"
@ -26,8 +25,7 @@ func (user User) Login(w http.ResponseWriter, gp *core.Goploy) *core.Response {
NamespaceList model.Namespaces `json:"namespaceList"` NamespaceList model.Namespaces `json:"namespaceList"`
} }
var reqData ReqData var reqData ReqData
err := json.Unmarshal(gp.Body, &reqData) if err := verify(gp.Body, &reqData); err != nil {
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
userData, err := model.User{Account: reqData.Account}.GetDataByAccount() userData, err := model.User{Account: reqData.Account}.GetDataByAccount()
@ -233,12 +231,7 @@ func (user User) Remove(_ http.ResponseWriter, gp *core.Goploy) *core.Response {
if reqData.ID == 1 { if reqData.ID == 1 {
return &core.Response{Code: core.Error, Message: "Can not delete the super manager"} return &core.Response{Code: core.Error, Message: "Can not delete the super manager"}
} }
if err := (model.User{ID: reqData.ID}).RemoveRow(); err != nil {
err := model.User{
ID: reqData.ID,
}.RemoveRow()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
return &core.Response{} return &core.Response{}
@ -263,7 +256,7 @@ func (user User) ChangePassword(_ http.ResponseWriter, gp *core.Goploy) *core.Re
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
if err := (model.User{ID: gp.UserInfo.ID, Password: reqData.NewPassword}.UpdatePassword()); err != nil { if err := (model.User{ID: gp.UserInfo.ID, Password: reqData.NewPassword}).UpdatePassword(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()} return &core.Response{Code: core.Error, Message: err.Error()}
} }
return &core.Response{} return &core.Response{}

View File

@ -19,7 +19,7 @@ func HasPublishAuth(_ http.ResponseWriter, gp *core.Goploy) error {
return err return err
} }
_, err := model.Project{ID: reqData.ProjectID}.GetUserProjectData(gp.UserInfo.ID) _, err := model.Project{ID: reqData.ProjectID, UserID: gp.UserInfo.ID}.GetUserProjectData()
if err != nil { if err != nil {
return errors.New("no permission") return errors.New("no permission")
} }

View File

@ -406,7 +406,7 @@ func (p Project) GetDataByName() (Project, error) {
} }
// GetUserProjectData - // GetUserProjectData -
func (p Project) GetUserProjectData(userID int64) (Project, error) { func (p Project) GetUserProjectData() (Project, error) {
var project Project var project Project
err := sq. err := sq.
Select("project_id, project.name, publisher_id, publisher_name, project.environment, project.branch, project.last_publish_token, project.deploy_state"). Select("project_id, project.name, publisher_id, publisher_name, project.environment, project.branch, project.last_publish_token, project.deploy_state").
@ -414,7 +414,7 @@ func (p Project) GetUserProjectData(userID int64) (Project, error) {
LeftJoin(projectTable+" ON project_user.project_id = project.id"). LeftJoin(projectTable+" ON project_user.project_id = project.id").
Where(sq.Eq{ Where(sq.Eq{
"project_user.project_id": p.ID, "project_user.project_id": p.ID,
"project_user.user_id": userID, "project_user.user_id": p.UserID,
"project.state": Enable, "project.state": Enable,
}). }).
RunWith(DB). RunWith(DB).

View File

@ -160,7 +160,7 @@ func ConnectSSH(user, password, host string, port int) (*ssh.Session, error) {
}, },
} }
// connet to ssh // connect to ssh
addr = fmt.Sprintf("%s:%d", host, port) addr = fmt.Sprintf("%s:%d", host, port)
if client, err = ssh.Dial("tcp", addr, clientConfig); err != nil { if client, err = ssh.Dial("tcp", addr, clientConfig); err != nil {
@ -187,4 +187,4 @@ func ConnectSSH(user, password, host string, port int) (*ssh.Session, error) {
func ClearNewline(str string) string { func ClearNewline(str string) string {
return strings.TrimRight(strings.Replace(str, "\r\n", "\n", -1), "\n") return strings.TrimRight(strings.Replace(str, "\r\n", "\n", -1), "\n")
} }

View File

@ -27,6 +27,6 @@ const (
) )
func (projectMessage ProjectMessage) canSendTo(client *Client) error { func (projectMessage ProjectMessage) canSendTo(client *Client) error {
_, err := model.Project{ID: projectMessage.ProjectID}.GetUserProjectData(client.UserInfo.ID) _, err := model.Project{ID: projectMessage.ProjectID, UserID: client.UserInfo.ID}.GetUserProjectData()
return err return err
} }