diff --git a/spring-lua/.idea/.gitignore b/spring-lua/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/spring-lua/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/spring-lua/.idea/compiler.xml b/spring-lua/.idea/compiler.xml new file mode 100644 index 0000000..e43b24c --- /dev/null +++ b/spring-lua/.idea/compiler.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-lua/.idea/encodings.xml b/spring-lua/.idea/encodings.xml new file mode 100644 index 0000000..0e1c064 --- /dev/null +++ b/spring-lua/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/spring-lua/.idea/git_toolbox_blame.xml b/spring-lua/.idea/git_toolbox_blame.xml new file mode 100644 index 0000000..7dc1249 --- /dev/null +++ b/spring-lua/.idea/git_toolbox_blame.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/spring-lua/.idea/jarRepositories.xml b/spring-lua/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/spring-lua/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/spring-lua/.idea/misc.xml b/spring-lua/.idea/misc.xml new file mode 100644 index 0000000..fdc35ea --- /dev/null +++ b/spring-lua/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/spring-lua/.idea/uiDesigner.xml b/spring-lua/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/spring-lua/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-lua/.idea/vcs.xml b/spring-lua/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/spring-lua/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/wxdgaming.boot.spring/wxdgaming.boot.springstarter/src/lua/util/GlobalUtil.lua b/spring-lua/lua/GlobalUtil.lua similarity index 88% rename from wxdgaming.boot.spring/wxdgaming.boot.springstarter/src/lua/util/GlobalUtil.lua rename to spring-lua/lua/GlobalUtil.lua index ad6e43c..45a8054 100644 --- a/wxdgaming.boot.spring/wxdgaming.boot.springstarter/src/lua/util/GlobalUtil.lua +++ b/spring-lua/lua/GlobalUtil.lua @@ -4,8 +4,6 @@ --- DateTime: 2024/7/28 下午2:49 --- -JsonUtil = luajava.bindClass("wxdgaming.boot.core.str.json.FastJsonUtil"); - -- 不要用这种方式,特别耗时 --function logbackUtil() -- return luajava.bindClass("logback.LogbackUtil"); @@ -25,4 +23,9 @@ end --- 设置数据 function setLuaData(key, value) return lua_data:put(key, value); -end \ No newline at end of file +end + +function urlPath(request) + return request:getServletPath(); +end + diff --git a/wxdgaming.boot.spring/wxdgaming.boot.springstarter/src/lua/script/web/index.lua b/spring-lua/lua/index.lua similarity index 55% rename from wxdgaming.boot.spring/wxdgaming.boot.springstarter/src/lua/script/web/index.lua rename to spring-lua/lua/index.lua index 0572f75..cb31487 100644 --- a/wxdgaming.boot.spring/wxdgaming.boot.springstarter/src/lua/script/web/index.lua +++ b/spring-lua/lua/index.lua @@ -4,15 +4,22 @@ --- DateTime: 2024/7/27 下午11:36 --- -function index(info) - local request = info.request - local response = info.response - local body = info.body +function root(request, response, postBody) + responseUtil:responseText(response, "hello world") +end + +function index(request, response, postBody) + print("") + print(request:getServletPath()) print("request = " .. tostring(request)) print("response = " .. tostring(response)) - local var = JsonUtil:toJson(info) - print(var) - return "lua -- dd" + responseUtil:responseText(response, "lua -- dd") +end + +function index3(request, response, postBody) + print("index3") + local obj = { error = 0, msg = "ok" }; + responseUtil:responseObj(response, obj) end function paramsTest(p1, p2, p3, p4, p5, p6) diff --git a/spring-lua/pom.xml b/spring-lua/pom.xml new file mode 100644 index 0000000..edba94f --- /dev/null +++ b/spring-lua/pom.xml @@ -0,0 +1,108 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.3.2 + + + wxd-gaming.boot + spring-lua + 0.0.1-SNAPSHOT + spring-lua + spring-lua + + + + + + + + + + + + + + + 21 + + + + org.springframework.boot + spring-boot-starter-web + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + junit + junit + test + + + com.alibaba + fastjson + 2.0.45 + + + + ch.qos.logback + logback-classic + + + ch.qos.logback + logback-core + + + org.slf4j + slf4j-api + + + org.apache.logging.log4j + log4j-core + + + + org.apache.bcel + bcel + 6.7.0 + + + org.luaj + luaj-jse + 3.0.1 + + + + + + + org.graalvm.buildtools + native-maven-plugin + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/spring-lua/src/main/java/wxdgaming/boot/springlua/SpringLuaApplication.java b/spring-lua/src/main/java/wxdgaming/boot/springlua/SpringLuaApplication.java new file mode 100644 index 0000000..ca1507d --- /dev/null +++ b/spring-lua/src/main/java/wxdgaming/boot/springlua/SpringLuaApplication.java @@ -0,0 +1,26 @@ +package wxdgaming.boot.springlua; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; +import org.springframework.boot.context.properties.ConfigurationPropertiesScan; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.annotation.EnableScheduling; + +@EnableAsync +@EnableScheduling +@ConfigurationPropertiesScan +@SpringBootApplication( + exclude = { + DataSourceAutoConfiguration.class, + MongoAutoConfiguration.class + } +) +public class SpringLuaApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringLuaApplication.class, args); + } + +} diff --git a/spring-lua/src/main/java/wxdgaming/boot/springlua/controller/ApiController.java b/spring-lua/src/main/java/wxdgaming/boot/springlua/controller/ApiController.java new file mode 100644 index 0000000..0be5594 --- /dev/null +++ b/spring-lua/src/main/java/wxdgaming/boot/springlua/controller/ApiController.java @@ -0,0 +1,48 @@ +package wxdgaming.boot.springlua.controller; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.luaj.vm2.LuaValue; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import wxdgaming.boot.springlua.serice.LuaService; + +import java.io.IOException; + +/** + * @author: wxd-gaming(無心道, 15388152619) + * @version: 2024-07-31 20:18 + **/ +@Controller +public class ApiController { + + final LuaService luaService; + + @Autowired + public ApiController(LuaService luaService) { + this.luaService = luaService; + } + + @RequestMapping("/lua/reload") + public String reload() throws IOException { + luaService.init(); + return "ok"; + } + + @RequestMapping("/**") + public void all(HttpServletRequest request, HttpServletResponse response, @RequestBody(required = false) String body) throws IOException { + String servletPath = request.getServletPath(); + if (servletPath.startsWith("/")) + servletPath = servletPath.substring(1); + servletPath = servletPath.replace("/", "_"); + LuaValue[] luaValues = luaService.parse(request, response, body); + LuaValue lua_func = luaService.get(servletPath); + if (lua_func == null || lua_func == LuaValue.NIL) { + lua_func = luaService.get("root"); + } + lua_func.invoke(luaValues); + } + +} diff --git a/spring-lua/src/main/java/wxdgaming/boot/springlua/filter/ApiAllowOrigin.java b/spring-lua/src/main/java/wxdgaming/boot/springlua/filter/ApiAllowOrigin.java new file mode 100644 index 0000000..8b68e74 --- /dev/null +++ b/spring-lua/src/main/java/wxdgaming/boot/springlua/filter/ApiAllowOrigin.java @@ -0,0 +1,24 @@ +package wxdgaming.boot.springlua.filter; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; + +/** + * 过滤器,处理跨域 + * + * @author: wxd-gaming(無心道, 15388152619) + * @version: 2024-08-01 08:45 + **/ +@Component +public class ApiAllowOrigin implements HandlerInterceptor { + + @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + response.setHeader("Access-Control-Allow-Origin", "*"); + response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); + response.setHeader("Access-Control-Max-Age", "3600"); + response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type"); + return HandlerInterceptor.super.preHandle(request, response, handler); + } +} diff --git a/spring-lua/src/main/java/wxdgaming/boot/springlua/serice/LuaService.java b/spring-lua/src/main/java/wxdgaming/boot/springlua/serice/LuaService.java new file mode 100644 index 0000000..e81d0d5 --- /dev/null +++ b/spring-lua/src/main/java/wxdgaming/boot/springlua/serice/LuaService.java @@ -0,0 +1,77 @@ +package wxdgaming.boot.springlua.serice; + +import jakarta.annotation.PostConstruct; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import org.luaj.vm2.Globals; +import org.luaj.vm2.LuaValue; +import org.luaj.vm2.Varargs; +import org.luaj.vm2.lib.jse.CoerceJavaToLua; +import org.luaj.vm2.lib.jse.JsePlatform; +import org.springframework.stereotype.Service; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +/** + * lua service + * + * @author: wxd-gaming(無心道, 15388152619) + * @version: 2024-07-31 20:08 + **/ +@Slf4j +@Getter +@Service +public class LuaService { + + private Globals globals; + + public LuaService() { + + } + + @PostConstruct + public void init() throws IOException { + this.globals = JsePlatform.standardGlobals(); + this.globals.set("responseUtil", CoerceJavaToLua.coerce(new ResponseUtil())); + Files.walk(Path.of("lua"), 99) + .map(Path::toFile) + .filter(File::isFile) + .forEach(f -> { + try { + String path = f.getPath(); + log.info("load lua {}", path); + String string = Files.readString(f.toPath()); + this.globals.load(string, path).call(); + } catch (Exception e) { + log.error("load lua error", e); + } + }); + } + + public LuaValue[] parse(Object... params) { + LuaValue[] luaValues = new LuaValue[params.length]; + for (int i = 0; i < params.length; i++) { + luaValues[i] = CoerceJavaToLua.coerce(params[i]); + } + return luaValues; + } + + public LuaValue get(String key) { + return this.globals.get(key); + } + + public LuaValue func(String method, Object... params) { + LuaValue luaValue = this.globals.get(method); + LuaValue[] luaValues = parse(params); + Varargs invoke = luaValue.invoke(luaValues); + LuaValue ret = null; + if (invoke != null && invoke != LuaValue.NONE && invoke != LuaValue.NIL) { + ret = invoke.arg1(); + } + return ret; + } + +} diff --git a/spring-lua/src/main/java/wxdgaming/boot/springlua/serice/ResponseUtil.java b/spring-lua/src/main/java/wxdgaming/boot/springlua/serice/ResponseUtil.java new file mode 100644 index 0000000..83a8bfe --- /dev/null +++ b/spring-lua/src/main/java/wxdgaming/boot/springlua/serice/ResponseUtil.java @@ -0,0 +1,74 @@ +package wxdgaming.boot.springlua.serice; + +import com.alibaba.fastjson.JSONObject; +import jakarta.servlet.ServletOutputStream; +import jakarta.servlet.http.HttpServletResponse; +import lombok.extern.slf4j.Slf4j; +import org.luaj.vm2.LuaTable; +import org.luaj.vm2.LuaValue; +import org.luaj.vm2.lib.jse.CoerceLuaToJava; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; + +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; + +/** + * @author: wxd-gaming(無心道, 15388152619) + * @version: 2024-07-31 20:39 + **/ +@Slf4j +public class ResponseUtil { + + public void response(HttpServletResponse response, String contentType, String text) throws Exception { + response(response, contentType, text.getBytes(StandardCharsets.UTF_8)); + } + + public void response(HttpServletResponse response, String contentType, byte[] bytes) throws Exception { + response.setStatus(HttpStatus.OK.value()); + response.setContentType(contentType); + try (ServletOutputStream outputStream = response.getOutputStream()) { + outputStream.write(bytes); + outputStream.flush(); + } + } + + public void responseObj(HttpServletResponse response, Object obj) throws Exception { + String res; + if (obj instanceof LuaTable luaTable) { + JSONObject jsonObject = new JSONObject(true); + LuaValue[] keys = luaTable.keys(); + for (LuaValue key : keys) { + jsonObject.put(key.toString(), CoerceLuaToJava.coerce(luaTable.get(key), String.class)); + } + res = jsonObject.toJSONString(); + } else if (obj instanceof LuaValue luaValue) { + res = (String) CoerceLuaToJava.coerce(luaValue, String.class); + } else { + res = String.valueOf(obj); + } + response( + response, + MediaType.TEXT_PLAIN.toString(), + res + ); + } + + public void responseJson(HttpServletResponse response, String json) throws Exception { + response(response, MediaType.APPLICATION_JSON.toString(), json); + } + + public void responseText(HttpServletResponse response, String text) throws Exception { + response(response, MediaType.TEXT_PLAIN.toString(), text); + } + + public void responseFile(HttpServletResponse response, String filePath) throws Exception { + File file = new File(filePath); + response.setHeader("Content-Disposition", "attachment;filename=" + file.getName()); + byte[] bytes = Files.readAllBytes(file.toPath()); + response(response, "application/x-download", bytes); + log.info("fileName={}", file.getName()); + } + +} diff --git a/spring-lua/src/main/resources/application.yml b/spring-lua/src/main/resources/application.yml new file mode 100644 index 0000000..e05c1db --- /dev/null +++ b/spring-lua/src/main/resources/application.yml @@ -0,0 +1,20 @@ +server: + port: 18801 + tomcat: + max-threads: 200 + min-spare-threads: 10 + +spring: + web: + resources: + chain: + cache: false + static-locations: classpath:/public/ + servlet: + multipart: + max-file-size: 20MB + max-request-size: 100MB + enabled: true + autoconfigure: + exclude: + - org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration diff --git a/spring-lua/src/main/resources/logback-spring.xml b/spring-lua/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..cb25803 --- /dev/null +++ b/spring-lua/src/main/resources/logback-spring.xml @@ -0,0 +1,67 @@ + + + + + + + + + + ${LOG_PATH}/error.log + + true + + + ${LOG_PATTERN} + utf-8 + + + + error + ACCEPT + DENY + + + + ${LOG_PATH}/logback/error-%d{yyyy-MM-dd}.log.gz + 20 + + + + + + + ${LOG_PATH}/app.log + + true + + + ${LOG_PATTERN} + utf-8 + + + + ${LOG_PATH}/logback/app-%d{yyyy-MM-dd}.log.gz + 20 + + + + + + + ${LOG_PATTERN} + + + + + + + + + + + + + + + diff --git a/spring-lua/src/test/java/code/TypeTest.java b/spring-lua/src/test/java/code/TypeTest.java new file mode 100644 index 0000000..e5c1a35 --- /dev/null +++ b/spring-lua/src/test/java/code/TypeTest.java @@ -0,0 +1,13 @@ +package code; + +import org.junit.Test; +import org.springframework.http.MediaType; + +public class TypeTest { + + @Test + public void t1() { + System.out.println(MediaType.TEXT_PLAIN.toString()); + } + +} diff --git a/wxdgaming.boot.spring/wxdgaming.boot.springstarter/src/main/java/wxdgaming/boot/spring/starter/service/LuaService.java b/wxdgaming.boot.spring/wxdgaming.boot.springstarter/src/main/java/wxdgaming/boot/spring/starter/service/LuaService.java deleted file mode 100644 index 11c7019..0000000 --- a/wxdgaming.boot.spring/wxdgaming.boot.springstarter/src/main/java/wxdgaming/boot/spring/starter/service/LuaService.java +++ /dev/null @@ -1,28 +0,0 @@ -package wxdgaming.boot.spring.starter.service; - -import jakarta.annotation.PostConstruct; -import lombok.Getter; -import lombok.extern.slf4j.Slf4j; -import org.springframework.context.annotation.Configuration; -import wxdgaming.boot.LuaBus; - -/** - * lua - * - * @author: wxd-gaming(無心道, 15388152619) - * @version: 2024-07-28 12:15 - **/ - -@Slf4j -@Getter -@Configuration -public class LuaService { - - LuaBus luaBus; - - @PostConstruct - public void init() { - luaBus = LuaBus.buildFromDirs("wxdgaming.boot.springstarter/src/lua"); - } - -} diff --git a/wxdgaming.boot.spring/wxdgaming.boot.springstarter/src/test/java/wxdgaming/boot/springstarter/scripts/TestApi.java b/wxdgaming.boot.spring/wxdgaming.boot.springstarter/src/test/java/wxdgaming/boot/springstarter/scripts/TestApi.java index acac379..c193652 100644 --- a/wxdgaming.boot.spring/wxdgaming.boot.springstarter/src/test/java/wxdgaming/boot/springstarter/scripts/TestApi.java +++ b/wxdgaming.boot.spring/wxdgaming.boot.springstarter/src/test/java/wxdgaming/boot/springstarter/scripts/TestApi.java @@ -3,16 +3,12 @@ package wxdgaming.boot.springstarter.scripts; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; -import org.luaj.vm2.LuaTable; -import org.luaj.vm2.LuaValue; -import org.luaj.vm2.lib.jse.CoerceJavaToLua; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.config.annotation.InterceptorRegistration; import wxdgaming.boot.core.lang.RandomUtils; import wxdgaming.boot.spring.starter.core.BastFilter; -import wxdgaming.boot.spring.starter.service.LuaService; import wxdgaming.boot.spring.starter.test.B1; import wxdgaming.boot.spring.starter.test.B2; @@ -33,26 +29,17 @@ public class TestApi implements BastFilter { @Autowired B1 b1; @Autowired B2 b2; - @Autowired LuaService luaService; public TestApi() { System.out.println("\n" + this.getClass()); } - @RequestMapping("/**") - public ResponseEntity all( + @RequestMapping("/lua") + public ResponseEntity lua( HttpServletRequest httpServletRequest, - HttpServletResponse httpServletResponse) throws Exception { - LuaTable luaTable = new LuaTable(); - luaTable.set("request", CoerceJavaToLua.coerce(httpServletRequest)); - luaTable.set("response", CoerceJavaToLua.coerce(httpServletResponse)); - luaTable.set("body", "body"); - LuaValue index = luaService - .getLuaBus() - .get("index") - .call(luaTable); - String string = index.toString(); - return ResponseEntity.ok("ok-" + b1 + " - " + string); + HttpServletResponse httpServletResponse, + @RequestBody(required = false) String body) throws Exception { + return ResponseEntity.ok("ok-" + b1 + " - "); } @RequestMapping("/api")