diff --git a/admin/src/main/java/com/example/action/AjaxController.java b/admin/src/main/java/com/example/action/AjaxController.java index d7c2096..bfee39a 100644 --- a/admin/src/main/java/com/example/action/AjaxController.java +++ b/admin/src/main/java/com/example/action/AjaxController.java @@ -101,22 +101,31 @@ public class AjaxController { private BaseResponse nginxStart(String jsonString, Token token) { BaseResponse baseResponse = new BaseResponse(); - String exec = ProcessUtil.execBat(nginxCtrl.getStartCmd()); - System.out.println(exec); + if (nginxCtrl.isRunning()) { + baseResponse.addError(ErrorType.BUSINESS_ERROR, "程序已经运行"); + return baseResponse; + } + ProcessUtil.execBat(nginxCtrl.getStartCmd()); return baseResponse; } private BaseResponse nginxStop(String jsonString, Token token) { BaseResponse baseResponse = new BaseResponse(); - String exec = ProcessUtil.execBat(nginxCtrl.getStopCmd()); - System.out.println(exec); + if (!nginxCtrl.isRunning()) { + baseResponse.addError(ErrorType.BUSINESS_ERROR, "程序尚未运行"); + return baseResponse; + } + ProcessUtil.execBat(nginxCtrl.getStopCmd()); return baseResponse; } private BaseResponse nginxReload(String jsonString, Token token) { BaseResponse baseResponse = new BaseResponse(); - String exec = ProcessUtil.execBat(nginxCtrl.getReloadCmd()); - System.out.println(exec); + if (!nginxCtrl.isRunning()) { + baseResponse.addError(ErrorType.BUSINESS_ERROR, "程序尚未运行"); + return baseResponse; + } + ProcessUtil.execBat(nginxCtrl.getReloadCmd()); return baseResponse; } diff --git a/admin/src/main/java/com/example/action/screen/Mapping.java b/admin/src/main/java/com/example/action/screen/Mapping.java index 746e033..6761881 100644 --- a/admin/src/main/java/com/example/action/screen/Mapping.java +++ b/admin/src/main/java/com/example/action/screen/Mapping.java @@ -2,6 +2,8 @@ package com.example.action.screen; import com.example.frame.base.Screen; import com.example.frame.utils.ProcessUtil; +import com.example.module.admin.ent.NginxCtrl; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ui.Model; import javax.servlet.http.HttpServletRequest; @@ -9,10 +11,11 @@ import javax.servlet.http.HttpServletResponse; import java.util.ArrayList; public class Mapping extends Screen { + @Autowired + private NginxCtrl nginxCtrl; @Override public void exec(Model model, HttpServletRequest request, HttpServletResponse response) { - boolean nginx = ProcessUtil.findProcess("nginx"); - model.addAttribute("run", nginx ? "true" : "false"); + model.addAttribute("run", nginxCtrl.isRunning() ? "true" : "false"); } } diff --git a/admin/src/main/java/com/example/config/NginxConfig.java b/admin/src/main/java/com/example/config/NginxConfig.java index 91431f4..dddc76d 100644 --- a/admin/src/main/java/com/example/config/NginxConfig.java +++ b/admin/src/main/java/com/example/config/NginxConfig.java @@ -27,6 +27,8 @@ public class NginxConfig { @Autowired private Environment environment; + @Autowired + private ObjectClient objectClient; @Bean public NginxCtrl initNginx() { @@ -69,6 +71,7 @@ public class NginxConfig { nginxCtrl.setStopCmd(stop.getAbsolutePath()); nginxCtrl.setReloadCmd(reload.getAbsolutePath()); nginxCtrl.setVersionCmd(nginxHome.getAbsolutePath() + " -v "); + return nginxCtrl; } } diff --git a/admin/src/main/java/com/example/config/SqliteConfig.java b/admin/src/main/java/com/example/config/SqliteConfig.java index 80d7f75..7d77705 100644 --- a/admin/src/main/java/com/example/config/SqliteConfig.java +++ b/admin/src/main/java/com/example/config/SqliteConfig.java @@ -3,6 +3,7 @@ package com.example.config; import com.example.frame.utils.FileUtil; import com.example.frame.utils.ZipUtil; import com.example.module.admin.ent.Mapping; +import com.example.module.admin.ent.Port; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.system.ApplicationHome; @@ -20,6 +21,7 @@ import java.io.IOException; import java.io.InputStream; import java.sql.SQLException; import java.util.ArrayList; +import java.util.List; @Configuration @@ -41,7 +43,15 @@ public class SqliteConfig { ArrayList objects = new ArrayList<>(); objects.add(Mapping.class); - return new ObjectClient(new File(dbpath, "data.db"), objects); + objects.add(Port.class); + ObjectClient objectClient = new ObjectClient(new File(dbpath, "data.db"), objects); + + List select = objectClient.select(Port.class, 1, 1); + if (select.size() == 0 && objectClient.insert(Port.class, new Port("8888")) != 1) { + throw new RuntimeException("Nginx default port 8888 init failed."); + } + + return objectClient; } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { diff --git a/admin/src/main/java/com/example/config/TaskConfig.java b/admin/src/main/java/com/example/config/TaskConfig.java index 7d600f8..c9f10df 100644 --- a/admin/src/main/java/com/example/config/TaskConfig.java +++ b/admin/src/main/java/com/example/config/TaskConfig.java @@ -5,6 +5,7 @@ import com.example.frame.base.Message; import com.example.frame.base.MessageType; import com.example.frame.utils.LogUtil; import com.example.frame.utils.ProcessUtil; +import com.example.module.admin.ent.NginxCtrl; import com.example.module.admin.ent.State; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; @@ -27,13 +28,15 @@ public class TaskConfig implements SchedulingConfigurer { @Autowired private GlobalController globalController; + @Autowired + private NginxCtrl nginxCtrl; @Scheduled(cron = "0/3 * * * * ? ") public void task() { Message message = new Message(); message.setType(MessageType.NGINX_STATE); State state = new State(); - state.setRun(ProcessUtil.findProcess("nginx")); + state.setRun(nginxCtrl.isRunning()); message.setObject(state); globalController.pushAll(message); } diff --git a/admin/src/main/java/com/example/frame/utils/ProcessUtil.java b/admin/src/main/java/com/example/frame/utils/ProcessUtil.java index ac0333d..a6248c3 100644 --- a/admin/src/main/java/com/example/frame/utils/ProcessUtil.java +++ b/admin/src/main/java/com/example/frame/utils/ProcessUtil.java @@ -19,11 +19,12 @@ public class ProcessUtil { * * @param exe 可执行exe文件路径 */ - public static String execExe(String exe) { - if (!exe.endsWith(".bat")) { - throw new RuntimeException(exe + "is not a file of .exe"); + public static void execExe(String exe) { + try { + Runtime.getRuntime().exec(exe); + } catch (IOException e) { + e.printStackTrace(); } - return exec(exe); } /** @@ -31,11 +32,15 @@ public class ProcessUtil { * * @param bat 可执行批处理文件路径 */ - public static String execBat(String bat) { + public static void execBat(String bat) { if (!bat.endsWith(".bat")) { throw new RuntimeException(bat + "is not a file of .bat"); } - return exec("call "+bat); + try { + Runtime.getRuntime().exec(bat); + } catch (IOException e) { + e.printStackTrace(); + } } /** diff --git a/admin/src/main/java/com/example/module/admin/ent/NginxCtrl.java b/admin/src/main/java/com/example/module/admin/ent/NginxCtrl.java index 1db8d84..e78fee2 100644 --- a/admin/src/main/java/com/example/module/admin/ent/NginxCtrl.java +++ b/admin/src/main/java/com/example/module/admin/ent/NginxCtrl.java @@ -53,4 +53,8 @@ public class NginxCtrl { public void setVersionCmd(String versionCmd) { this.versionCmd = versionCmd; } + + public boolean isRunning(){ + return ProcessUtil.findProcess("nginx.exe"); + } } diff --git a/admin/src/main/java/com/example/module/admin/ent/Port.java b/admin/src/main/java/com/example/module/admin/ent/Port.java new file mode 100644 index 0000000..311954a --- /dev/null +++ b/admin/src/main/java/com/example/module/admin/ent/Port.java @@ -0,0 +1,33 @@ +package com.example.module.admin.ent; + +import xyz.wbsite.wsqlite.anonation.TableField; + +import java.util.Date; + +/** + * MAPPING - 映射 + * + * @author author + * @version 0.0.1 + * @since 2019-09-28 + */ +public class Port { + + /** + * VALUE - 端口值 + */ + @TableField + private String value; + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public Port(String value) { + this.value = value; + } +} \ No newline at end of file diff --git a/admin/src/main/resources/nginx.conf b/admin/src/main/resources/nginx.conf new file mode 100644 index 0000000..8c7b6c8 --- /dev/null +++ b/admin/src/main/resources/nginx.conf @@ -0,0 +1,60 @@ + +#user nobody; +worker_processes 1; + +#error_log logs/error.log; +#error_log logs/error.log notice; +#error_log logs/error.log info; + +#pid logs/nginx.pid; + + +events { + worker_connections 1024; +} + + +http { + include mime.types; + default_type application/octet-stream; + sendfile on; + keepalive_timeout 65; + + server { + listen 8888; + server_name localhost; + + #charset koi8-r; + + #access_log logs/host.access.log main; + + location / { + root html; + index index.html index.htm; + } + + location /tzzdxxsb/ { + proxy_pass http://127.0.0.1:8081/; + proxy_redirect off; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + client_max_body_size 50m; + client_body_buffer_size 128k; + proxy_connect_timeout 300; + proxy_send_timeout 300; + proxy_read_timeout 300; + proxy_buffer_size 4k; + proxy_buffers 4 32k; + proxy_busy_buffers_size 64k; + proxy_temp_file_write_size 64k; + index index.html index.htm index.jsp index.do; + + } + + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root html; + } + } +} diff --git a/admin/src/main/resources/templates/screen/mapping.ftl b/admin/src/main/resources/templates/screen/mapping.ftl index 6f0ef4f..6d323f5 100644 --- a/admin/src/main/resources/templates/screen/mapping.ftl +++ b/admin/src/main/resources/templates/screen/mapping.ftl @@ -15,7 +15,7 @@ - Nginx 控制中心 + Nginx 控制中心 @@ -37,12 +37,23 @@ - + + + + 应用 + + + + + 新增 导出 + + + - - - - - - #app { padding: 10px; + width: 800px; } .box-card { margin: 10px; } + .control a:first-child { + padding-right: 10px; + } + .control a { display: inline-block; line-height: 50px; @@ -180,6 +190,9 @@ {min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur'} ], }, + services: [ + {port: "8080"} + ], select: [], result: [], run: ${run?default('false')}