|
| 1 | +# tio-boot 整合 magic-script |
| 2 | + |
| 3 | +## magic-script 简介 |
| 4 | + |
| 5 | +magic-script 是基于 jvm 的动态脚本语言https://www.ssssssss.org/magic-api/pages/base/script/ |
| 6 | + |
| 7 | +## tio-boot 整合 magic-script |
| 8 | + |
| 9 | +1.添加依赖 |
| 10 | +添加 magic-script 依赖 |
| 11 | + |
| 12 | +```xml |
| 13 | +<magic-script.version>1.8.8</magic-script.version> |
| 14 | +<dependency> |
| 15 | + <groupId>org.ssssssss</groupId> |
| 16 | + <artifactId>magic-script</artifactId> |
| 17 | + <version>${magic-script.version}</version> |
| 18 | +</dependency> |
| 19 | +``` |
| 20 | + |
| 21 | +完整的依赖如下 |
| 22 | + |
| 23 | +```xml |
| 24 | +<properties> |
| 25 | + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
| 26 | + <java.version>1.8</java.version> |
| 27 | + <maven.compiler.source>${java.version}</maven.compiler.source> |
| 28 | + <maven.compiler.target>${java.version}</maven.compiler.target> |
| 29 | + <graalvm.version>23.1.1</graalvm.version> |
| 30 | + <tio-boot.version>1.4.2</tio-boot.version> |
| 31 | + <lombok-version>1.18.30</lombok-version> |
| 32 | + <hotswap-classloader.version>1.2.2</hotswap-classloader.version> |
| 33 | + <magic-script.version>1.8.8</magic-script.version> |
| 34 | + <final.name>web-hello</final.name> |
| 35 | + <main.class>com.litongjava.tio.web.hello.HelloApp</main.class> |
| 36 | +</properties> |
| 37 | +<dependencies> |
| 38 | + |
| 39 | + <dependency> |
| 40 | + <groupId>com.alibaba.fastjson2</groupId> |
| 41 | + <artifactId>fastjson2</artifactId> |
| 42 | + <version>2.0.12</version> |
| 43 | + </dependency> |
| 44 | + |
| 45 | + <dependency> |
| 46 | + <groupId>org.ssssssss</groupId> |
| 47 | + <artifactId>magic-script</artifactId> |
| 48 | + <version>${magic-script.version}</version> |
| 49 | + </dependency> |
| 50 | + |
| 51 | + |
| 52 | + <dependency> |
| 53 | + <groupId>ch.qos.logback</groupId> |
| 54 | + <artifactId>logback-classic</artifactId> |
| 55 | + <version>1.2.3</version> |
| 56 | + </dependency> |
| 57 | + |
| 58 | + <dependency> |
| 59 | + <groupId>com.litongjava</groupId> |
| 60 | + <artifactId>tio-boot</artifactId> |
| 61 | + <version>${tio-boot.version}</version> |
| 62 | + </dependency> |
| 63 | + |
| 64 | + <dependency> |
| 65 | + <groupId>org.projectlombok</groupId> |
| 66 | + <artifactId>lombok</artifactId> |
| 67 | + <version>${lombok-version}</version> |
| 68 | + <optional>true</optional> |
| 69 | + <scope>provided</scope> |
| 70 | + </dependency> |
| 71 | + |
| 72 | + <dependency> |
| 73 | + <groupId>com.litongjava</groupId> |
| 74 | + <artifactId>hotswap-classloader</artifactId> |
| 75 | + <version>${hotswap-classloader.version}</version> |
| 76 | + </dependency> |
| 77 | + |
| 78 | + <dependency> |
| 79 | + <groupId>junit</groupId> |
| 80 | + <artifactId>junit</artifactId> |
| 81 | + <version>4.12</version> |
| 82 | + <scope>test</scope> |
| 83 | + </dependency> |
| 84 | + |
| 85 | +</dependencies> |
| 86 | +``` |
| 87 | + |
| 88 | +添加工具类 |
| 89 | +创建了一个名为 `ScriptManager` 的工具类,这个类包含两个方法:`executeScript` 和 `executeClasspathScript`。 |
| 90 | + |
| 91 | +- `executeScript(String script, MagicScriptContext context)`:这个方法通过传入的脚本字符串和上下文对象来执行脚本。它首先检查上下文是否为调试模式,然后创建并执行脚本。 |
| 92 | +- `executeClasspathScript(String filename)`:这个方法读取类路径下的脚本文件,转换为字符串,并执行它。这对于从资源文件夹中加载和执行脚本非常有用。 |
| 93 | + |
| 94 | +```java |
| 95 | +package com.litongjava.magic.script; |
| 96 | + |
| 97 | +import java.net.URL; |
| 98 | +import java.nio.charset.StandardCharsets; |
| 99 | + |
| 100 | +import org.ssssssss.script.MagicScript; |
| 101 | +import org.ssssssss.script.MagicScriptContext; |
| 102 | +import org.ssssssss.script.MagicScriptDebugContext; |
| 103 | + |
| 104 | +import com.litongjava.tio.utils.hutool.FileUtil; |
| 105 | +import com.litongjava.tio.utils.hutool.ResourceUtil; |
| 106 | + |
| 107 | +public class ScriptManager { |
| 108 | + /** |
| 109 | + * 执行脚本 |
| 110 | + */ |
| 111 | + public static Object executeScript(String script, MagicScriptContext context) { |
| 112 | + script = (context instanceof MagicScriptDebugContext ? MagicScript.DEBUG_MARK : "") + script; |
| 113 | + MagicScript magicScript = MagicScript.create(script, null); |
| 114 | + // 执行脚本 |
| 115 | + return magicScript.execute(context); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * 执行class-path下的脚步 |
| 120 | + */ |
| 121 | + @SuppressWarnings("unchecked") |
| 122 | + public static <T> T executeClasspathScript(String filename) throws Exception { |
| 123 | + // 读取脚本文件 |
| 124 | + URL resource = ResourceUtil.getResource(filename); |
| 125 | + java.io.File file = FileUtil.file(resource.getFile()); |
| 126 | + byte[] bytes = FileUtil.readBytes(file); |
| 127 | + String script = new String(bytes, StandardCharsets.UTF_8); |
| 128 | + |
| 129 | + // 创建脚本上下文 |
| 130 | + MagicScriptContext context = new MagicScriptContext(); |
| 131 | + |
| 132 | + // 执行脚本 |
| 133 | + Object result = ScriptManager.executeScript(script, context); |
| 134 | + |
| 135 | + return (T) result; |
| 136 | + } |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +1.3.运行一个脚本 |
| 141 | +创建 magic-script 脚本文件 src\main\resources\ms\helloworld.ms |
| 142 | + |
| 143 | +```js |
| 144 | +var sql = """select * from test_data""" |
| 145 | +println(sql) |
| 146 | +return """hello""" |
| 147 | +``` |
| 148 | + |
| 149 | +如何通过代码运行 src\main\resources\ms\helloworld.ms |
| 150 | + |
| 151 | +使用 Java 代码运行脚本 |
| 152 | +创建了一个 `ScriptRunner` 类,其中的 `main` 方法用于从资源文件 `helloworld.ms` 中读取脚本并执行。这个脚本包含一个 SQL 语句和一个打印语句,并返回一个字符串 "hello"。 |
| 153 | + |
| 154 | +```java |
| 155 | +package com.litongjava.magic.script; |
| 156 | + |
| 157 | +import java.io.IOException; |
| 158 | +import java.nio.charset.StandardCharsets; |
| 159 | +import java.nio.file.Files; |
| 160 | +import java.nio.file.Path; |
| 161 | +import java.nio.file.Paths; |
| 162 | + |
| 163 | +import org.ssssssss.script.MagicScriptContext; |
| 164 | + |
| 165 | +public class ScriptRunner { |
| 166 | + |
| 167 | + public static void main(String[] args) { |
| 168 | + // 设置文件路径 |
| 169 | + Path path = Paths.get("src/main/resources/ms/helloworld.ms"); |
| 170 | + |
| 171 | + try { |
| 172 | + // 读取脚本文件 |
| 173 | + byte[] bytes = Files.readAllBytes(path); |
| 174 | + String script = new String(bytes, StandardCharsets.UTF_8); |
| 175 | + |
| 176 | + // 创建脚本上下文 |
| 177 | + MagicScriptContext context = new MagicScriptContext(); |
| 178 | + |
| 179 | + // 执行脚本 |
| 180 | + Object result = ScriptManager.executeScript(script, context); |
| 181 | + System.out.println(result); |
| 182 | + } catch (IOException e) { |
| 183 | + e.printStackTrace(); |
| 184 | + } |
| 185 | + } |
| 186 | +} |
| 187 | +``` |
| 188 | + |
| 189 | +1.4.整合 web 请求 |
| 190 | +如何在 Web 服务中使用 Magic Script 来处理 HTTP 请求。这涉及到几个步骤: |
| 191 | + |
| 192 | +- **脚本文件 `web_hello.ms`**:包含用于 Web 请求的脚本,它从 `TioControllerContext` 获取 HTTP 请求和响应对象,设置 JSON 响应并返回。 |
| 193 | +- **配置类 `HttpServerRequestHanlderConfig`**:在这个类中,你通过使用 `SimpleHttpRoutes` 来添加一个路由 "/hi",该路由关联到 `web_hello.ms` 脚本。这样,每当有 HTTP 请求到这个路由时,就会执行对应的脚本。 |
| 194 | + |
| 195 | +**处理流程**: |
| 196 | + |
| 197 | +1. **Web 请求到达**:当一个 HTTP 请求到达路由 "/hi" 时,路由处理函数被触发。 |
| 198 | +2. **执行脚本**:路由处理函数调用 `ScriptManager.executeClasspathScript` 来执行 `web_hello.ms` 脚本。 |
| 199 | +3. **脚本处理**:脚本从 Tio 框架的上下文中获取请求和响应对象,执行数据库查询,然后设置响应对象的 JSON 格式,并返回这个响应对象。 |
| 200 | +4. **返回响应**:最终,响应对象被转换为 JSON 格式并发送回客户端。 |
| 201 | + |
| 202 | +src\main\resources\ms\web_hello.ms |
| 203 | + |
| 204 | +``` |
| 205 | +import com.litongjava.tio.http.common.HttpRequest |
| 206 | +import com.litongjava.tio.http.common.HttpResponse |
| 207 | +import com.litongjava.tio.boot.http.TioControllerContext |
| 208 | +import com.litongjava.tio.utils.resp.RespVo |
| 209 | +
|
| 210 | +//调用静态方法 |
| 211 | +var request=TioControllerContext.getRequest(); |
| 212 | +var response=TioControllerContext.getResponse(); |
| 213 | +
|
| 214 | +
|
| 215 | +var sql = """select * from test_data where name=?""" |
| 216 | +println(sql) |
| 217 | +
|
| 218 | +response.setJson(RespVo.ok(sql)) |
| 219 | +return response; |
| 220 | +``` |
| 221 | + |
| 222 | +添加 handler |
| 223 | + |
| 224 | +```java |
| 225 | +HttpServerRequestHanlderConfig |
| 226 | + |
| 227 | +package com.litongjava.tio.web.hello.config; |
| 228 | + |
| 229 | +import com.litongjava.jfinal.aop.annotation.AInitialization; |
| 230 | +import com.litongjava.jfinal.aop.annotation.BeforeStartConfiguration; |
| 231 | +import com.litongjava.magic.script.ScriptManager; |
| 232 | +import com.litongjava.tio.boot.server.TioBootServer; |
| 233 | +import com.litongjava.tio.http.server.handler.SimpleHttpRoutes; |
| 234 | + |
| 235 | +@BeforeStartConfiguration |
| 236 | +public class HttpServerRequestHanlderConfig { |
| 237 | + |
| 238 | + @AInitialization |
| 239 | + public void httpRoutes() { |
| 240 | + |
| 241 | + // 创建simpleHttpRoutes |
| 242 | + SimpleHttpRoutes simpleHttpRoutes = new SimpleHttpRoutes(); |
| 243 | + |
| 244 | + simpleHttpRoutes.add("/hi", (request) -> { |
| 245 | + String filename = "ms/web_hello.ms"; |
| 246 | + return ScriptManager.executeClasspathScript(filename); |
| 247 | + |
| 248 | + }); |
| 249 | + |
| 250 | + // 将simpleHttpRoutes添加到TioBootServer |
| 251 | + TioBootServer.me().setHttpRoutes(simpleHttpRoutes); |
| 252 | + } |
| 253 | +} |
| 254 | +``` |
| 255 | + |
| 256 | +response 如下 |
| 257 | + |
| 258 | +``` |
| 259 | +{"data":"select * from test_data where name=?","ok":true,"msg":null,"code":1} |
| 260 | +``` |
| 261 | + |
| 262 | +## ref |
| 263 | + |
| 264 | +- https://www.ssssssss.org/magic-api/pages/base/script/ |
| 265 | +- https://www.ssssssss.org/magic-api/pages/function/other/ |
0 commit comments