From 04c7a106e80800a62db8515a7452f2fc1b73693b Mon Sep 17 00:00:00 2001
From: sunty <1172534965@qq.com>
Date: 星期五, 10 四月 2020 17:01:04 +0800
Subject: [PATCH] add swag code respformat a lot code about server
---
code/code.go | 24 ++++++
config/config.go | 1
go.mod | 5 +
main.go | 6 +
controllers/swfsControllers.go | 120 +++++++++++++++++------------
util/util.go | 16 ++++
router/router.go | 10 ++
7 files changed, 127 insertions(+), 55 deletions(-)
diff --git a/code/code.go b/code/code.go
new file mode 100644
index 0000000..f806399
--- /dev/null
+++ b/code/code.go
@@ -0,0 +1,24 @@
+package code
+
+import "net/http"
+
+// Code 閿欒杈撳嚭鏁版嵁缁撴瀯
+type Code struct {
+ Status int `json:"status"` // HTTP 鐘舵��
+ Success bool `json:"success"` // 鎴愬姛鎴栬�呭け璐�
+ Message string `json:"msg"` // 鎻忚堪淇℃伅
+}
+
+var (
+ LicenseExpired = &Code{http.StatusForbidden, false, "license expired"}
+ // Success 璇锋眰澶勭悊鎴愬姛
+ Success = &Code{http.StatusOK, true, "璇锋眰澶勭悊鎴愬姛"}
+ AddSuccess = &Code{http.StatusOK, true, "璁″叆鎴愬姛"}
+ UpdateSuccess = &Code{http.StatusOK, true, "鏇存柊鎴愬姛"}
+ UpdateFail = &Code{http.StatusBadRequest, false, "鏇存柊澶辫触"}
+ DelSuccess = &Code{http.StatusOK, true, "鍒犻櫎鎴愬姛"}
+ // RequestParamError 璇锋眰鍙傛暟閿欒
+ RequestParamError = &Code{http.StatusBadRequest, false, "璇锋眰鍙傛暟鏈夎"}
+ CreateFirstNodeErr = &Code{http.StatusInternalServerError, false, "鍒涘缓鑺傜偣澶辫触锛�"}
+ AddClusterInfoErr = &Code{http.StatusInternalServerError, false, "鍔犲叆鑺傜偣澶辫触锛�"}
+)
diff --git a/config/config.go b/config/config.go
index 54b570d..f49f244 100644
--- a/config/config.go
+++ b/config/config.go
@@ -10,6 +10,7 @@
EsServerPort string `mapstructure: "esServerPort"`
CoreBaseUnit string `mapstructure: "coreBaseUnit"`
TimeOut string `mapstructure: "timeOut"`
+ ScriptPath string `mapstructure: scriptPath`
}
type elastic struct {
diff --git a/controllers/swfsControllers.go b/controllers/swfsControllers.go
index c4c8c38..1465b6a 100644
--- a/controllers/swfsControllers.go
+++ b/controllers/swfsControllers.go
@@ -6,10 +6,18 @@
"net/http"
"strconv"
"strings"
+ "swfs/code"
"swfs/config"
"swfs/util"
"sync"
"time"
+)
+
+const (
+ StartServerScript = "seaweedfs_start.sh"
+ StopServerScript = "seaweedfs_stop.sh"
+ StartScriptAsVolume = "option=1"
+ StartScriptAsMaster = "option=2"
)
type SeaweedfsController struct{}
@@ -18,13 +26,23 @@
Role string `json:"role"`
}
-//淇敼
func (sc *SeaweedfsController) UpdateSWFSServiceController(c *gin.Context) {
- oldPeers := GetOldPeers()
+ oldPeers := GetOldPeers(config.Server.ScriptPath, StartServerScript)
newPeers := GetNewPeers()
- UpdatePeers(oldPeers, newPeers)
+ UpdatePeers(config.Server.ScriptPath, StartServerScript, oldPeers, newPeers)
+ ReplaceLineContentBySearch(StartScriptAsVolume, config.Server.ScriptPath, StartServerScript)
}
+// @Security ApiKeyAuth
+// @Summary 鏂拌妭鐐瑰姞鍏�
+// @Description 鏂拌妭鐐瑰姞鍏�
+// @Accept json
+// @Produce json
+// @Tags swfs
+// @Param obj body SWFSInfo true "鍔犲叆瑙掕壊鍙傛暟"
+// @Success 200 {string} json "{"code":200, msg:"", success:true}"
+// @Failure 500 {string} json "{"code":500, msg:"", success:false}"
+// @Router /node/api-v/swfs/addSWFSNode [POST]
func (sc *SeaweedfsController) AddSWFSNodeController(c *gin.Context) {
var body SWFSInfo
c.BindJSON(&body)
@@ -32,27 +50,47 @@
nowPeers := GetNowPeersList()
if role == "master" {
AsMaster(nowPeers)
+ util.ResponseFormat(c, code.AddSuccess, "鍔犲叆鑺傜偣鎴愬姛")
+ return
} else if role == "volume" {
AsVolume(nowPeers)
+ util.ResponseFormat(c, code.AddSuccess, "鍔犲叆鑺傜偣鎴愬姛")
+ return
} else {
+ util.ResponseFormat(c, code.RequestParamError, "閫夋嫨鑺傜偣绫诲瀷閿欒")
return
}
}
-func ReplaceLineContentBySearch(replaceContent string) {
- resContent := GetNowLineContent("/opt/vasystem/script/seaweedfs_start.sh", "#start_master_server")
- replaceStr := "sed -ie 's/" + resContent + "/" + replaceContent + "/g' /opt/vasystem/seaweedfs_start.sh"
- util.RunScript(replaceStr)
+func (sc *SeaweedfsController) RoleOfVolumeToMasterController(c *gin.Context) {
+ AsMaster(GetNowPeersList())
}
func (sc *SeaweedfsController) RestartMasterController(c *gin.Context) {
- end := "sh /opt/vasystem/script/seaweedfs_stop.sh"
- start := "sh /opt/vasystem/script/seaweedfs_start.sh"
- util.RunScript(end)
- util.RunScript(start)
+ StopServer(config.Server.ScriptPath)
+ time.Sleep(time.Second * 1)
+ StartServer(config.Server.ScriptPath)
}
+//鍚姩鏈嶅姟
+func StartServer(scriptPath string) {
+ util.RunScript("sh " + scriptPath + StartServerScript)
+}
+
+//鍋滄鏈嶅姟
+func StopServer(scriptPath string) {
+ util.RunScript("sh " + scriptPath + StopServerScript)
+}
+
+//鏍规嵁鎼滅储鍐呭鏇挎崲鏁磋鍐呭
+func ReplaceLineContentBySearch(replaceContent string, scriptPath string, scriptFile string) {
+ resContent := GetNowLineContent(scriptPath+"/"+scriptFile, "option=")
+ replaceStr := "sed -ie 's/" + resContent + "/" + replaceContent + "/g' " + scriptPath + "/" + scriptFile
+ util.RunScript(replaceStr)
+}
+
+//鏇存柊鎵�鏈夎妭鐐圭殑鑴氭湰鍙傛暟
func UpdateAllNodesScriptArgument(nowPeers []interface{}) {
for _, val := range nowPeers {
ip := val.(string)
@@ -61,15 +99,17 @@
}
}
+//璇锋眰浣滀负涓昏妭鐐规搷浣滄祦绋�
func RequestMasterNodesOperation(nowPeers []interface{}) {
//fmt.Println("config.Server.CoreBaseUnit", config.Server.CoreBaseUnit)
coreBaseUnit, _ := strconv.Atoi(config.Server.CoreBaseUnit)
//fmt.Println("nowPeers: ", nowPeers)
//fmt.Println("coreBaseUnit: ", coreBaseUnit)
UpdateAllNodesScriptArgument(nowPeers)
- RestartAllMaster(nowPeers, coreBaseUnit)
+ RestartAllServer(nowPeers, coreBaseUnit)
}
+//閲嶅惎鍏朵粬鑺傜偣鏈嶅姟骞堕獙璇�
func RestartServer(ip string, timeOut int) {
url := "http://" + ip + ":7020/node/api-v/swfs/restartMaster"
http.Get(url)
@@ -88,7 +128,8 @@
}
}
-func RestartAllMaster(nowPeers []interface{}, coreBaseUnit int) {
+//鏋勫缓閲嶅惎娴佺▼
+func RestartAllServer(nowPeers []interface{}, coreBaseUnit int) {
coreThread := len(nowPeers)/coreBaseUnit + 1
masterIp := make([]string, 0)
timeOut, _ := strconv.Atoi(config.Server.TimeOut)
@@ -120,34 +161,30 @@
}
}
-func GetOldPeers() string {
- str := "cat /opt/vasystem/seaweedfs_start.sh | grep peers="
- peers := strings.Split(util.RunScript(str), "\n")[0]
- return peers
+//鑾峰彇鏈湴浠ヤ娇鐢ㄩ泦缇よ〃鍗�
+func GetOldPeers(scriptPath string, scriptFile string) string {
+ return GetNowLineContent(scriptPath+"/"+scriptFile, "peers=")
}
+//鑾峰彇鏌ユ壘鍐呭褰撳墠琛屽唴瀹�
func GetNowLineContent(filePath string, searchContent string) string {
scriptStr := "cat" + filePath + "| grep " + searchContent
- content := strings.Split(util.RunScript(scriptStr), "\n")[0]
- return content
+ return strings.Split(util.RunScript(scriptStr), "\n")[0]
}
+//浣滀负鏁版嵁鑺傜偣鍔犲叆
func AsVolume(nowPeers []interface{}) {
- ReplaceLineContentBySearch("start_master_server")
- fmt.Println(nowPeers)
+ ReplaceLineContentBySearch(StartScriptAsVolume, config.Server.ScriptPath, StartServerScript)
}
+//浣滀负涓昏妭鐐瑰姞鍏ワ紙榛樿鍖呭惈鏁版嵁鑺傜偣锛�
func AsMaster(nowPeers []interface{}) {
AddNewMasterToPeers()
RequestMasterNodesOperation(nowPeers)
}
-func (sc *SeaweedfsController) RoleOfVolumeToMasterController(c *gin.Context) {
- AsMaster(GetNowPeersList())
-}
-
+//鑾峰彇褰撳墠闆嗙兢鍒楄〃
func GetNowPeersList() []interface{} {
-
getUrl := "http://" + config.Server.EsServerIp + ":" + config.Server.EsServerPort + "/" + config.BasicFS.IndexName + "/_search"
getJson := `{
"query": {
@@ -166,43 +203,24 @@
buf, _ := util.EsReq("POST", getUrl, []byte(getJson))
source, _ := util.Sourcelist(buf)
- //fmt.Println(source)
peers := source[0]["peers"].([]interface{})
return peers
}
+//鑾峰彇褰撳墠闆嗙兢鍒楄〃鏍煎紡鍖栦俊鎭�
func GetNewPeers() string {
-
- getUrl := "http://" + config.Server.EsServerIp + ":" + config.Server.EsServerPort + "/" + config.BasicFS.IndexName + "/_search"
- getJson := `{
- "query": {
- "bool": {
- "filter": [
- {
- "term": {
- "application":"nodeOperation"
- }
- }
- ]
- }
- },
- "size": 1
-}`
-
- buf, _ := util.EsReq("POST", getUrl, []byte(getJson))
- source, _ := util.Sourcelist(buf)
- //fmt.Println(source)
- peers := source[0]["peers"].([]interface{})
- fmt.Println(peers)
+ peers := GetNowPeersList()
p := "peers=" + strings.Replace(strings.Trim(fmt.Sprint(peers), "[]"), " ", ",", -1)
return p
}
-func UpdatePeers(oldPeers string, newPeers string) {
- str := "sed -ie 's/" + oldPeers + "/" + newPeers + "/g' /opt/vasystem/seaweedfs_start.sh"
+//鏇存柊鏈湴闆嗙兢鍒楄〃
+func UpdatePeers(scriptPath string, scriptFile string, oldPeers string, newPeers string) {
+ str := "sed -ie 's/" + oldPeers + "/" + newPeers + "/g' " + scriptPath + "/" + scriptFile
util.RunScript(str)
}
+//鍚戦泦缇ゅ姞鍏ユ柊鐨刴aster
func AddNewMasterToPeers() (result bool) {
peer := config.Server.EsServerIp + ":6333"
addUrl := "http://" + config.Server.EsServerIp + ":" + config.Server.EsServerPort + "/" + config.BasicFS.IndexName + "/_update_by_query"
diff --git a/go.mod b/go.mod
index e899b4a..136b825 100644
--- a/go.mod
+++ b/go.mod
@@ -1,8 +1,11 @@
-module test
+module swfs
go 1.12
require (
+ github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc
github.com/gin-gonic/gin v1.6.1
github.com/spf13/viper v1.6.2
+ github.com/swaggo/gin-swagger v1.2.0
+ github.com/swaggo/swag v1.5.1
)
diff --git a/main.go b/main.go
index 1ca5668..921592d 100644
--- a/main.go
+++ b/main.go
@@ -2,7 +2,8 @@
import (
"flag"
- "test/config"
+ "swfs/config"
+ "swfs/router"
)
var env = flag.String("pro", "pro", "read storage info")
@@ -12,5 +13,6 @@
}
func main() {
-
+ r := router.NewRouter()
+ r.Run("0.0.0.0:7020")
}
diff --git a/router/router.go b/router/router.go
index 3075620..587cae0 100644
--- a/router/router.go
+++ b/router/router.go
@@ -2,20 +2,28 @@
import (
"github.com/gin-gonic/gin"
+ "github.com/swaggo/gin-swagger"
+ "github.com/swaggo/gin-swagger/swaggerFiles"
"swfs/controllers"
+ _ "swfs/docs"
)
func NewRouter() *gin.Engine {
r := gin.Default()
+ r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
swfsController := new(controllers.SeaweedfsController)
urlPrefix := "/node/api-v"
swfsApi := r.Group(urlPrefix + "/swfs")
{
- swfsApi.GET("/addNode", swfsController.AddSWFSNodeController)
+ swfsApi.POST("/addSWFSNode", swfsController.AddSWFSNodeController)
swfsApi.GET("/updateSWFSService", swfsController.UpdateSWFSServiceController)
swfsApi.GET("/restartMaster", swfsController.RestartMasterController)
swfsApi.GET("roleOfVolumeToMaster", swfsController.RoleOfVolumeToMasterController)
}
+ // 鏂囦欢 涓婁紶
+ r.Static("static", "./static") // 闈欐�佹枃浠�
+ //澶栭儴璁块棶swagger.json
+ r.StaticFile("/swagger.json", "./docs/swagger.json")
return r
}
diff --git a/util/util.go b/util/util.go
index bf2337a..480afb1 100644
--- a/util/util.go
+++ b/util/util.go
@@ -5,9 +5,11 @@
"encoding/json"
"errors"
"fmt"
+ "github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
"os/exec"
+ "swfs/code"
"time"
)
@@ -98,3 +100,17 @@
total = int(middle)
return total, nil
}
+
+// ResponseFormat 杩斿洖鏁版嵁鏍煎紡鍖�
+func ResponseFormat(c *gin.Context, respStatus *code.Code, data interface{}) {
+ if respStatus == nil {
+ respStatus = code.RequestParamError
+ }
+ c.JSON(respStatus.Status, gin.H{
+ "code": respStatus.Status,
+ "success": respStatus.Success,
+ "msg": respStatus.Message,
+ "data": data,
+ })
+ return
+}
--
Gitblit v1.8.0