@@ -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