Skip to content

Commit 10bcc78

Browse files
committed
Update the tinystruct to 1.7.10, Add junit test support.
1 parent 5d184e9 commit 10bcc78

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
<maven.compiler.target>17</maven.compiler.target>
3333
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3434
<tomcat.embed.version>11.0.11</tomcat.embed.version>
35-
<tinystruct.version>1.7.9</tinystruct.version>
35+
<tinystruct.version>1.7.10</tinystruct.version>
36+
<jupiter.version>6.0.1</jupiter.version>
3637
</properties>
3738
<dependencies>
3839
<dependency>
@@ -51,6 +52,12 @@
5152
<artifactId>tomcat-embed-core</artifactId>
5253
<version>${tomcat.embed.version}</version>
5354
</dependency>
55+
<dependency>
56+
<groupId>org.junit.jupiter</groupId>
57+
<artifactId>junit-jupiter-engine</artifactId>
58+
<version>${jupiter.version}</version>
59+
<scope>test</scope>
60+
</dependency>
5461
</dependencies>
5562
<build>
5663
<plugins>

src/main/java/org/tinystruct/system/TomcatServer.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void init() {
6868
@Argument(key = "http.proxyPort", description = "Proxy port for http"),
6969
@Argument(key = "https.proxyHost", description = "Proxy host for https"),
7070
@Argument(key = "https.proxyPort", description = "Proxy port for https")
71-
}, example = "bin/dispatcher start --import org.tinystruct.system.TomcatServer --server-port 777", mode = org.tinystruct.application.Action.Mode.CLI)
71+
}, example = "bin/dispatcher start --import org.tinystruct.system.TomcatServer --server-port 777", mode = Action.Mode.CLI)
7272
@Override
7373
public void start() throws ApplicationException {
7474
if (started) return;
@@ -111,7 +111,7 @@ public void start() throws ApplicationException {
111111
}
112112
}
113113

114-
System.out.println(ApplicationManager.call("--logo", null, org.tinystruct.application.Action.Mode.CLI));
114+
System.out.println(ApplicationManager.call("--logo", null, Action.Mode.CLI));
115115

116116
final long start = System.currentTimeMillis();
117117
final String webappDirLocation = ".";
@@ -150,7 +150,16 @@ public void start() throws ApplicationException {
150150

151151
// Open the default browser
152152
getContext().setAttribute("--url", "http://localhost:" + webPort);
153-
ApplicationManager.call("open", getContext(), org.tinystruct.application.Action.Mode.CLI);
153+
ApplicationManager.call("open", getContext(), Action.Mode.CLI);
154+
155+
// Keep the server running
156+
logger.info("Server is running. Press Ctrl+C to stop.");
157+
158+
// Add shutdown hook
159+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
160+
logger.info("Shutting down HTTP server...");
161+
stop();
162+
}));
154163

155164
tomcat.getServer().await();
156165
} catch (LifecycleException e) {
@@ -361,7 +370,8 @@ public void service(HttpServletRequest request, HttpServletResponse response) th
361370

362371
String query = _request.getParameter("q");
363372
if (query != null) {
364-
handleRequest(query, context, _response);
373+
Action.Mode mode = Action.Mode.fromName(request.getMethod());
374+
handleRequest(query, context, _response, mode);
365375
} else {
366376
handleDefaultPage(context, _response);
367377
}
@@ -438,12 +448,13 @@ else if(call instanceof String) {
438448
* @param query The query string
439449
* @param context The application context
440450
* @param response The HTTP response object
451+
* @param mode
441452
* @throws IOException if an I/O error occurs
442453
*/
443-
private void handleRequest(String query, org.tinystruct.application.Context context, Response<HttpServletResponse, ServletOutputStream> response) throws IOException, ApplicationException {
454+
private void handleRequest(String query, org.tinystruct.application.Context context, Response<HttpServletResponse, ServletOutputStream> response, Action.Mode mode) throws IOException, ApplicationException {
444455
// Handle request
445456
query = StringUtilities.htmlSpecialChars(query);
446-
Object message = ApplicationManager.call(query, context);
457+
Object message = ApplicationManager.call(query, context, mode);
447458
if (message != null) {
448459
if (message instanceof byte[]) {
449460
byte[] bytes = (byte[]) message;
@@ -470,7 +481,7 @@ private void handleRequest(String query, org.tinystruct.application.Context cont
470481
*/
471482
private void handleDefaultPage(org.tinystruct.application.Context context, Response<HttpServletResponse, ServletOutputStream> response) throws IOException, ApplicationException {
472483
try (BufferedWriter bufferedWriter = getWriter(response.get())) {
473-
bufferedWriter.write(String.valueOf(ApplicationManager.call(settings.getOrDefault("default.home.page", "say/Praise the Lord."), context)));
484+
bufferedWriter.write(String.valueOf(ApplicationManager.call(settings.getOrDefault("default.home.page", "say/Praise the Lord."), context, Action.Mode.HTTP_GET)));
474485
}
475486
}
476487

0 commit comments

Comments
 (0)