Эх сурвалжийг харах

关闭开启 服务新增 不是当前容器的服务的时候调用其他底座的接口启动和停止服务

andy 1 жил өмнө
parent
commit
d7d10f551d

+ 73 - 0
src/main/java/com/scbfkj/uni/api/ControlApi.java

@@ -1,9 +1,13 @@
 package com.scbfkj.uni.api;
 
 import com.scbfkj.uni.library.DataAliasGetUtil;
+import com.scbfkj.uni.library.RequestUtil;
 import com.scbfkj.uni.library.UniReturnUtil;
 import com.scbfkj.uni.process.DataBase;
+import com.scbfkj.uni.process.Web;
 import com.scbfkj.uni.service.ControlService;
+import com.scbfkj.uni.system.Config;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -17,8 +21,44 @@ import java.util.Optional;
 @RestController
 @RequestMapping("controlApi")
 public class ControlApi {
+    private static final DataBase DATA_BASE = new DataBase();
+    private final static String SERVICE_ID = "serviceid";
+
+    @Value("${app.inner.ssl.enable:false}")
+    private boolean httpSslEnable = false;
+
     @PostMapping("start")
     public ResponseEntity<Map<String, Object>> start(@RequestBody Map<String, Object> body) throws Exception {
+        Optional<String> serviceIdOpt = DataAliasGetUtil.getValue(SERVICE_ID, body);
+        Optional<String> containerCode = DataAliasGetUtil.getValue("containercode", body);
+        if ((serviceIdOpt.isEmpty() || containerCode.isEmpty())) {
+            return ResponseEntity.ok(UniReturnUtil.fail("服务ID不能为空"));
+        }
+
+        String code = containerCode.get();
+        if (!Config.getContainerCode().equals(code)) {
+            List<Map<String, Object>> containers = DATA_BASE.query(Config.getCenterConnectionStr(), "select * from container where containercode = ? limit 1 offset 0", code);
+            if (containers.isEmpty()) {
+                return ResponseEntity.ok(UniReturnUtil.fail("容器编号错误"));
+            } else {
+                Map<String, Object> containerMap = containers.get(0);
+                Object requesturi = containerMap.get("requesturi");
+                if (requesturi == null) {
+                    return ResponseEntity.ok(UniReturnUtil.fail("不知道容器的访问地址"));
+                }
+                String url = requesturi.toString();
+                if (!url.startsWith("http")) {
+                    if (httpSslEnable) {
+                        url = "https://" + url + "/controlApi/start";
+                    } else {
+                        url = "http://" + url + "/controlApi/start";
+                    }
+                }
+                Map<String, Object> stringObjectMap = new Web().execWebApi(RequestUtil.getHeaders(), "post", body, url);
+                return ResponseEntity.ok(stringObjectMap);
+            }
+        }
+
         Map<String, Object> started = ControlService.startOrStop(body, "1", false);
         return ResponseEntity.ok(started);
     }
@@ -31,7 +71,40 @@ public class ControlApi {
      */
     @PostMapping("stop")
     public ResponseEntity<Map<String, Object>> stop(@RequestBody Map<String, Object> body) throws Exception {
+        Optional<String> serviceIdOpt = DataAliasGetUtil.getValue(SERVICE_ID, body);
+        Optional<String> containerCode = DataAliasGetUtil.getValue("containercode", body);
+        if ((serviceIdOpt.isEmpty() || containerCode.isEmpty())) {
+            return ResponseEntity.ok(UniReturnUtil.fail("服务ID不能为空"));
+        }
+
+        String code = containerCode.get();
+        if (!Config.getContainerCode().equals(code)) {
+            List<Map<String, Object>> containers = DATA_BASE.query(Config.getCenterConnectionStr(), "select * from container where containercode = ? limit 1 offset 0", code);
+            if (containers.isEmpty()) {
+                return ResponseEntity.ok(UniReturnUtil.fail("容器编号错误"));
+            } else {
+                Map<String, Object> containerMap = containers.get(0);
+                Object requesturi = containerMap.get("requesturi");
+                if (requesturi == null) {
+                    return ResponseEntity.ok(UniReturnUtil.fail("不知道容器的访问地址"));
+                }
+                String url = requesturi.toString();
+                if (!url.startsWith("http")) {
+                    if (httpSslEnable) {
+                        url = "https://" + url + "/controlApi/stop";
+                    } else {
+                        url = "http://" + url + "/controlApi/stop";
+                    }
+                }
+                Map<String, Object> stringObjectMap = new Web().execWebApi(RequestUtil.getHeaders(), "post", body, url);
+                return ResponseEntity.ok(stringObjectMap);
+            }
+        }
+
+
+
         Map<String, Object> stoped = ControlService.startOrStop(body, "0", false);
+
         return ResponseEntity.ok(stoped);
     }