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

@ -1,5 +1,8 @@
package com.example.action; 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.mgr.MappingManager;
import com.example.module.admin.req.*; import com.example.module.admin.req.*;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
@ -13,13 +16,9 @@ import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.InputStreamReader; 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.BaseResponse;
import com.example.frame.base.ErrorType; import com.example.frame.base.ErrorType;
import com.example.frame.base.Token; import com.example.frame.base.Token;
import com.example.frame.utils.LogUtil;
import com.example.frame.base.Error; import com.example.frame.base.Error;
@Controller @Controller
@ -27,6 +26,8 @@ public class AjaxController {
@Autowired @Autowired
private MappingManager mappingManager; private MappingManager mappingManager;
@Autowired
private NginxCtrl nginxCtrl;
@RequestMapping("/ajax") @RequestMapping("/ajax")
@ResponseBody @ResponseBody
@ -56,6 +57,15 @@ public class AjaxController {
// 示例 // 示例
case "ajax.example.example": case "ajax.example.example":
break; 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": case "ajax.admin.mapping.create":
baseResponse = createMapping(jsonString, token); baseResponse = createMapping(jsonString, token);
@ -89,6 +99,27 @@ public class AjaxController {
return baseResponse; 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!"); 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 nginxCtrl = new NginxCtrl();
nginxCtrl.setStartCmd("start " + exe.getAbsolutePath()); nginxCtrl.setStartCmd(start.getAbsolutePath());
nginxCtrl.setStopCmd(exe.getAbsolutePath() + " -s quit"); nginxCtrl.setStopCmd(stop.getAbsolutePath());
nginxCtrl.setReloadCmd(exe.getAbsolutePath() + " - s reload "); nginxCtrl.setReloadCmd(reload.getAbsolutePath());
nginxCtrl.setVersionCmd(exe.getAbsolutePath() + " -v "); nginxCtrl.setVersionCmd(nginxHome.getAbsolutePath() + " -v ");
return nginxCtrl; return nginxCtrl;
} }
} }

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

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

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

Loading…
Cancel
Save

Powered by TurnKey Linux.