WebUIController.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package org.ssssssss.magicapi.config;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  6. import org.springframework.jdbc.core.JdbcTemplate;
  7. import org.springframework.web.bind.annotation.RequestBody;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.ResponseBody;
  10. import org.ssssssss.magicapi.model.JsonBean;
  11. import org.ssssssss.script.MagicScriptDebugContext;
  12. import org.ssssssss.script.MagicScriptEngine;
  13. import org.ssssssss.script.MagicScriptError;
  14. import org.ssssssss.script.parsing.Span;
  15. import java.util.Arrays;
  16. import java.util.List;
  17. import java.util.Map;
  18. import java.util.UUID;
  19. public class WebUIController {
  20. private static Logger logger = LoggerFactory.getLogger(WebUIController.class);
  21. private JdbcTemplate template;
  22. private int debugTimeout;
  23. public WebUIController(JdbcTemplate template,int debugTimeout) {
  24. this.template = template;
  25. this.debugTimeout = debugTimeout;
  26. }
  27. public void printBanner(){
  28. System.out.println(" __ __ _ _ ____ ___ ");
  29. System.out.println(" | \\/ | __ _ __ _ (_) ___ / \\ | _ \\|_ _|");
  30. System.out.println(" | |\\/| | / _` | / _` || | / __| / _ \\ | |_) || | ");
  31. System.out.println(" | | | || (_| || (_| || || (__ / ___ \\ | __/ | | ");
  32. System.out.println(" |_| |_| \\__,_| \\__, ||_| \\___|/_/ \\_\\|_| |___|");
  33. System.out.println(" |___/ " + WebUIController.class.getPackage().getImplementationVersion());
  34. }
  35. @RequestMapping("/delete")
  36. @ResponseBody
  37. public JsonBean<Void> delete(String id) {
  38. try {
  39. Map<String, Object> info = template.queryForMap("select * from magic_api_info where id = ?", id);
  40. if (info != null) {
  41. template.update("delete from magic_api_info where id = ?", id);
  42. }
  43. return new JsonBean<>();
  44. } catch (Exception e) {
  45. logger.error("删除接口出错", e);
  46. return new JsonBean<>(-1, e.getMessage());
  47. }
  48. }
  49. @RequestMapping("/list")
  50. @ResponseBody
  51. public JsonBean<List<ApiInfo>> list() {
  52. try {
  53. return new JsonBean<>(template.query("select id,api_name name,api_path path,api_method method from magic_api_info order by api_update_time desc",new BeanPropertyRowMapper<ApiInfo>(ApiInfo.class)));
  54. } catch (Exception e) {
  55. logger.error("查询接口列表失败", e);
  56. return new JsonBean<>(-1, e.getMessage());
  57. }
  58. }
  59. @RequestMapping("/continue")
  60. @ResponseBody
  61. public JsonBean<Object> debugContinue(String id){
  62. MagicScriptDebugContext context = MagicScriptDebugContext.getDebugContext(id);
  63. if(context == null){
  64. return new JsonBean<>(0,"debug session not found!");
  65. }
  66. try {
  67. context.singal();
  68. } catch (InterruptedException e) {
  69. e.printStackTrace();
  70. }
  71. if(context.isRunning()){
  72. return new JsonBean<>(1000,context.getId(),context.getDebugInfo());
  73. }else if(context.isException()){
  74. return resolveThrowable((Throwable) context.getReturnValue());
  75. }
  76. return new JsonBean<>(context.getReturnValue());
  77. }
  78. @RequestMapping("/test")
  79. @ResponseBody
  80. public JsonBean<Object> test(@RequestBody(required = false) Map<String, Object> request) {
  81. Object script = request.get("script");
  82. if (script != null) {
  83. request.remove("script");
  84. Object breakpoints = request.get("breakpoints");
  85. request.remove("breakpoints");
  86. MagicScriptDebugContext context = new MagicScriptDebugContext(request);
  87. try {
  88. context.setBreakpoints((List<Integer>) breakpoints);
  89. context.setTimeout(this.debugTimeout);
  90. Object result = MagicScriptEngine.execute(script.toString(), context);
  91. if(context.isRunning()){
  92. return new JsonBean<>(1000,context.getId(),result);
  93. }else if(context.isException()){
  94. return resolveThrowable((Throwable) context.getReturnValue());
  95. }
  96. return new JsonBean<>(result);
  97. } catch (Exception e) {
  98. return resolveThrowable(e);
  99. }
  100. }
  101. return new JsonBean<>(0, "脚本不能为空");
  102. }
  103. private JsonBean<Object> resolveThrowable(Throwable root){
  104. MagicScriptError.ScriptException se = null;
  105. Throwable parent = root;
  106. do{
  107. if(parent instanceof MagicScriptError.ScriptException){
  108. se = (MagicScriptError.ScriptException)parent;
  109. }
  110. }while((parent = parent.getCause()) != null);
  111. logger.error("测试脚本出错",root);
  112. if(se != null){
  113. Span.Line line = se.getLine();
  114. return new JsonBean<>(-1000, se.getSimpleMessage(), line == null ? null : Arrays.asList(line.getLineNumber(), line.getEndLineNumber(),line.getStartCol(), line.getEndCol()));
  115. }
  116. return new JsonBean<>(-1,root.getMessage());
  117. }
  118. @RequestMapping("/get")
  119. @ResponseBody
  120. public JsonBean<Map<String, Object>> get(String id) {
  121. try {
  122. return new JsonBean<>(template.queryForMap("select * from magic_api_info where id = ?", id));
  123. } catch (Exception e) {
  124. logger.error("查询接口出错");
  125. return new JsonBean<>(-1, e.getMessage());
  126. }
  127. }
  128. @RequestMapping("/save")
  129. @ResponseBody
  130. public JsonBean<String> save(ApiInfo info) {
  131. try {
  132. if (StringUtils.isBlank(info.getMethod())) {
  133. return new JsonBean<>(0, "请求方法不能为空");
  134. }
  135. if (StringUtils.isBlank(info.getPath())) {
  136. return new JsonBean<>(0, "请求路径不能为空");
  137. }
  138. if (StringUtils.isBlank(info.getName())) {
  139. return new JsonBean<>(0, "接口名称不能为空");
  140. }
  141. if (StringUtils.isBlank(info.getScript())) {
  142. return new JsonBean<>(0, "脚本内容不能为空");
  143. }
  144. if (StringUtils.isBlank(info.getId())) {
  145. info.setId(UUID.randomUUID().toString().replace("-", ""));
  146. Integer count = template.queryForObject("select count(*) from magic_api_info where api_method = ? and api_path = ?",
  147. Integer.class,
  148. info.getMethod(),
  149. info.getPath());
  150. if (count > 0) {
  151. return new JsonBean<>(0, String.format("接口%s:%s已存在", info.getMethod(), info.getPath()));
  152. }
  153. long time = System.currentTimeMillis();
  154. template.update("insert into magic_api_info(id,api_method,api_path,api_script,api_name,api_create_time,api_update_time) values(?,?,?,?,?,?,?)",
  155. info.getId(),
  156. info.getMethod(),
  157. info.getPath(),
  158. info.getScript(),
  159. info.getName(),
  160. time,
  161. time);
  162. } else {
  163. Integer count = template.queryForObject("select count(*) from magic_api_info where api_method = ? and api_path = ? and id !=?",
  164. Integer.class,
  165. info.getMethod(),
  166. info.getPath(),
  167. info.getId());
  168. if (count > 0) {
  169. return new JsonBean<>(0, String.format("接口%s:%s已存在", info.getMethod(), info.getPath()));
  170. }
  171. template.update("update magic_api_info set api_method = ?,api_path = ?,api_script = ?,api_name = ?,api_update_time = ? where id = ?",
  172. info.getMethod(),
  173. info.getPath(),
  174. info.getScript(),
  175. info.getName(),
  176. System.currentTimeMillis(),
  177. info.getId());
  178. }
  179. return new JsonBean<>(info.getId());
  180. } catch (Exception e) {
  181. logger.error("保存接口出错", e);
  182. return new JsonBean<>(-1, e.getMessage());
  183. }
  184. }
  185. }