From 36577d265b5535b3ca38fccc2c4e5902219a125f Mon Sep 17 00:00:00 2001 From: "xin.cao" Date: Sat, 30 Mar 2019 21:25:43 +0800 Subject: [PATCH] ok --- .../ui/controller/SwitcherController.java | 48 ++++++++++++++++++- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/jswitcher-ui/src/main/java/com/github/xincao9/jswitcher/ui/controller/SwitcherController.java b/jswitcher-ui/src/main/java/com/github/xincao9/jswitcher/ui/controller/SwitcherController.java index 5a6af3f..ac82eb2 100644 --- a/jswitcher-ui/src/main/java/com/github/xincao9/jswitcher/ui/controller/SwitcherController.java +++ b/jswitcher-ui/src/main/java/com/github/xincao9/jswitcher/ui/controller/SwitcherController.java @@ -26,6 +26,8 @@ import com.github.xincao9.jswitcher.api.service.SwitcherService; import com.github.xincao9.jswitcher.api.vo.Switcher; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; @@ -66,10 +68,24 @@ public class SwitcherController { */ @GetMapping("keys") public ResponseEntity>> keys() { + try { + return ResponseEntity.ok(getKeys()); + } catch (Throwable e) { + LOGGER.error(e.getMessage()); + } + return ResponseEntity.status(500).build(); + } + + /** + * 获取开关列表 + * + * @return 开关列表 + */ + private List> getKeys () { try { List endpoints = discoveryService.query(SwitcherService.class.getTypeName()); if (endpoints == null || endpoints.isEmpty()) { - return ResponseEntity.status(400).build(); + return Collections.EMPTY_LIST; } List> keys = new ArrayList(); AtomicInteger no = new AtomicInteger(0); @@ -86,7 +102,35 @@ public ResponseEntity>> keys() { keys.add(v0); }); } - return ResponseEntity.ok(keys); + return keys; + } catch (Throwable e) { + LOGGER.error(e.getMessage()); + } + return Collections.EMPTY_LIST; + } + + /** + * 开关列表 + * + * @return 开关 + */ + @GetMapping("tree") + public ResponseEntity>>>> tree() { + try { + List> keys = getKeys(); + Map>>> tree = new HashMap(); + for (Map key : keys) { + String application = String.valueOf(key.get("application")); + if (!tree.containsKey(application)) { + tree.put(application, new HashMap()); + } + String name = String.valueOf(key.get("key")); + if (!tree.get(application).containsKey(name)) { + tree.get(application).put(name, new ArrayList()); + } + tree.get(application).get(name).add(key); + } + return ResponseEntity.ok(tree); } catch (Throwable e) { LOGGER.error(e.getMessage()); }