0.0.1-SNAPSHOT
wangbing 5 years ago
parent 8e5a42930e
commit 5e3ba5e368

@ -1,5 +1,8 @@
package com.example.action;
import com.example.config.NginxConfig;
import com.example.frame.utils.*;
import com.example.module.admin.ent.NginxCtrl;
import com.example.module.admin.mgr.MappingManager;
import com.example.module.admin.req.*;
import org.springframework.stereotype.Controller;
@ -13,13 +16,9 @@ import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import com.example.frame.utils.LocalData;
import com.example.frame.utils.MapperUtil;
import com.example.frame.utils.Message;
import com.example.frame.base.BaseResponse;
import com.example.frame.base.ErrorType;
import com.example.frame.base.Token;
import com.example.frame.utils.LogUtil;
import com.example.frame.base.Error;
@Controller
@ -27,6 +26,8 @@ public class AjaxController {
@Autowired
private MappingManager mappingManager;
@Autowired
private NginxCtrl nginxCtrl;
@RequestMapping("/ajax")
@ResponseBody
@ -56,6 +57,15 @@ public class AjaxController {
// 示例
case "ajax.example.example":
break;
case "nginx.start":
baseResponse = nginxStart(jsonString, token);
break;
case "nginx.stop":
baseResponse = nginxStop(jsonString, token);
break;
case "nginx.reload":
baseResponse = nginxReload(jsonString, token);
break;
// 创建映射
case "ajax.admin.mapping.create":
baseResponse = createMapping(jsonString, token);
@ -89,6 +99,27 @@ public class AjaxController {
return baseResponse;
}
private BaseResponse nginxStart(String jsonString, Token token) {
BaseResponse baseResponse = new BaseResponse();
String exec = ProcessUtil.execBat(nginxCtrl.getStartCmd());
System.out.println(exec);
return baseResponse;
}
private BaseResponse nginxStop(String jsonString, Token token) {
BaseResponse baseResponse = new BaseResponse();
String exec = ProcessUtil.execBat(nginxCtrl.getStopCmd());
System.out.println(exec);
return baseResponse;
}
private BaseResponse nginxReload(String jsonString, Token token) {
BaseResponse baseResponse = new BaseResponse();
String exec = ProcessUtil.execBat(nginxCtrl.getReloadCmd());
System.out.println(exec);
return baseResponse;
}
/**
*
*/

@ -0,0 +1,18 @@
package com.example.action.screen;
import com.example.frame.base.Screen;
import com.example.frame.utils.ProcessUtil;
import org.springframework.ui.Model;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
public class Mapping extends Screen {
@Override
public void exec(Model model, HttpServletRequest request, HttpServletResponse response) {
boolean nginx = ProcessUtil.findProcess("nginx");
model.addAttribute("run", nginx ? "true" : "false");
}
}

@ -60,13 +60,15 @@ public class NginxConfig {
throw new RuntimeException("nginx home not exists!");
}
File exe = new File(nginxHome, "nginx.exe");
File start = new File(nginxHome, "start.bat");
File stop = new File(nginxHome, "stop.bat");
File reload = new File(nginxHome, "reload.bat");
NginxCtrl nginxCtrl = new NginxCtrl();
nginxCtrl.setStartCmd("start " + exe.getAbsolutePath());
nginxCtrl.setStopCmd(exe.getAbsolutePath() + " -s quit");
nginxCtrl.setReloadCmd(exe.getAbsolutePath() + " - s reload ");
nginxCtrl.setVersionCmd(exe.getAbsolutePath() + " -v ");
nginxCtrl.setStartCmd(start.getAbsolutePath());
nginxCtrl.setStopCmd(stop.getAbsolutePath());
nginxCtrl.setReloadCmd(reload.getAbsolutePath());
nginxCtrl.setVersionCmd(nginxHome.getAbsolutePath() + " -v ");
return nginxCtrl;
}
}

@ -33,7 +33,7 @@ public class TaskConfig implements SchedulingConfigurer {
Message message = new Message();
message.setType(MessageType.NGINX_STATE);
State state = new State();
state.setRun(true);
state.setRun(ProcessUtil.findProcess("nginx"));
message.setObject(state);
globalController.pushAll(message);
}

@ -16,34 +16,31 @@ import java.io.InputStreamReader;
public class ProcessUtil {
/**
* windowsexe
* @param path exe
*
* @param exe exe
*/
public static void execExe(String path) {
Runtime rn = Runtime.getRuntime();
Process p = null;
try {
p = rn.exec(path);
} catch (Exception e) {
System.out.println("Error exec!");
public static String execExe(String exe) {
if (!exe.endsWith(".bat")) {
throw new RuntimeException(exe + "is not a file of .exe");
}
return exec(exe);
}
/**
* windows
* @param path
*
* @param bat
*/
public static void execBat(String path) {
Runtime rn = Runtime.getRuntime();
Process p = null;
try {
p = rn.exec(path);
} catch (Exception e) {
System.out.println("Error exec!");
public static String execBat(String bat) {
if (!bat.endsWith(".bat")) {
throw new RuntimeException(bat + "is not a file of .bat");
}
return exec("call "+bat);
}
/**
* windows cmd
*
* @param command cmd
*/
public static String execCmd(String command) {
@ -53,6 +50,7 @@ public class ProcessUtil {
/**
*
*
* @param command cmd
*/
public static String exec(String command) {
@ -60,6 +58,7 @@ public class ProcessUtil {
Runtime runtime = Runtime.getRuntime();
Process process = null;
try {
System.out.println("==>>" + command);
process = runtime.exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
String line = null;

Before

Width:  |  Height:  |  Size: 3.9 KiB

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

@ -103,6 +103,24 @@ window.ajax = {
data: data
})
},
nginxStart: function (data) {
return jsonRequest({
method: "nginx.start",
data: data
})
},
nginxStop: function (data) {
return jsonRequest({
method: "nginx.stop",
data: data
})
},
nginxReload: function (data) {
return jsonRequest({
method: "nginx.reload",
data: data
})
},
fileUpload: function (file) {
var fd = new FormData();
fd.append("file", file);

@ -16,21 +16,23 @@
<el-card class="box-card control">
<a> Nginx 控制中心</a>
<a>
<a @click="nginxStart">
<el-image v-if="run" :src="'${Uri.getUrl('/static/img/start_.png')}'" style="width: 50px; height: 50px"
fit="fill"></el-image>
<el-image v-if="!run" :src="'${Uri.getUrl('/static/img/start.png')}'" style="width: 50px; height: 50px"
fit="fill"></el-image>
</a>
<a>
<a @click="nginxStop">
<el-image v-if="run" :src="'${Uri.getUrl('/static/img/stop.png')}'" style="width: 50px; height: 50px"
fit="fill"></el-image>
<el-image v-if="!run" :src="'${Uri.getUrl('/static/img/stop_.png')}'" style="width: 50px; height: 50px"
fit="fill"></el-image>
</a>
<a>
<el-image :src="'${Uri.getUrl('/static/img/restart.png')}'" style="width: 50px; height: 50px"
<a @click="nginxReload">
<el-image v-if="run" :src="'${Uri.getUrl('/static/img/reload.png')}'" style="width: 50px; height: 50px"
fit="fill"></el-image>
<el-image v-if="!run" :src="'${Uri.getUrl('/static/img/reload_.png')}'" style="width: 50px; height: 50px"
fit="fill"></el-image>
</a>
</el-card>
@ -180,7 +182,7 @@
},
select: [],
result: [],
run: false
run: ${run?default('false')}
},
methods: {
onSearch: function () {
@ -269,7 +271,6 @@
}
},
startMonitor: function () {
console.log(this)
if (window.EventSource) {
window.evtSource = new EventSource('${Uri.getUrl("/sse/1")}');
window.evtSource.addEventListener('message', function (e) {
@ -287,6 +288,36 @@
}
}.bind(this))
}
},
nginxStart: function () {
ajax.nginxStart().then(function (response) {
if (response.errors.length > 0) {
nav.e(response.errors[0].message);
} else {
this.onFind();
this.form.dialog = false;
}
}.bind(this))
},
nginxStop: function () {
ajax.nginxStop().then(function (response) {
if (response.errors.length > 0) {
nav.e(response.errors[0].message);
} else {
this.onFind();
this.form.dialog = false;
}
}.bind(this))
},
nginxReload: function () {
ajax.nginxReload().then(function (response) {
if (response.errors.length > 0) {
nav.e(response.errors[0].message);
} else {
this.onFind();
this.form.dialog = false;
}
}.bind(this))
}
},
created: function () {

Loading…
Cancel
Save

Powered by TurnKey Linux.