1 Star 0 Fork 0

xwtec/deploy

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
devServer.go 3.42 KB
一键复制 编辑 原始数据 按行查看 历史
邹大侠 提交于 2024-05-30 21:03 . fix sth
package main
import (
"bytes"
"devServer/Util"
"encoding/json"
"fmt"
_ "fmt"
"io"
"net/http"
_ "net/http"
"os/exec"
"strings"
"time"
)
var devPath = "/mnt/c/Users/Administrator/IdeaProjects/data-manage/data-api/target/"
var prodPath = "/data/cmttg/testmanage/"
var path = prodPath
func readLog(w http.ResponseWriter, r *http.Request) {
lines, _ := Util.ReadLines(path + "/api.out")
result := ResultJson{Code: 1000}
result.Data = lines
marshal, _ := json.Marshal(result)
_, _ = io.WriteString(w, string(marshal))
}
func status(w http.ResponseWriter, r *http.Request) {
lines, _ := Util.ReadLines(path + "api.out")
result := getStatusByLines(lines)
marshal, _ := json.Marshal(result)
_, _ = io.WriteString(w, string(marshal))
}
func getStatusByLines(lines []string) ResultJson {
//获取当前日期字符串
dateStr := time.Now().Format("2006-01-02")
//判断lines是否包含当前日期字符串
isIncludeCurrentDate := false
isIncludeError := false
isIncludeSuccess := false
for _, line := range lines {
if (isIncludeCurrentDate && isIncludeSuccess) || (isIncludeCurrentDate && isIncludeError) {
break
}
if !isIncludeCurrentDate {
isIncludeCurrentDate = strings.Contains(line, dateStr)
}
if !isIncludeError {
isIncludeError = strings.Contains(line, "ERROR")
}
if !isIncludeSuccess {
isIncludeSuccess = strings.Contains(line, "项目启动成功")
}
}
hasPid := serviceStatus()
if hasPid && isIncludeCurrentDate {
if !isIncludeError && isIncludeSuccess {
result := ResultJson{Code: 1000, Data: 2}
return result
}
if isIncludeError {
result := ResultJson{Code: 1000, Data: 3}
return result
}
result := ResultJson{Code: 1000, Data: 1}
return result
}
if hasPid {
if lines != nil || len(lines) > 0 {
result := ResultJson{Code: 1000, Data: 2}
return result
}
result := ResultJson{Code: 1000, Data: 1}
return result
}
result := ResultJson{Code: 1000, Data: 0}
return result
}
func serviceStatus() bool {
actionCmd := exec.Command("bash", "-c", " cd "+path+" \n "+path+"run.sh status ")
var actionOut bytes.Buffer
actionCmd.Stdout = &actionOut
err := actionCmd.Run()
return err == nil
}
func executeShell(w http.ResponseWriter, r *http.Request) {
// 如果是预检请求,直接返回
if r.Method == http.MethodOptions {
return
}
//获取参数
result := ResultJson{Code: 1000}
q := r.URL.Query()
action := q.Get("action")
// 调用shell命令
pwd := exec.Command("bash", "-c", " pwd ")
var currentDir bytes.Buffer
pwd.Stdout = &currentDir
_ = pwd.Run()
fmt.Println(currentDir.String())
actionCmd := exec.Command("bash", "-c", " cd "+path+" \n "+path+"run.sh "+action+" ")
var actionOut bytes.Buffer
actionCmd.Stdout = &actionOut
err := actionCmd.Run()
fmt.Println(actionOut.String())
result.ActionOut = actionOut.String()
if action == "status" {
result.Code = 1000
if err != nil {
result.Data = 0
} else {
result.Data = 1
}
}
if action == "stop" {
result.Data = 4
if err == nil {
result.Data = 0
}
}
if action == "restart" {
result.Data = 1
if err != nil {
result.Data = 0
}
}
if action == "start" {
result.Data = 1
if err != nil {
result.Data = 0
}
}
if err != nil {
fmt.Printf("Error: %s\n", err)
result.Code = 500
result.Message = err.Error()
} else {
result.Message = action + "执行成功"
}
marshal, _ := json.Marshal(result)
_, _ = io.WriteString(w, string(marshal))
fmt.Println(string(marshal))
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/xwtec_1/deploy.git
[email protected]:xwtec_1/deploy.git
xwtec_1
deploy
deploy
master

搜索帮助